<?php
// msql_select_db($cfg_data,$link_vcrx);
// msql_select_db('vcrxol_vare',$link_vcrx);
// echo date("Y-m-d H:i:s");

// 

{


  
  
function crea_vndd0()
{
  include('../_cfg/config.php');
  $sql_enti ='select enti_codigo, enti_descri from enti0 ';
  $sql_enti.='where enti_codigo in(select distinct vndd from vtas1 where vndd is not null and vndd!="")';
  $sql_enti.='or enti_codigo in(select distinct vndd from clie1 where vndd is not null and vndd!="")';
  $res_enti=msql_query($sql_enti,$link_vcrx);
  
  while($aso_enti=msql_fetch_assoc($res_enti))
  {
    $xcodi=$aso_enti['enti_codigo'];
    $xdesc=$aso_enti['enti_descri'];
    $xcodi=substr($xcodi,0,10);
    if(!empty($xcodi))
    {
      $sql_inse='insert into vndd0(vndd_codigo,vndd_status,vndd_idate)value("'.$xcodi.'","-",now())';
      $res_inse=msql_query($sql_inse,$link_vcrx);
      $sql_updt='update vndd0 set vndd_descri="'.$xdesc.'" where vndd_codigo="'.$xcodi.'"';
      $res_updt=msql_query($sql_updt,$link_vcrx);
    }
  }
  
}
  
  
  
  
  
  
  
function regula_punimdlo0()
{
  include('../_cfg/config.php');
  
  $sql_item ='select * from item0 ';
  $sql_item.='where item_codigo like "20%" ';
  $sql_item.='and length(trim(item_codigo))=8 ';
  $sql_item.='order by item_codigo';
  $res_item=msql_query($sql_item,$link_vcrx);
  
  while($aso_item=msql_fetch_assoc($res_item))
  {regula_punimdlo($aso_item['item_codigo']);}
  
}
  
  
function regula_punimdlo($item)
{
//   return;
  include('../_cfg/config.php');
  if(empty($item)){return;}
  if(strlen($item)<8){return;}
  
  $sql_item='select * from item0 where item_codigo like "'.$item.'%" order by item_codigo';
  $res_item=msql_query($sql_item,$link_vcrx);
  $codigo=msql_result($res_item,0,'item_codigo');
  $item_mdlo=msql_result($res_item,0,'item_mdlo');
  $item_rfer=msql_result($res_item,0,'item_rfer');
  $master=msql_result($res_item,0,'item_master');
  $raiz=msql_result($res_item,0,'item_raiz');
  
  if(strlen($master)<6){$master=$codigo;}
  if(strlen($raiz)<6){$raiz=$codigo;}
  
  $puni=msql_result($res_item,0,'item_puni');
  $iva=msql_result($res_item,0,'item_iva');
  $ice=msql_result($res_item,0,'item_ice');
  $irbp=msql_result($res_item,0,'item_irbp');
  
  $puni1=@msql_result($res_item,0,'item_puni1');
  $puni2=@msql_result($res_item,0,'item_puni2');
  $puni3=@msql_result($res_item,0,'item_puni3');
  $puni4=@msql_result($res_item,0,'item_puni4');
  $puni5=@msql_result($res_item,0,'item_puni5');
  
  $dcto1=@msql_result($res_item,0,'item_dcto1');
  $dcto2=@msql_result($res_item,0,'item_dcto2');
  $dcto3=@msql_result($res_item,0,'item_dcto3');
  $dcto4=@msql_result($res_item,0,'item_dcto4');
  $dcto5=@msql_result($res_item,0,'item_dcto5');
  
  $pvp1=@msql_result($res_item,0,'item_pvp1');
  $pvp2=@msql_result($res_item,0,'item_pvp2');
  $pvp3=@msql_result($res_item,0,'item_pvp3');
  $pvp4=@msql_result($res_item,0,'item_pvp4');
  $pvp5=@msql_result($res_item,0,'item_pvp5');

  if($puni==0){$puni=$puni1;}
  if($puni1==0){$puni1=$puni;}
  $dcto1=0;

     
  // eliminado en agosto 2022
     if(1==2)
     {
          
  
  if($pvp1>0){$puni1=$pvp1/(1+$iva/100)/(1+$ice/100);}
  if($pvp2>0){$puni2=$pvp2/(1+$iva/100)/(1+$ice/100);}
  if($pvp3>0){$puni3=$pvp3/(1+$iva/100)/(1+$ice/100);}
  if($pvp4>0){$puni4=$pvp4/(1+$iva/100)/(1+$ice/100);}
  if($pvp5>0){$puni5=$pvp5/(1+$iva/100)/(1+$ice/100);}
  
  if($dcto2>0){$puni2=$puni1*(1-$dcto2/100);}
  if($dcto3>0){$puni3=$puni1*(1-$dcto3/100);}
  if($dcto4>0){$puni4=$puni1*(1-$dcto4/100);}
  if($dcto5>0){$puni5=$puni1*(1-$dcto5/100);}

  if($pvp1==0){$pvp1=$puni1*(1+$ice/100)*(1+$iva/100);}
  if($pvp2==0){$pvp2=$puni2*(1+$ice/100)*(1+$iva/100);}
  if($pvp3==0){$pvp3=$puni3*(1+$ice/100)*(1+$iva/100);}
  if($pvp4==0){$pvp4=$puni4*(1+$ice/100)*(1+$iva/100);}
  if($pvp5==0){$pvp5=$puni5*(1+$ice/100)*(1+$iva/100);}
    
  if($dcto2==0 and $puni2>0){$dcto2=(1-$puni2/$puni1)*100;}
  if($dcto3==0 and $puni3>0){$dcto3=(1-$puni3/$puni1)*100;}
  if($dcto4==0 and $puni4>0){$dcto4=(1-$puni4/$puni1)*100;}
  if($dcto5==0 and $puni5>0){$dcto5=(1-$puni5/$puni1)*100;}
     }
  

//   if(!empty($item_mdlo))
  {
    $sql_prec ='update item0 set ';
    $sql_prec.='item_puni1="'.$puni1.'", item_dcto1="'.$dcto1.'", item_pvp1="'.$pvp1.'", ';
    $sql_prec.='item_puni2="'.$puni2.'", item_dcto2="'.$dcto2.'", item_pvp2="'.$pvp2.'", ';
    $sql_prec.='item_puni3="'.$puni3.'", item_dcto3="'.$dcto3.'", item_pvp3="'.$pvp3.'", ';
    $sql_prec.='item_puni4="'.$puni4.'", item_dcto4="'.$dcto4.'", item_pvp4="'.$pvp4.'", ';
    $sql_prec.='item_puni5="'.$puni5.'", item_dcto5="'.$dcto5.'", item_pvp5="'.$pvp5.'", ';
    $sql_prec.='item_iva="'.$iva.'", item_ice="'.$ice.'", item_irbp="'.$irbp.'" ';
    $sql_prec.='where item_codigo like "'.$item.'%"';
    $res_prec=@msql_query($sql_prec,$link_vcrx);
    
  }
  
  if(!empty($item_mdlo))
  {

    $sql_prec ='update item0 set ';
    $sql_prec.='item_puni ="'.$puni1.'",  ';
    $sql_prec.='item_puni1="'.$puni1.'", item_dcto1="'.$dcto1.'", item_pvp1="'.$pvp1.'", ';
    $sql_prec.='item_puni2="'.$puni2.'", item_dcto2="'.$dcto2.'", item_pvp2="'.$pvp2.'", ';
    $sql_prec.='item_puni3="'.$puni3.'", item_dcto3="'.$dcto3.'", item_pvp3="'.$pvp3.'", ';
    $sql_prec.='item_puni4="'.$puni4.'", item_dcto4="'.$dcto4.'", item_pvp4="'.$pvp4.'", ';
    $sql_prec.='item_puni5="'.$puni5.'", item_dcto5="'.$dcto5.'", item_pvp5="'.$pvp5.'", ';
    $sql_prec.='item_iva="'.$iva.'", item_ice="'.$ice.'", item_irbp="'.$irbp.'" ';
    $sql_prec.='where item_codigo like "'.$item_mdlo.'%"';
    $res_prec=@msql_query($sql_prec,$link_vcrx);
    
    
//     $sql_prec='update item0 set item_iva="'.$iva.'", item_ice="'.$ice.'", item_irbp="'.$irbp.'" where item_codigo like "'.$item_mdlo.'%"';
//     $res_prec=@msql_query($sql_prec,$link_vcrx);
//     $sql_prec='update item0 set item_puni="'.$puni1.'", item_dcto1="'.$dcto1.'", item_pvp1="'.$pvp1.'" where item_codigo like "'.$item_mdlo.'%"';
//     $res_prec=@msql_query($sql_prec,$link_vcrx);
//     $sql_prec='update item0 set item_puni1="'.$puni1.'", item_dcto1="'.$dcto1.'", item_pvp1="'.$pvp1.'" where item_codigo like "'.$item_mdlo.'%"';
//     $res_prec=@msql_query($sql_prec,$link_vcrx);
//     $sql_prec='update item0 set item_puni2="'.$puni2.'", item_dcto2="'.$dcto2.'", item_pvp2="'.$pvp2.'" where item_codigo like "'.$item_mdlo.'%"';
//     $res_prec=@msql_query($sql_prec,$link_vcrx);
//     $sql_prec='update item0 set item_puni3="'.$puni3.'", item_dcto3="'.$dcto3.'", item_pvp3="'.$pvp3.'" where item_codigo like "'.$item_mdlo.'%"';
//     $res_prec=@msql_query($sql_prec,$link_vcrx);
//     $sql_prec='update item0 set item_puni4="'.$puni4.'", item_dcto4="'.$dcto4.'", item_pvp4="'.$pvp4.'" where item_codigo like "'.$item_mdlo.'%"';
//     $res_prec=@msql_query($sql_prec,$link_vcrx);
//     $sql_prec='update item0 set item_puni5="'.$puni5.'", item_dcto5="'.$dcto5.'", item_pvp5="'.$pvp5.'" where item_codigo like "'.$item_mdlo.'%"';
//     $res_prec=@msql_query($sql_prec,$link_vcrx);
  }
  
  $sql_puni='update item0 set item_puni1=item_puni where item_puni1=0 or item_puni1 is null';
  $res_puni=@msql_query($sql_puni,$link_vcrx);
  
  $sql_puni='update item0 set item_puni=item_puni1 where item_puni=0 or item_puni is null or';
  $res_puni=@msql_query($sql_puni,$link_vcrx);

  
  
  return;
}

  

function regula_pvp()
{
  include('../_cfg/config.php');
 
  $sql_pvp='update item0 set item_puni1=item_puni where item_puni1=0 or item_puni1 is null';
  $res_pvp=@msql_query($sql_pvp,$link_vcrx);
  
  $sql_pvp ='update item0 set ';
  $sql_pvp.='item_pvp1= case when ifnull(item_pvp1,0)=0 then item_puni1*(1+item_ice/100)*(1+item_iva/100) else item_pvp1 end, ';
  $sql_pvp.='item_pvp2= case when ifnull(item_pvp2,0)=0 then item_puni2*(1+item_ice/100)*(1+item_iva/100) else item_pvp2 end, ';
  $sql_pvp.='item_pvp3= case when ifnull(item_pvp3,0)=0 then item_puni3*(1+item_ice/100)*(1+item_iva/100) else item_pvp3 end, ';
  $sql_pvp.='item_pvp4= case when ifnull(item_pvp4,0)=0 then item_puni4*(1+item_ice/100)*(1+item_iva/100) else item_pvp4 end, ';
  $sql_pvp.='item_pvp5= case when ifnull(item_pvp5,0)=0 then item_puni5*(1+item_ice/100)*(1+item_iva/100) else item_pvp5 end ';
  $res_pvp=@msql_query($sql_pvp,$link_vcrx);

//   $sql_pvp ='update item0 set ';
//   $sql_pvp.='item_puni1= case when ifnull(item_puni1,0)=0 then item_pvp1/(1+item_iva/100)/(1+item_ice/100) else item_pvp1 end, ';
//   $sql_pvp.='item_puni2= case when ifnull(item_puni2,0)=0 then item_pvp2/(1+item_iva/100)/(1+item_ice/100) else item_pvp2 end, ';
//   $sql_pvp.='item_puni3= case when ifnull(item_puni3,0)=0 then item_pvp3/(1+item_iva/100)/(1+item_ice/100) else item_pvp3 end, ';
//   $sql_pvp.='item_puni4= case when ifnull(item_puni4,0)=0 then item_pvp4/(1+item_iva/100)/(1+item_ice/100) else item_pvp4 end, ';
//   $sql_pvp.='item_puni5= case when ifnull(item_puni5,0)=0 then item_pvp5/(1+item_iva/100)/(1+item_ice/100) else item_pvp5 end ';
//   $res_pvp=@msql_query($sql_pvp,$link_vcrx);
     
     
  $sql_pvp ='update item0 set ';
  $sql_pvp.='item_puni1= case when ifnull(item_pvp1,0)!=0 then item_pvp1/(1+item_iva/100)/(1+item_ice/100) else item_puni1 end, ';
  $sql_pvp.='item_puni2= case when ifnull(item_pvp2,0)!=0 then item_pvp2/(1+item_iva/100)/(1+item_ice/100) else item_puni2 end, ';
  $sql_pvp.='item_puni3= case when ifnull(item_pvp3,0)!=0 then item_pvp3/(1+item_iva/100)/(1+item_ice/100) else item_puni3 end, ';
  $sql_pvp.='item_puni4= case when ifnull(item_pvp4,0)!=0 then item_pvp4/(1+item_iva/100)/(1+item_ice/100) else item_puni4 end, ';
  $sql_pvp.='item_puni5= case when ifnull(item_pvp5,0)!=0 then item_pvp5/(1+item_iva/100)/(1+item_ice/100) else item_puni5 end ';
  $res_pvp=@msql_query($sql_pvp,$link_vcrx);
     
     
  /////////

//   $sql_pvp ='update item0 set ';
//   $sql_pvp.='item_pvp1= case when item_pvp1=0 or item_pvp1 is null then item_puni1*(1+item_ice/100)*(1+item_iva/100) else item_pvp1 end, ';
//   $sql_pvp.='item_pvp2= case when item_pvp2=0 or item_pvp2 is null then item_puni2*(1+item_ice/100)*(1+item_iva/100) else item_pvp2 end, ';
//   $sql_pvp.='item_pvp3= case when item_pvp3=0 or item_pvp3 is null then item_puni3*(1+item_ice/100)*(1+item_iva/100) else item_pvp3 end, ';
//   $sql_pvp.='item_pvp4= case when item_pvp4=0 or item_pvp4 is null then item_puni4*(1+item_ice/100)*(1+item_iva/100) else item_pvp4 end, ';
//   $sql_pvp.='item_pvp5= case when item_pvp5=0 or item_pvp5 is null then item_puni5*(1+item_ice/100)*(1+item_iva/100) else item_pvp5 end ';
//   $res_pvp=@msql_query($sql_pvp,$link_vcrx);
    
  $sql_dcto ='update item0 set ';
  $sql_dcto.='item_dcto2= case when ifnull(item_dcto2,0)=0 and item_puni2>0 then (1-item_puni2/item_puni1)*100 else item_dcto2 end, ';
  $sql_dcto.='item_dcto3= case when ifnull(item_dcto3,0)=0 and item_puni3>0 then (1-item_puni3/item_puni1)*100 else item_dcto3 end, ';
  $sql_dcto.='item_dcto4= case when ifnull(item_dcto4,0)=0 and item_puni4>0 then (1-item_puni4/item_puni1)*100 else item_dcto4 end, ';
  $sql_dcto.='item_dcto5= case when ifnull(item_dcto5,0)=0 and item_puni5>0 then (1-item_puni5/item_puni1)*100 else item_dcto5 end  ';
  $res_dcto=@msql_query($sql_dcto,$link_vcrx);
  
//   $sql_dcto='update item0 set item_dcto2=(1-item_puni2/item_puni1)*100 where item_puni2>0 and (item_dcto2=0 or item_dcto2 is null)';
//   $res_dcto=@msql_query($sql_dcto,$link_vcrx);
  
//   $sql_dcto='update item0 set item_dcto3=(1-item_puni3/item_puni1)*100 where item_puni3>0 and (item_dcto3=0 or item_dcto3 is null)';
//   $res_dcto=@msql_query($sql_dcto,$link_vcrx);
  
//   $sql_dcto='update item0 set item_dcto4=(1-item_puni4/item_puni1)*100 where item_puni4>0 and (item_dcto4=0 or item_dcto4 is null)';
//   $res_dcto=@msql_query($sql_dcto,$link_vcrx);
  
//   $sql_dcto='update item0 set item_dcto5=(1-item_puni5/item_puni1)*100 where item_puni5>0 and (item_dcto5=0 or item_dcto5 is null)';
//   $res_dcto=@msql_query($sql_dcto,$link_vcrx);  
  
}
  
   
  
function pvta_item($item,$fecha,$numfac,$tipodoc='')
{
  if(empty($item)){return;}
  include('../_cfg/config.php');
  
  if(empty($fecha)){date('Y-m-d');}

  	$fecha1=date('Y-m-d',strtotime($fecha.' -30 days')); 
  	$fecha2=date('Y-m-d',strtotime($fecha.' +30 days')); 

     
     
  $sql_vtas ='select ';  
  $sql_vtas.='abs(DATEDIFF("'.$fecha.'",fecha)) as diasdif,';
  $sql_vtas.=' vtas12.* from vtas12 ';  
  $sql_vtas.=' where item like "'.$item.'%" ';  
  $sql_vtas.=' and status!="O" ';
  
     
  if($tipodoc!='')
  {$sql_vtas.=' and (docu like "'.$tipodoc.'%")';}
  else
  {$sql_vtas.=' and (docu like "FC%" or docu like "NC%")';}
  if($numfac!='')
  {$sql_vtas.=' and numero ="'.$numfac.'" ';}
  $sql_vtas.=' order by diasdif, fecha desc, item ';
  $sql_vtas.=' limit 1 ';
  $res_vtas=msql_query($sql_vtas,$link_vcrx);

//      echo '<msgalert>';
//      if($numfac!=''){}
//      else{echo '  ==>'.'*******'.$item.'-'.$fecha;}
//      echo '</msgalert>';

     
if($item=='2020100340')
{
     
//        echo '<msgalert>';
//        echo '<br>*/*/*/*/==='.$sql_vtas;
//        echo '</msgalert>';
     
}
  $puni=@msql_result($res_vtas,0,'puni');
  $dcto=@msql_result($res_vtas,0,'dcto');
  $iva=@msql_result($res_vtas,0,'iva');
  
  $pvta=$puni;
  $pvta=$puni*(1-$dcto/100);

  if($pvta==0)
  {
       $sql_vtas ='select ';  
       $sql_vtas.='abs(DATEDIFF("'.$fecha.'",fecha)) as diasdif,';
       $sql_vtas.=' vtas12.* from vtas12 ';  
    $sql_vtas.=' where item like "'.$item.'%" ';  
    $sql_vtas.=' and status!="O" ';
//     $sql_vtas.=' and docu like "FC%" ';
    $sql_vtas.=' and (docu like "FC%" or docu like "NC%")';
    {$sql_vtas.=' and fecha<="'.$fecha2.'" ';}
    $sql_vtas.=' order by fecha desc, item ';
    $sql_vtas.=' limit 1 ';
    $res_vtas=msql_query($sql_vtas,$link_vcrx);
     
//        echo '<msgalert>';
//        echo '<br>*/*/*/*/'.$sql_vtas;
//        echo '</msgalert>';
    $puni=@msql_result($res_vtas,0,'puni');
    $iva=@msql_result($res_vtas,0,'iva');

    $pvta=$puni*(1+$iva/100);
    $pvta=$puni;
  }
  
//   echo ' <br>*/*/*'.$item.'-'.$pvta;
  
    
  return $pvta;
    
}
  
  
  
function updt_vtascuni($docu,$idnum)
{
  include('../_cfg/config.php');
  if(!mb_stristr('FC.NC',substr($docu,0,2))){return;}
  $sql_vtas='select * from vtas1 left join vtas2 on idnum1=idnum2 where idnum2='.$idnum;
  $res_vtas=msql_query($sql_vtas,$link_vcrx);
  
//   echo '=========';   
     
  while($aso_vtas=msql_fetch_assoc($res_vtas))
  {
    $fecha=$aso_vtas['fecha'];
    $item=$aso_vtas['item'];
    $costo=cost_prom($item,$fecha);
// echo '<msgalert>';         
// echo '==='.$item.'-'.$costo;         
// echo '</msgalert>';         
       
    $sql_updt='update vtas2 set cuni='.$costo.' where idnum2='.$idnum.' and item="'.$item.'"';
    $res_updt=msql_query($sql_updt,$link_vcrx);
  }
  
}

   
  
function updt_vtaslote($docu,$idnum)
{
     include('../_cfg/config.php');
     if(!mb_stristr('FC.NC',substr($docu,0,2))){return;}

     $sql_lote='select * from empr0 ';
     $res_lote=msql_query($sql_lote,$link_vcrx);
     $empr_lote=@msql_result($res_lote,0,'empr_lote');  

     if(empty($empr_lote)){return;}

     $sql_vtas='select * from vtas1 left join vtas2 on idnum1=idnum2 where idnum1='.$idnum;
     $res_vtas=msql_query($sql_vtas,$link_vcrx);

     while($aso_vtas=msql_fetch_assoc($res_vtas))
     {
          $numfac=$aso_vtas['numero'];
          $fecha=$aso_vtas['fecha'];
          $item=$aso_vtas['item'];
          
          $idfrom=$aso_vtas['idfrom'];
          $mdfrom=$aso_vtas['mdfrom'];
          
          $lote='';
          $sql_inve ='select * from inve1 left join inve2 on idnum1=idnum2 ';
          $sql_inve.='where status!="O" and (docu="EB10" or docu="IB12") and item="'.$item.'" and numfac="'.$numfac.'"';
          $res_inve=msql_query($sql_inve,$link_vcrx);
//           $lote=msql_result($res_inve,0,'lote');
          while($aso_inve=msql_fetch_assoc($res_inve))
          {
               $xlote=$aso_inve['lote'];
               if(!empty($xlote)){$lote.=' /'.$xlote;}
          }
          $lote=trim($lote);

          $sql_updt='update vtas2 set lote="'.$lote.'" where idnum2='.$idnum.' and item="'.$item.'"';
          $res_updt=msql_query($sql_updt,$link_vcrx);
          
          if($mdfrom=='clie')
          {
               $sql_updt='update clie2 set lote="'.$lote.'" where idnum2='.$idfrom.' and item="'.$item.'"';
               $res_updt=msql_query($sql_updt,$link_vcrx);
          }
          
          
     }
  
}
  
  
   
  
function get_idvtas($docu,$idnum)
{
//   if(!mb_stristr('EB',substr($docu,0,2))){return;}
  if(!mb_stristr('EB',substr($docu,0,2)) and $docu!='IB12'){return;}
     
  if($idnum==""){return;}
  include('../_cfg/config.php');

  $idvtas='';
  $tabla='inve';
  $sql_inve='select * from '.$tabla.'1 where docu="'.$docu.'" and idnum1="'.$idnum.'" and status not in("O","X")';
  $res_inve=msql_query($sql_inve,$link_vcrx);
  if(msql_num_rows($res_inve)!=0)
  {
    $ifecha=msql_result($res_inve,0,'fecha');
    $ienti=msql_result($res_inve,0,'enti');
    $iptoe=msql_result($res_inve,0,'ptoe');
    $inumero=msql_result($res_inve,0,'numero');
    $inumfac=@msql_result($res_inve,0,'numfac');
    
    $idvtas='';

  	$fecha1=date('Y-m-d',strtotime($ifecha.' -30 days')); 
  	$fecha2=date('Y-m-d',strtotime($ifecha.' +30 days')); 
    
    $docu2='FC10';
       if(substr($docu,0,2)=='IB')
       {$docu2='NC10';}
    $tabla='vtas';
    
    $sql_vtas ='select * from '.$tabla.'1 ';
    $sql_vtas.=' where docu="'.$docu2.'"';
    $sql_vtas.=' and status not in("O","X")';
    $sql_vtas.=' and numero="'.$inumfac.'"';
    $sql_vtas.=' order by fecha desc';
    $res_vtas=msql_query($sql_vtas,$link_vcrx);
    if(msql_num_rows($res_vtas)!=0){$idvtas=msql_result($res_vtas,0,'idnum1');}
    else
    {
      $sql_vtas ='select * from '.$tabla.'1 ';
      $sql_vtas.=' where docu="'.$docu2.'" ';
      $sql_vtas.=' and status not in("O","X") ';
      $sql_vtas.=' and idfrom="'.$idnum.'"';
      $sql_vtas.=' and lower(mdfrom)="inve" ';
      $res_vtas=msql_query($sql_vtas,$link_vcrx);
      if(msql_num_rows($res_vtas)!=0){$idvtas=msql_result($res_vtas,0,'idnum1');}
      else
      {
        $sql_vtas ='select * from '.$tabla.'12 ';
        $sql_vtas.=' where docu="'.$docu2.'"';
        $sql_vtas.=' and status not in("O","X")';
        $sql_vtas.=' and enti like "'.substr($ienti,0,10).'%" ';
        $sql_vtas.=' and fecha>="'.$fecha1.'"';
        $sql_vtas.=' and fecha<="'.$fecha2.'"';
        $sql_vtas.=' and item in(select item from inve2 where idnum2="'.$idnum.'")';
        $sql_vtas.=' order by fecha desc';
        $res_vtas=msql_query($sql_vtas,$link_vcrx);
        if(msql_num_rows($res_vtas)!=0){$idvtas=msql_result($res_vtas,0,'idnum1');}
           else
           {
             $sql_vtas ='select * from '.$tabla.'1 ';
             $sql_vtas.=' where docu="'.$docu2.'"';
             $sql_vtas.=' and status not in("O","X")';
             $sql_vtas.=' and enti like "'.substr($ienti,0,10).'%" ';
             $sql_vtas.=' and fecha>="'.$fecha1.'"';
             $sql_vtas.=' and fecha<="'.$fecha2.'"';
             $sql_vtas.=' order by fecha desc';
             $res_vtas=msql_query($sql_vtas,$link_vcrx);
             if(msql_num_rows($res_vtas)!=0){$idvtas=msql_result($res_vtas,0,'idnum1');}
           }
      }
      
    }
    
    
    
//     echo '*/*/*/*/'.$idvtas;
    return $idvtas;
    
  }
  
  
}
  

function vtas_ingr_difer($docu,$idnum)
{
  include('../_cfg/config.php');
  $xingdif=0;
  if(substr($docu,0,2)=='NC')
  {
    $sql_fact='select * from vtas1 where idnum1='.$idnum.'';
    $res_fact=msql_query($sql_fact,$link_vcrx);
    
    $todocu=msql_result($res_fact,0,'todocu');
    $toptoe=msql_result($res_fact,0,'toptoe');
    $tonume=msql_result($res_fact,0,'tonume');
    
    $sql_fact='select * from vtas1 where docu="'.$todocu.'" and  ptoe="'.$toptoe.'" and  numero="'.$tonume.'"';
    $res_fact=msql_query($sql_fact,$link_vcrx);
    $docu=msql_result($res_fact,0,'docu');
    $idnum=msql_result($res_fact,0,'idnum1');

  }
    
  if(!mb_stristr('FC.NC',substr($docu,0,2))){return 0;}
  
  $sql_fact='select *,sum(cant) as tcantfc from vtas12 where idnum1="'.$idnum.'"';
  $sql_fact.=' and item in(select distinct item from inve2)';
  $res_fact=msql_query($sql_fact,$link_vcrx);
  $docuv=msql_result($res_fact,0,'docu');
  $ptoev=msql_result($res_fact,0,'ptoe');
  $numev=msql_result($res_fact,0,'numero');
  $fechav=msql_result($res_fact,0,'fecha');
  $tcantfc=msql_result($res_fact,0,'tcantfc');
  
//   $fecha1=date('Y-01-01');
  $fecha1=date('Y-01-01', strtotime($fechav));
  $fecha2=date('Y-m-t', strtotime($fechav));

  $sql_fact='select *,sum(cant) as tcantnc from vtas12 where todocu="'.$docu.'" and  toptoe="'.$ptoev.'" and  tonume="'.$numev.'" and fecha between "'.$fecha1.'" and "'.$fecha2.'" and status not in("O","X")';
  $res_fact=msql_query($sql_fact,$link_vcrx);
  $tcantnc=@msql_result($res_fact,0,'tcantnc');
  
  $sql_inve='select *,sum(cant) as tcanteb from inve12 where numfac="'.$numev.'" and fecha between "'.$fecha1.'" and "'.$fecha2.'" and status not in("O","X")';
  $res_inve=msql_query($sql_inve,$link_vcrx);
  $tcanteb=@msql_result($res_inve,0,'tcanteb');
//   explain_sql($sql_inve,1);
  
  $tdifer=$tcantfc-$tcantnc-$tcanteb;
  
  if($tdifer>0){$xingdif=1;}
   
//    echo '<br>*/*/*/FC '.$tcantfc;
//    echo '<br>*/*/*/NC '.$tcantnc;
//    echo '<br>*/*/*/EB '.$tcanteb;
//    echo '<br>*/*/*/DF '.$tdifer;
//    echo '<br>*/*/*/R: '.$xingdif;

  
  return $xingdif;
}

     
  
}

?>