<?php //session_start();?>
<?php

// 
{

  function set_mrca()
  {
		include('../_cfg/config.php');
    if(!mb_stristr('orel.saar',$cfg_empr)){return;}
    
    $sql_mrca='select * from mrca0 ';
    $res_mrca=msql_query($sql_mrca,$link_vcrx);
    
    msql_data_seek($res_mrca,0);
    while($aso_mrca=msql_fetch_assoc($res_mrca))
    {
      $mrca_codigo=trim($aso_mrca['mrca_codigo']);
      $mrca_descri=trim($aso_mrca['mrca_descri']);
      if(strlen($mrca_descri)>=6)
      {
        $sql_updt ='update item0 set item_mrca="'.$mrca_codigo.'" ';
        $sql_updt.='where item_descri like "%'.$mrca_descri.'%" and (item_mrca="" or item_mrca is null)';
        $res_updt=msql_query($sql_updt,$link_vcrx);
      }
    }

    
//    $res_mrca=msql_query($sql_mrca,$link_vcrx);
     msql_data_seek($res_mrca,0);
    while($aso_mrca=msql_fetch_assoc($res_mrca))
    {
      $mrca_codigo=trim($aso_mrca['mrca_codigo']);
      $mrca_descri=trim($aso_mrca['mrca_descri']);
      if(strlen($mrca_descri)>=3)
      {
        $sql_updt ='update item0 set item_mrca="'.$mrca_codigo.'" ';
        $sql_updt.='where item_descri like "%'.$mrca_descri.'%" and (item_mrca="" or item_mrca is null)';
        $res_updt=msql_query($sql_updt,$link_vcrx);
      }
    }
    
     msql_data_seek($res_mrca,0);
    while($aso_mrca=msql_fetch_assoc($res_mrca))
    {
      $mrca_codigo=trim($aso_mrca['mrca_codigo']);
      $mrca_descri=trim($aso_mrca['mrca_descri']);
      if(strlen($mrca_descri)==2)
      {
        $sql_updt ='update item0 set item_mrca="'.$mrca_codigo.'" ';
        $sql_updt.='where item_descri like "% '.$mrca_descri.'%" and (item_mrca="" or item_mrca is null)';
        $res_updt=msql_query($sql_updt,$link_vcrx);
      }
    }
    
    $sql_updt='update mrca0 set mrca_status="I"';
    $res_updt=msql_query($sql_updt,$link_vcrx);
    
    $sql_updt='update mrca0 set mrca_status="-" where mrca_codigo in(select item_mrca from item0)';
    $res_updt=msql_query($sql_updt,$link_vcrx);
    
    
    
  }
  
  function set_lsuborel()
  {
     include('../_cfg/config.php');
     if($cfg_empr!='orel'){return;}
       
     $sql_lsub='select count(lsub_descri) as ccc, lsub_descri from lsub0 where lsub_status!="X" group by lsub_descri order by ccc desc';
     $res_lsub=msql_query($sql_lsub,$link_vcrx);
     $ccc=msql_result($res_lsub,0,'ccc');

     if($ccc>1)
     {
          $sql_updt='update item0 set item_lsub=trim(item_lsub)';
          $res_updt=msql_query($sql_updt,$link_vcrx);


          $sql_updt ='update item0 set item_lsub=case ';
          $sql_updt.='when item_lsub like "UNIV%" then "UNIV-" ';
          $sql_updt.='when item_lsub like "COM%" then "COMP-" ';
          $sql_updt.='when item_lsub like "ROB%" then "ROBO-" ';
          $sql_updt.='when item_lsub like "REP%" then "REPU-" ';
          $sql_updt.='when item_lsub like "FER%" then "FERR-" ';
          $sql_updt.='when item_lsub like "ELECTRO%" then "ELCA-" ';
          $sql_updt.='when item_lsub = "ELE" then "ELCA-" ';
          $sql_updt.='when item_lsub = "ELI" then "ELCO-" ';
          $sql_updt.='else item_lsub ';
          $sql_updt.='end ';
          $res_updt=msql_query($sql_updt,$link_vcrx);

          $sql_updt='insert into lsub0(lsub_codigo,lsub_descri,lsub_status)value("FERR-","FERRETERIA","-")';
          $res_updt=msql_query($sql_updt,$link_vcrx);

          $sql_updt='insert into lsub0(lsub_codigo,lsub_descri,lsub_status)value("COMP-","COMPUTACION","-")';
          $res_updt=msql_query($sql_updt,$link_vcrx);

          $sql_updt='insert into lsub0(lsub_codigo,lsub_descri,lsub_status)value("ELCA-","ELECTRONICA","-")';
          $res_updt=msql_query($sql_updt,$link_vcrx);

          $sql_updt='insert into lsub0(lsub_codigo,lsub_descri,lsub_status)value("ELCO-","ELECTRICO","-")';
          $res_updt=msql_query($sql_updt,$link_vcrx);

          $sql_updt='insert into lsub0(lsub_codigo,lsub_descri,lsub_status)value("FERR-","COMPUTACION","-")';
          $res_updt=msql_query($sql_updt,$link_vcrx);

          $sql_updt='insert into lsub0(lsub_codigo,lsub_descri,lsub_status)value("REPU-","REPUESTOS","-")';
          $res_updt=msql_query($sql_updt,$link_vcrx);

          $sql_updt='insert into lsub0(lsub_codigo,lsub_descri,lsub_status)value("UNIV-","UNIVERSAL","-")';
          $res_updt=msql_query($sql_updt,$link_vcrx);

          $sql_updt='insert into lsub0(lsub_codigo,lsub_descri,lsub_status)value("ROBO-","ROBOTICA","-")';
          $res_updt=msql_query($sql_updt,$link_vcrx);

          $sql_updt='update lsub0 set lsub_status="X"';
          $res_updt=msql_query($sql_updt,$link_vcrx);

          $sql_updt='update lsub0 set lsub_status="-" where lsub_codigo in(select distinct item_lsub from item0)';
          $res_updt=msql_query($sql_updt,$link_vcrx);

          //     set_marca()
     }
  }
  
  function gmny_op10($docu,$idnum)
  {
    return;
		include('../_cfg/config.php');
    if($docu!='OP10'){return;}
    if(!mb_stristr('gmny',$cfg_empr)){return;}
    
    $tabla='prod';
    
    $sql_op10 ='select distinct(substring(item,1,8)) as item,cant,puni,fecha ';
    $sql_op10.='from '.$tabla.'12 ';
    $sql_op10.='where idnum1="'.$idnum.'"';
    $sql_op10.=' and cant*puni>0 ';
    $res_op10=msql_query($sql_op10,$link_vcrx);
  
//     explain_sql($sql_op10,1);
    
    while($aso_op10=msql_fetch_assoc($res_op10))
    {
      $xitem=trim($aso_op10['item']);
      $xcant=($aso_op10['cant']);
      $xpuni=($aso_op10['puni']);
      
//       echo '<br>***'.$xitem; 
//       echo '<br>***'.$xpuni; 
        
      if(strlen($xitem)==8)
      {
        $sql_item ='select * from item0 where item_codigo like "'.$xitem.'%" ';
        $sql_item.='and length(trim(item_codigo))>8 ';
        $sql_item.='and item_status not in ("O","X","I") ';
        $sql_item.='order by item_codigo';
        $res_item=msql_query($sql_item,$link_vcrx);

        while($aso_item=msql_fetch_assoc($res_item))
        {
          $xitem2=trim($aso_item['item_codigo']);
            
          $sql_item2='select * from '.$tabla.'2 where item ="'.$xitem2.'" and idnum2="'.$idnum.'" ';
          $res_item2=msql_query($sql_item2,$link_vcrx);
          $xiddet=@msql_result($res_item2,0,'iddet')*1;
          
          
          if($xiddet==0)
          {
            $xiddet=get_iddet($docu,$idnum);
            $sql_inse='insert into '.$tabla.'2(idnum2,iddet,item,cant,puni,idate2)';
            $sql_inse.='value("'.$idnum.'","'.$xiddet.'","'.$xitem2.'","'.$xcant.'","'.$xpuni.'",now())';
            $res_inse=msql_query($sql_inse,$link_vcrx);
          }
          
//           $sql_updt ='update '.$tabla.'2 set cant="'.$xcant.'" where idnum2="'.$idnum.'" and iddet="'.$xiddet.'"';
//           $sql_updt.=' and cant=0 ';
//           $res_updt=msql_query($sql_updt,$link_vcrx);

//           $sql_updt ='update '.$tabla.'2 set puni="'.$xpuni.'" where idnum2="'.$idnum.'" and iddet="'.$xiddet.'"';
//           $sql_updt.=' and puni=0 ';
//           $res_updt=msql_query($sql_updt,$link_vcrx);

          if(substr($xitem2,10,2)=='00')
          {
            $sql_item00='select * from item0 where item_codigo like "'.substr($xitem2,0,10).'%"  ';
            $res_item00=msql_query($sql_item00,$link_vcrx);
            if(msql_num_rows($res_item00)>1)
            {
              $sql_dele='delete from '.$tabla.'2 where idnum2="'.$idnum.'" and item="'.$xitem2.'"';
              $res_dele=msql_query($sql_dele,$link_vcrx);
//               echo '<br>****'.$sql_dele;
            }
          }
          
          
        }
        
      }
    }
    
    
  }
  
  
  function gmny_refa($penti,$pfecha='')
  {
		include('../_cfg/config.php');
    
    if($pfecha==''){$pfecha=date('Ymd');}
    
    $xfecha=date('Y-m-d',strtotime($pfecha.' -30 days'));

    $trefa=0;
    $sql_refa='select * from user0 where user_referidox="'.$penti.'" or user_referidox like "'.substr($penti,0,10).'%" order by user_idate desc';
    $res_refa=msql_query($sql_refa,$link_vcrx);
    
    while($aso_refa=@msql_fetch_assoc($res_refa))
    { 
      $xenti=substr(trim($aso_refa['user_cedula']),0,10);
      $sql_vtas ='select * from vtas1 where enti like "'.$xenti.'%"';
      $sql_vtas.=' and status!="O"';
      $sql_vtas.=' and status!="X"';
      $sql_vtas.=' and fecha>="'.$xfecha.'"';
      $sql_vtas.=' limit 1';
      $res_vtas=msql_query($sql_vtas,$link_vcrx);
      
      if(msql_num_rows($res_vtas)!=0){$trefa=$trefa+1;}
    }
    
    return $trefa;
  }
  
  
  function tfle_plan($docu,$idnum,$asub,$tipo='I')
  {
    //$tipo = Nuevo Detalle Xeliminar
		include('../_cfg/config.php');
    
    
    if(empty($idnum)){return;}
//     $sql_asub='select * from asub0 where asub_codigo like "'.substr($asub,0,4).'%" order by asub_codigo';
//     $res_asub=msql_query($sql_asub,$link_vcrx);
    
// echo msql_error($link_vcrx);    
    if($tipo=='I' and substr($asub,5,4)=='0000')
    {
      $sql_asub='select * from asub0 where asub_codigo like "'.substr($asub,0,4).'%" order by asub_codigo';
      $res_asub=msql_query($sql_asub,$link_vcrx);
      
      while($aso_asub=@msql_fetch_assoc($res_asub))
      { 
  //     echo '*/*/*wwww';
        $xasub=trim($aso_asub['asub_codigo']);
        $sql_plan2='select * from plan2 where idnum2="'.$idnum.'" and asub="'.$xasub.'"';
        $res_plan2=msql_query($sql_plan2,$link_vcrx);
        if(msql_num_rows($res_plan2)==0)
        {
          $iddet=get_iddet($docu,$idnum);
          $sql_inse2='insert into plan2(idnum2,iddet,asub) value("'.$idnum.'","'.$iddet.'","'.$xasub.'")';
          $res_inse2=msql_query($sql_inse2,$link_vcrx);

          $sql_updt='update plan2 set cant=1 where cant is null and idnum2="'.$idnum.'"';
          $res_updt=msql_query($sql_updt,$link_vcrx);
        }
      }
    }

    
    if($tipo=='I' and substr($asub,5,4)!='0000')
    {
        $sql_plan2='select * from plan2 where idnum2="'.$idnum.'" and asub like "'.substr($asub,0,4).'%"';
        $res_plan2=msql_query($sql_plan2,$link_vcrx);
        if(msql_num_rows($res_plan2)!=0)
        {
          $iddet=get_iddet($docu,$idnum);
          $sql_inse2='insert into plan2(idnum2,iddet,asub) value("'.$idnum.'","'.$iddet.'","'.$asub.'")';
          $res_inse2=msql_query($sql_inse2,$link_vcrx);

          $sql_updt='update plan2 set cant=1 where cant is null and idnum2="'.$idnum.'"';
          $res_updt=msql_query($sql_updt,$link_vcrx);
        }

    }
    
    
    if($tipo=='X' and substr($asub,5,4)=='0000')
    {
        $sql_dele2='delete from plan2 where idnum2="'.$idnum.'" and asub like "'.substr($asub,0,4).'%"';
        $res_dele2=msql_query($sql_dele2,$link_vcrx);
    }
    
    if($tipo=='X' and substr($asub,5,4)!='0000')
    {
        $sql_dele2='delete from plan2 where idnum2="'.$idnum.'" and asub = "'.$asub.'"';
        $res_dele2=msql_query($sql_dele2,$link_vcrx);
    }
    
    
  }
  
  
  function gen_atvdjw($docu,$fecha,$concepto)
  {
//     $sql_atvd='insert into atvd'
  }
  
  function mflw_xls()
  {
    
  }  
  

  function gnya_niveles($maes,$pcodigo,$phijo='',$pnivel=1)
  {
    
		include('../_cfg/config.php');
    $referx0=$pcodigo;
    if($pcodigo==''){return;}
    if($phijo==''){$phijo=$pcodigo;}

    $sql_updt='update user0 set user_referx'.$pnivel.'='.$pcodigo.' where user_referidox="'.$phijo.'"';
    $res_updt=msql_query($sql_updt,$link_vcrx);
//     echo '*/*/'.$sql_updt;
    if($pnivel==5){return;}

    $sql_nivel1='select * from '.$maes.'0 where '.$maes.'_referidox="'.$phijo.'"';
    $res_nivel1=msql_query($sql_nivel1,$link_vcrx);
    if(msql_num_rows($res_nivel1)==0){return;}
    
    while($aso_nivel1=@msql_fetch_assoc($res_nivel1))
    { 
      $referx1=trim($aso_nivel1[$maes.'_codigo']);
      gnya_niveles($maes,$pcodigo,$referx1,$pnivel+1);
      
//       $sql_updt='update user0 set user_referx2='.$referx0.' where user_referidox="'.$referx1.'"';
//       $res_updt=msql_query($sql_updt,$link_vcrx);
//       $sql_nivel2='select * from '.$maes.'0 where '.$maes.'_referidox="'.$referx1.'"';
//       $res_nivel2=msql_query($sql_nivel2,$link_vcrx);
//       while($aso_nivel2=@msql_fetch_assoc($res_nivel2)
//       { 
//         $referx2=trim($aso_nivel2[$maes.'_codigo']);
//         $sql_updt='update user0 set user_referx3='.$referx0.' where user_referidox="'.$referx2.'"';
//         $res_updt=msql_query($sql_updt,$link_vcrx);
//         $sql_nivel3='select * from '.$maes.'0 where '.$maes.'_referidox="'.$referx2.'"';
//         $res_nivel3=msql_query($sql_nivel3,$link_vcrx);
//         while($aso_nivel3=@msql_fetch_assoc($res_nivel3)
//         { 
//           $referx3=trim($aso_nivel3[$maes.'_codigo']);
//           $sql_updt='update user0 set user_referx4='.$referx0.' where user_referidox="'.$referx3.'"';
//           $res_updt=msql_query($sql_updt,$link_vcrx);
//           $sql_nivel4='select * from '.$maes.'0 where '.$maes.'_referidox="'.$referx3.'"';
//           $res_nivel4=msql_query($sql_nivel4,$link_vcrx);
//           while($aso_nivel4=@msql_fetch_assoc($res_nivel4)
//           { 
//             $referx4=trim($aso_nivel4[$maes.'_codigo']);
//             $sql_updt='update user0 set user_referx5='.$referx0.' where user_referidox="'.$referx4.'"';
//             $res_updt=msql_query($sql_updt,$link_vcrx);
//           }
//         }
//       }
    }
  }    
  
  function gnya_comi0($docu,$idnum)
  {
		include('../_cfg/config.php');
    

//     echo '*/*/*/*'.$_SESSION['user_vcrx_empr'].$cfg_empr;
		if($cfg_empr!='gnya' and $_SESSION['user_vcrx_empr']!='gnya'){return;}
    if(!mb_stristr('FC',substr($docu,0,2))){return;}
    
    $tabla=r_fiel('docu',$docu,'tabla');

    $sql_orig='select * from '.$tabla.'1 where idnum1='.$idnum.'';
    $res_orig=msql_query($sql_orig,$link_vcrx);
    
    $xenti=msql_result($res_orig,0,'enti');
    $xptoe=msql_result($res_orig,0,'ptoe');
    $xnumero=msql_result($res_orig,0,'numero');
    $xfecha=msql_result($res_orig,0,'fecha');
    
    $semaini=sema_ini();
    
    if($xfecha<$semaini){return;}
    
    $sql_refer='select * from user0 where user_cedula like "'.substr($xenti,0,10).'%" order by user_codigo desc ';
    $res_refer=msql_query($sql_refer,$link_vcrx);
    $referx0=msql_result($res_refer,0,'user_codigo');
    
    $referx1=r_fiel('user',$referx0,'referidox');
    gnya_comi1($docu,$idnum,$referx1,1);
    if($referx1==565){return;}
    
    $referx2=r_fiel('user',$referx1,'referidox');
    gnya_comi1($docu,$idnum,$referx2,2);
    if($referx2==565){return;}
    
    $referx3=r_fiel('user',$referx2,'referidox');
    gnya_comi1($docu,$idnum,$referx3,3);
    if($referx3==565){return;}
    
//     $referx4=r_fiel('user',$referx3,'referidox');
//     gnya_comi1($docu,$idnum,$referx4,4);
//     if($referx4==565){return;}
    
//     $referx5=r_fiel('user',$referx4,'referidox');
//     gnya_comi1($docu,$idnum,$referx5,5);
//     if($referx5==565){return;}
    
    
  }    
  
  
  function gnya_comi1($docu,$idnum,$referx,$nivel)
  {
		include('../_cfg/config.php');
    $newdocu='CM10';
    $newptoe='001'.'00'.$nivel;
    $newenti=r_fiel('user',$referx,'cedula');

//        echo '*/*/*/';
    if($newenti==''){return;}

//     echo 'AAA';
    $sql_orig ='select * from vtas12 where idnum1="'.$idnum.'" ';
    $res_orig=msql_query($sql_orig,$link_vcrx);
    $odocu=msql_result($res_orig,0,'docu');
    $optoe=msql_result($res_orig,0,'ptoe');
    $onumero=msql_result($res_orig,0,'numero');
    $oenti=msql_result($res_orig,0,'enti_descri');
    $ofecha2=msql_result($res_orig,0,'fecha');
    $ostatus=msql_result($res_orig,0,'status');
    
    $ofecha1=date('Y-m-d',strtotime($ofecha2.' -12 month'));
    
    //validar si referido cumple requisitos
    $sql_vtas ='select * from vtas12';
    $sql_vtas.=' where status!="O"';
    $sql_vtas.=' and fecha>="'.$ofecha1.'"';
    $sql_vtas.=' and fecha<="'.$ofecha2.'"';
    $sql_vtas.=' and enti like "'.substr($newenti,0,10).'%"';
    $sql_vtas.=' and item_descri like "KIT%"';
    $sql_vtas.=' limit 10';
    $res_vtas =msql_query($sql_vtas,$link_vcrx);
//     echo $sql_vtas;
    if(msql_num_rows($res_vtas)==0){ echo ''; return;}
    
    
    
    $sql_comi ='select * from comi1';
//     $sql_comi.=' where mdfrom="'.$tabla.'"';
    $sql_comi.=' where mdfrom="VTAS"';
    $sql_comi.=' and idfrom="'.$idnum.'"';
    $sql_comi.=' and ptoe like "'.$newptoe.'"';
    $sql_comi.=' and docu like "'.$newdocu.'"';
    $sql_comi.=' and enti like "'.substr($newenti,0,10).'%"';
    $sql_comi.=' and status!="X" ';
    $sql_comi.=' order by idnum1 desc ';
    $res_comi =msql_query($sql_comi,$link_vcrx);
    
    if(msql_num_rows($res_comi)==0)
    {
    	$newnumero=nume_docu($newdocu,$newptoe);
      $newidnum=clon_docu($docu,$idnum,$newdocu);
    }
    else
    {
      $newidnum=msql_result($res_comi,0,'idnum1');
      $newnumero=msql_result($res_comi,0,'numero');
      $newptoe=msql_result($res_comi,0,'ptoe');
    }
    
    
    $sql_comi ='update comi1 set ';
    $sql_comi.=' ptoe="'.$newptoe.'",';
    $sql_comi.=' numero="'.$newnumero.'",';
    $sql_comi.=' enti="'.$newenti.'",';
    $sql_comi.=' concepto="'.$odocu.'-'.$optoe.'-'.$onumero.' /'.$oenti.'",';
    $sql_comi.=' status="'.$ostatus.'" ';
    $sql_comi.=' where idnum1='.$newidnum.'';
    $res_comi=msql_query($sql_comi,$link_vcrx);
    
    $sql_comi ='update comi2 set ';
    $sql_comi.=' docum="'.$docu.'",';
    $sql_comi.=' ptoem="'.$optoe.'",';
//     $sql_comi.=' numer="'.$xnumero.'",';
    $sql_comi.=' numer="'.$onumero.'",';
    $sql_comi.=' detalle=detalle ';
    $sql_comi.=' where idnum2='.$newidnum.'';
    $res_comi=msql_query($sql_comi,$link_vcrx);
    
//     echo '<br>'.msql_error($link_vcrx).'<br>'.$sql_comi;

    
    
      $sql_comi ='update comi2,vtas2 set ';
      $sql_comi.=' comi2.puni=vtas2.puni,';
      $sql_comi.=' comi2.dcto=vtas2.dcto,';
      $sql_comi.=' comi2.detalle=vtas2.detalle ';
      $sql_comi.=' where comi2.idnum2='.$newidnum.'';
      $sql_comi.=' and vtas2.idnum2='.$idnum.'';
      $sql_comi.=' and comi2.item=vtas2.item ';
      $res_comi=msql_query($sql_comi,$link_vcrx);
//     echo $sql_comi.msql_error($link_vcrx);
    
    
    $sql_comi ='update comi2, item0 set ';
    $sql_comi.=' cant=cant,';
//     $sql_comi.=' puni=0.5,';
    // cambio solicitado 29/jul/2019
    $sql_comi.=' puni=0.1,';
    $sql_comi.=' detalle=detalle ';
    $sql_comi.=' where idnum2='.$newidnum.'';
    $sql_comi.=' and item=item_codigo ';
    $sql_comi.=' and item_descri like "KIT%" ';
    $res_comi=msql_query($sql_comi,$link_vcrx);

    
    
    $porcomi=1;

    
    if($nivel==1){$porcomi=.25;}
    if($nivel==2){$porcomi=.04;}
    if($nivel==3){$porcomi=.05;}
//     if($nivel==4){$porcomi=.02;}
//     if($nivel==5){$porcomi=.003;}

//     if(date('Y-m-d')>='2019-07-16')
//     {
//       if($nivel==1){$porcomi=.12;}
//       if($nivel==2){$porcomi=.02;}
//       if($nivel==3){$porcomi=.03;}
//       if($nivel==4){$porcomi=.005;}
//       if($nivel==5){$porcomi=.001;}
//     }
    
//     if(date('Y-m-d')>='2020-05-18')
//     {
//       if($nivel==1){$porcomi=10;}
//       if($nivel==2){$porcomi=5;}
//       if($nivel==3){$porcomi=10;}
//     }
    
//     if(date('Y-m-d')>='2020-05-01')
//     {
//       if($nivel==1){$porcomi=.05;}
//       if($nivel==2){$porcomi=.05;}
//       if($nivel==3){$porcomi=.05;}
//     }
    
    if(date('Y-m-d')>='2021-06-01')
    {
      if($nivel==1){$porcomi=.06;}
      if($nivel==2){$porcomi=.03;}
      if($nivel==3){$porcomi=.01;}
    }
    
      $sql_comi ='update comi2, item0 set ';
      $sql_comi.=' cant=cant,';
      $sql_comi.=' puni=puni*(1-dcto/100)*'.$porcomi.',';
      $sql_comi.=' detalle=detalle ';
      $sql_comi.=' where idnum2='.$newidnum.'';
      $sql_comi.=' and item=item_codigo ';
      $sql_comi.=' and item_descri not like "KIT%" ';
      $res_comi=msql_query($sql_comi,$link_vcrx);
//     echo '<br>'.msql_error($link_vcrx).'<br>'.$sql_comi;
    
      $sql_comi ='update comi2, item0 set ';
      $sql_comi.=' cant=cant,';
      $sql_comi.=' puni=0,';
      $sql_comi.=' detalle=detalle ';
      $sql_comi.=' where idnum2='.$newidnum.'';
      $sql_comi.=' and item=item_codigo ';
      $sql_comi.=' and item_descri like "KIT%" ';
      $res_comi=msql_query($sql_comi,$link_vcrx);
//     echo '<br>'.msql_error($link_vcrx).'<br>'.$sql_comi;
    
    
    
      $sql_comi ='update comi2, item0 set ';
      $sql_comi.=' dcto=0,';
      $sql_comi.=' iva=0,';
      $sql_comi.=' ice=0,';
      $sql_comi.=' prop=0,';
      $sql_comi.=' ssid=0,';
      $sql_comi.=' detalle=detalle ';
      $sql_comi.=' where idnum2='.$newidnum.'';
      $sql_comi.=' and item=item_codigo ';
//       $sql_comi.=' and item_descri not like "KIT%" ';
      $res_comi=msql_query($sql_comi,$link_vcrx);
    
    
    $sql_comi ='delete from comi2 ';
    $sql_comi.=' where idnum2='.$newidnum.'';
    $sql_comi.=' and cant*puni<(1/100) ';
//     $res_comi=msql_query($sql_comi,$link_vcrx);
    
  }
  
   
  function gmny_calc1($docu,$idnum)
  {
		include('../_cfg/config.php');
    if(!mb_stristr('gmny',$cfg_empr)){return;}
    
		$tabla=r_fiel('docu',$docu,'tabla');
//     echo '***'.$docu.'+++'.$tabla;
    if(!mb_stristr('vtas.clie.carr',$tabla)){return;}
    
       
    $sql_docu='select * from '.$tabla.'1 where idnum1='.$idnum;
    $res_docu=msql_query($sql_docu,$link_vcrx);
    
    //ojo error
    $enti=msql_result($res_docu,0,'enti');
    $fecha=msql_result($res_docu,0,'fecha');
    
//     $enti_dcto=r_fiel('enti',$enti,'enti_dcto');
    
//     $sql_docu ='update '.$tabla.'2, item0 set dcto=0 ';
//     $sql_docu.=' where idnum2="'.$idnum.'" ';
//     $sql_docu.=' and item=item_codigo ';
//     $sql_docu.=' and (item_descri like "KIT%" or item_descri like "CATALO%" or item_descri like "ENVIO%")';
//     $res_docu=msql_query($sql_docu,$link_vcrx);
       
    if($cfg_empr=='gmny')
    {
      $sql_docu ='update '.$tabla.'2 set dcto=0 ';
      $sql_docu.=' where idnum2="'.$idnum.'" ';
      $res_docu=msql_query($sql_docu,$link_vcrx);
    }
       
       
    if($fecha>="2020-05-01" and 1==2)
    {
      $trefa=gmny_refa($enti,$fecha);
      $dcto=20;
      if($trefa==1){$dcto=25;}
      if($trefa==2){$dcto=30;}
      $sql_docu ='update '.$tabla.'2 set dcto="'.$dcto.'"';
      $sql_docu.=' where idnum2="'.$idnum.'" ';
      $res_docu =msql_query($sql_docu,$link_vcrx);
    }
    
    // 3/julio/2020
    if($fecha>="2020-07-01")
    {
      $dcto2=0;
      $sql_grup='select *, sum(cant) as stcant from '.$tabla.'12 ';
      $sql_grup.=' where idnum2='.$idnum.' ';
      $sql_grup.='group by substring(item,1,6)';
      $res_grup=msql_query($sql_grup,$link_vcrx);
      while($aso_grup=msql_fetch_assoc($res_grup))
      {
        if($aso_grup['stcant']>=4)
        {
          $dcto5=5;
    //       echo '<msgsitemas><br>'.$aso_grup['stcant'].'</msgsistemas>';
          $sql_docu ='update '.$tabla.'2 set dcto='.$dcto5.' ';
          $sql_docu.=' where idnum2="'.$idnum.'" ';
          $sql_docu.=' and item like "'.substr($aso_grup['item'],0,6).'%" ';
          $res_docu =msql_query($sql_docu,$link_vcrx);
        }
      }
    }
    
    // descuento cero (0) para kits catalogos o envios
    $sql_docu ='update '.$tabla.'2, item0 set dcto=0 ';
    $sql_docu.=' where idnum2="'.$idnum.'" ';
    $sql_docu.=' and item=item_codigo ';
    $sql_docu.=' and (item_descri like "KIT%" or item_descri like "CATALO%" or item_descri like "ENVIO%")';
    $res_docu =msql_query($sql_docu,$link_vcrx);
    
    return;
  }
  
  function gnya_calc1($docu,$idnum)
  {
		include('../_cfg/config.php');
    
		$tabla=r_fiel('docu',$docu,'tabla');
//     echo '***'.$docu.'+++'.$tabla;
    if(!mb_stristr('vtas.clie.carr',$tabla)){return;}
    if(!mb_stristr('gnya',$cfg_empr)){return;}
    
//     $sql_docu='select * from '.$tabla.'1 where idnum1='.$idnum;
//     $res_docu=msql_query($sql_docu,$link_vcrx);

    $sql_docu ='select ';
    $sql_docu.='*, sum(tsubt) as stsubt, sum(cant) as stcant ';
    $sql_docu.='from '.$tabla.'12 ';
    $sql_docu.='where idnum1="'.$idnum.'" ';
    $sql_docu.='and item_descri not like "%TRANS%" ';
    $sql_docu.='and item_descri not like "%ENVIO%" ';
    $sql_docu.='and item_descri not like "%LOGIS%" ';
    $res_docu=msql_query($sql_docu,$link_vcrx);

    $tsubt=msql_result($res_docu,0,'stsubt');
    $tcant=msql_result($res_docu,0,'stcant');
    
    
    //ojo error
    $enti=@msql_result($res_docu,0,'enti');
    $fecha=@msql_result($res_docu,0,'fecha');
    $dcto2=0;
    
// 	if($tsubt>=50){$dcto2=10;}
    
  // 13/julio /2020  
// 	if($tsubt>=50){$dcto2=25;}

       
//1 abr /2020
       $dcto2=0;
	if($tcant>=12){$dcto2=20;}

       
       
//1 may /2021
       $dcto2=0;
	if($tcant>=3){$dcto2=25;}
	if($tcant>=12){$dcto2=35;}
    
//28 jun /2021
       $dcto2=0;
	if($tcant>=3){$dcto2=25;}
	if($tcant>=6){$dcto2=35;}
	if($tcant>=12){$dcto2=40;}
    
       
       
//     $dcto2=20;
//     echo '<msgsistemas>*/*/*/'.$tcant.'</msgsistemas>';
        
        
	
				$sql_dcto2='update carr2 set dcto='.$dcto2.' where iva>0 and idnum2='.$idnum;
				$res_dcto2=msql_query($sql_dcto2,$link_vcrx);
    
//     echo '<msgsistemas>*/*/*/'.$sql_dcto2.'</msgsistemas>';

        $sql_dcto2='update carr2, item0 set dcto='.$dcto0.' where item=item_codigo and item_descri like "KIT%" and idnum2='.$idnum;
				$res_dcto2=msql_query($sql_dcto2,$link_vcrx);

        $sql_dcto2='update carr2, item0 set dcto='.$dcto0.' where item=item_codigo and item_descri like "CATALO%" and idnum2='.$idnum;
				$res_dcto2=msql_query($sql_dcto2,$link_vcrx);
// 				echo explain_sql($sql_dcto2);
				$res_tcarr=msql_query($sql_tcarr,$link_vcrx);
    
    return;
  }
  
  
  function dly1_calc1($docu,$idnum)
  {
		include('../_cfg/config.php');
    
		$tabla=r_fiel('docu',$docu,'tabla');
//     echo '***'.$docu.'+++'.$tabla;
    if(!mb_stristr('vtas.clie.carr',$tabla)){return;}
    
    $sql_docu='select * from '.$tabla.'1 where idnum1='.$idnum;
    $res_docu=msql_query($sql_docu,$link_vcrx);
    
    $enti=substr(msql_result($res_docu,0,'enti'),0,10);
    $drtr=substr(@msql_result($res_docu,0,'drtr'),0,10);
    $fecha=msql_result($res_docu,0,'fecha');
    
    $dcto2=30;
    $enti_dcto=r_fiel('enti',$enti,'enti_dcto');
    $drtr_dcto=r_fiel('enti',$drtr,'enti_dcto');
    if($enti_dcto>$dcto2){$dcto2=$enti_dcto;}
    if($drtr_dcto>$dcto2){$dcto2=$drtr_dcto;}
    
    $sql_docu='update '.$tabla.'2 set dcto=0 where idnum2='.$idnum;
    $res_docu=msql_query($sql_docu,$link_vcrx);
    
    $sql_docu ='update '.$tabla.'2, item0 ';
    $sql_docu.=' set puni=item_puni';
    $sql_docu.=' where item=item_codigo and item_descri not like "LOGISTICA%" and idnum2='.$idnum;
    $res_docu=msql_query($sql_docu,$link_vcrx);
    
    $sql_docu ='update '.$tabla.'2, item0 ';
    $sql_docu.=' set puni=0, cant=0, detalle="*** NO DISPONIBLE ***"';
    $sql_docu.=' where item=item_codigo and item_hoja=0 and item_descri not like "LOGISTICA%" and idnum2='.$idnum;
    $res_docu=msql_query($sql_docu,$link_vcrx);
    
    $sql_docu ='update '.$tabla.'2, item0 ';
    $sql_docu.=' set dcto='.$dcto2;
    $sql_docu.=' where item=item_codigo and item_descri not like "LOGISTICA%" and idnum2='.$idnum;
    $res_docu=msql_query($sql_docu,$link_vcrx);
    
    $sql_docu ='select * from '.$tabla.'1 ';
    $sql_docu.=' where status!="O" and status!="X"' ;
    $sql_docu.=' and(enti like "'.$enti.'%" ';
    if($drtr!="")
    {$sql_docu.=' or drtr like "'.$drtr.'%"';}
    $sql_docu.=')';
    $sql_docu.=' and fecha="'.$fecha.'"';
    $sql_docu.=' order by idnum1';
    $res_docu=msql_query($sql_docu,$link_vcrx);
//     echo '***'.$sql_docu;
    
    $logi_puni=3;
    if(msql_num_rows($res_docu)==2){$logi_puni=1.5;}
    if(msql_num_rows($res_docu)>=3){$logi_puni=1;}
    
    while($aso_docu=msql_fetch_assoc($res_docu))
		{dly1_logi($aso_docu['docu'],$aso_docu['idnum1'],$logi_puni);}
    
//     echo '****'.msql_error();
    
    return;
  }
  
  
  function dly1_logi($docu,$idnum,$puni)
  {
    // solicitado 25 enero 2019
    return;
    
		include('../_cfg/config.php');
    $logi_item="8818041298";
    
    //1709653727   JOSE ALARCON
    $enti_excep='1709653727';

    $tabla=r_fiel('docu',$docu,'tabla');

    if(!mb_stristr('vtas.clie',$tabla)){return;}
    
    $sql_logi='select * from '.$tabla.'1 where idnum1='.$idnum;
    $res_logi=msql_query($sql_logi,$link_vcrx);

    $enti_logi=substr(@msql_result($res_logi,0,'enti'),0,10);
    $drtr_logi=substr(@msql_result($res_logi,0,'drtr'),0,10);

    $sql_logi='select * from '.$tabla.'12 where idnum2='.$idnum.' and item like "'.$logi_item.'%"';
    $res_logi=msql_query($sql_logi,$link_vcrx);


//     echo '***'.$enti_excep;
//     echo '***'.$enti_logi;
//     echo '***'.$drtr_logi;
//     echo '+++'.msql_result($res_logi,0,'enti');
//     echo '+++'.msql_result($res_logi,0,'drtr');
    if(@mb_stristr($enti_excep,$enti_logi)){return;} 
    if(@mb_stristr($enti_excep,$drtr_logi)){return;} 
    
//    echo $sql_logi; 
    if(msql_num_rows($res_logi)==0)
    {
      $xiddet=get_iddet($docu,$idnum);
      $sql_inse='insert into '.$tabla.'2(idnum2,iddet,item)value("'.$idnum.'","'.$xiddet.'","'.$logi_item.'")';
      $res_inse=msql_query($sql_inse,$link_vcrx);
      $res_logi=msql_query($sql_logi,$link_vcrx);
    }
    $xiddet=msql_result($res_logi,0,'iddet');
    $sql_updt ='update '.$tabla.'2 ';
    $sql_updt.=' set item="'.$logi_item.'",';
    $sql_updt.=' cant=1, ';
    $sql_updt.=' puni="'.$puni.'", ';
    $sql_updt.=' detalle=" " ';
    $sql_updt.=' where idnum2="'.$idnum.'"';
    $sql_updt.=' and iddet="'.$xiddet.'"';
    $res_updt=msql_query($sql_updt,$link_vcrx);
//     echo $sql_updt;
//     echo msql_error();
    
    
    return;
    
  }
  
	function ecgl_calc1($docu,$idnum)
	{
		include('../_cfg/config.php');
// 		return;
		if($cfg_empr!='ecgl' and $_SESSION['user_vcrx_empr']!='ecgl'){return;}
    if(!mb_stristr('PF.PC.OP',substr($docu,0,2))){return;}
// 		echo '*/*/*';
// 		echo Date('h:i:s',strtotime('now'));
    $valmin=20;
    $perfinc=4;
    $saquinc=4;
    
    //22 agosto eliminamos incluidos
//     $perfinc=0;
//     $saquinc=0;
    
		$tabla=r_fiel('docu',$docu,'tabla');
    
//     if(!mb_stristr('vtas.clie.prod',$tabla)){return;}
    
    $sql_docu='select * from '.$tabla.'1 where idnum1='.$idnum;
    $res_docu=msql_query($sql_docu,$link_vcrx);
    $enti=msql_result($res_docu,0,'enti');
    $fecha=msql_result($res_docu,0,'fecha');
    
    $enti_dcto=r_fiel('enti',$enti,'enti_dcto');
    
    if($fecha<"2018-07-01"){return;}

// 		$sql_calc ='delete from '.$tabla.'2 ';
// 		$sql_calc.='where idnum2="'.$idnum.'"';
// 		$sql_calc.=' and (item not in(select item_codigo from item0 where item_lsub like "VIDR%") or item is null)';
// 		$res_calc=msql_query($sql_calc,$link_vcrx);
    		
		$sql_vidr ='select * from '.$tabla.'2 left join item0 ';
		$sql_vidr.=' on item=item_codigo ';
		$sql_vidr.=' where idnum2='.$idnum;
		$sql_vidr.=' and item_lsub like "VIDR%"';
		$sql_vidr.=' order by idnum2 ';
		
		$iddet=0;
		$ccont=0;
		$res_vidr=msql_query($sql_vidr,$link_vcrx);
		while($aso_vidr=msql_fetch_assoc($res_vidr))
		{
			$ccont++;
      $docu_dcto=$aso_vidr['dcto'];

      $perfor1=$aso_vidr['perfor1'];
      $perfor1=alltrim($perfor1);
      $perfor1=strtoupper($perfor1);
      $perfor1=str_replace(')','),',$perfor1);
      $perfor1=str_replace(';',',',$perfor1);
      $perfor1=str_replace('/',',',$perfor1);
      $perfor1=str_replace('-',',',$perfor1);
      $perfor1=str_replace(',,',',',$perfor1);
      $perfor1=str_replace(',',', ',$perfor1);

      $saques1=$aso_vidr['saques1'];
      $saques1=alltrim($saques1);
      $saques1=strtoupper($saques1);
      $saques1=str_replace(')','),',$saques1);
      $saques1=str_replace(';',',',$saques1);
      $saques1=str_replace('/',',',$saques1);
      $saques1=str_replace('-',',',$saques1);
      $saques1=str_replace(',,',',',$saques1);
      $saques1=str_replace(',',', ',$saques1);
        
			$sql_updt ='update '.$tabla.'2 ';
			$sql_updt.=' set perfor1="'.$perfor1.'",saques1="'.$saques1.'", itnum= '.$ccont;
			$sql_updt.=' where idnum2='.$idnum. ' and iddet='.$aso_vidr['iddet'];
			msql_query($sql_updt,$link_vcrx);
			$iddet=$aso_vidr['iddet'];
		}
		
		$ccont=0;
		$res_vidr=msql_query($sql_vidr,$link_vcrx);
		while($aso_vidr=msql_fetch_assoc($res_vidr))
		{
			$itnum=$aso_vidr['itnum'];
			$lmns=$aso_vidr['lmns'];
			$mm_l=$aso_vidr['mm_l'];
			$mm_a=$aso_vidr['mm_a'];
			$mrca=r_fiel('item',$aso_vidr['item'],'mrca');
			$mm_e=r_fiel('item',$aso_vidr['item']);
			$mm_e=trim(substr($mm_e,strpos($mm_e,'MM')-2,2));

			//TEMPLADO
			if($mm_e>0 and 1==2)
			{
				$sql_xitem ='select * from item0 where item_descri like "TEMPLA%" and item_descri like "%'.$mm_e.'MM%"';
				$res_xitem=msql_query($sql_xitem,$link_vcrx);
//         echo explain_sql($sql_xitem);

				$xitem=msql_result($res_xitem,0,'item_codigo');
				
				$iddet++;
				$sql_inse ='insert into '.$tabla.'2(idnum2,iddet,itnum,item)';
				$sql_inse.='value("'.$idnum.'","'.$iddet.'","'.$itnum.'","'.$xitem.'") ';
				$res_inse=msql_query($sql_inse,$link_vcrx);
			}
			
			//PULIDO
			// pulido si es vidrio ecgl va incluido
			if($aso_vidr['puli']>0 and 1==2)
			{
				$xitem="8818000252";
				if($aso_vidr['puli']==1 and $mrca!='ECGL'){$xitem="8818000328";}
// 				if($aso_vidr['puli']==1){$xitem="8818000328";}
				if($aso_vidr['puli']==2){$xitem="8818000260";}
				
				$iddet++;
				$sql_inse ='insert into '.$tabla.'2(idnum2,iddet,itnum,item)';
				$sql_inse.='value("'.$idnum.'","'.$iddet.'","'.$itnum.'","'.$xitem.'") ';
				$res_inse=msql_query($sql_inse,$link_vcrx);
			}
			
      $perf0=0;
      $perf1=0;
      $perf2=0;
      $saqu0=0;
      $saqu1=0;
      $saqu2=0;
      $perfor1=$aso_vidr['perfor1'];
      $saques1=$aso_vidr['saques1'];
      
			if($perfor1!='' and 1==2)
			{
        $perfor1=explode(',',$perfor1);
        $dperf0='';
        $dperf1='';
        $dperf2='';
        foreach($perfor1 as $rperfor1)
        {
          $rperfor1=trim($rperfor1);
          $rperf1=intval($rperfor1);
          if($rperf1>0 and $rperf1<=26)
          {
            $perf1++;
            $dperf0.=$rperfor1.', ';
          }
          if($rperf1>26)
          {
            $perf2++;
            $dperf2.=$rperfor1.', ';
          }
        }
        if($perf1>$perfinc){$perf0=$perfinc;}else{$perf0=$perf1;}
        $perf1=$perf1-$perfinc;
        if($perf1<0){$perf1=0;}
			}

			if($saques1!='' and 1==2)
			{
        $saques1=explode(',',$saques1);
        $dsaqu0='';
        $dsaqu1='';
        $dsaqu2='';
        foreach($saques1 as $rsaques1)
        {
          $rsaques1=trim($rsaques1);
          $rsaqu1=substr($rsaques1,0,1);
//           echo '*/*/*'.$rsaqu1;
          if($rsaqu1!='')
          {
          if(mb_stristr('R.C',$rsaqu1))
          {
            $saqu1++;
            if($saqu1<=$saquinc)
            {$dsaqu0.=$rsaques1.', ';}
            else
            {$dsaqu1.=$rsaques1.', ';}
          }
          if(mb_stristr('S.T',$rsaqu1))
          {
            $saqu2++;
            $dsaqu2.=$rsaques1.', ';
          }
          }
        }
        if($saqu1>$saquinc){$saqu0=$saquinc;}else{$saqu0=$saqu1;}
        $saqu1=$saqu1-$saquinc;
        if($saqu1<0){$saqu1=0;}
			}

			if($perf0>0 and 1==2)
			{
				$xitem="8818000278";
				$iddet++;
				$sql_inse ='insert into '.$tabla.'2(idnum2,iddet,itnum,item,cant,perfor1)';
				$sql_inse.='value("'.$idnum.'","'.$iddet.'","'.$itnum.'","'.$xitem.'","'.$perf0.'","'.$dperf0.'") ';
				$res_inse=msql_query($sql_inse,$link_vcrx);
			}

			if($perf1>0 and 1==2)
			{
				$xitem="8818000286";
				$iddet++;
				$sql_inse ='insert into '.$tabla.'2(idnum2,iddet,itnum,item,cant)';
				$sql_inse.='value("'.$idnum.'","'.$iddet.'","'.$itnum.'","'.$xitem.'","'.$perf1.'") ';
				$res_inse=msql_query($sql_inse,$link_vcrx);
			}

			if($perf2>0 and 1==2)
			{
				$xitem="8818000294";
				$iddet++;
				$sql_inse ='insert into '.$tabla.'2(idnum2,iddet,itnum,item,cant,perfor1)';
				$sql_inse.='value("'.$idnum.'","'.$iddet.'","'.$itnum.'","'.$xitem.'","'.$perf2.'","'.$dperf2.'") ';
				$res_inse=msql_query($sql_inse,$link_vcrx);
			}

			if($saqu0>0 and 1==2)
			{
				$xitem="8818000229";
				$iddet++;
				$sql_inse ='insert into '.$tabla.'2(idnum2,iddet,itnum,item,cant,saques1)';
				$sql_inse.='value("'.$idnum.'","'.$iddet.'","'.$itnum.'","'.$xitem.'","'.$saqu0.'","'.$dsaqu0.'") ';
				$res_inse=msql_query($sql_inse,$link_vcrx);
//         echo $sql_inse;
			}

			if($saqu1>0 and 1==2)
			{
				$xitem="8818000237";
				$iddet++;
				$sql_inse ='insert into '.$tabla.'2(idnum2,iddet,itnum,item,cant,saques1)';
				$sql_inse.='value("'.$idnum.'","'.$iddet.'","'.$itnum.'","'.$xitem.'","'.$saqu1.'","'.$dsaqu1.'") ';
				$res_inse=msql_query($sql_inse,$link_vcrx);
			}

			if($saqu2>0 and 1==2)
			{
				$xitem="8818000245";
				$iddet++;
				$sql_inse ='insert into '.$tabla.'2(idnum2,iddet,itnum,item,cant,saques1)';
				$sql_inse.='value("'.$idnum.'","'.$iddet.'","'.$itnum.'","'.$xitem.'","'.$saqu2.'","'.$dsaqu2.'") ';
				$res_inse=msql_query($sql_inse,$link_vcrx);
			}

      
			$sql_updt ='update '.$tabla.'2 ';
			$sql_updt.=' set ';
			$sql_updt.=' lmns="'.$lmns.'",';
			$sql_updt.=' mm_l="'.$mm_l.'",';
			$sql_updt.=' mm_a="'.$mm_a.'",';
			$sql_updt.=' prop=0,';
			$sql_updt.=' iddet=iddet';
			$sql_updt.=' where idnum2='.$idnum.' and itnum='.$itnum;
			msql_query($sql_updt,$link_vcrx);

			$sql_updt ='update '.$tabla.'2 ';
			$sql_updt.=' set ';
			$sql_updt.=' prop=0,';
			$sql_updt.=' iddet=iddet';
			$sql_updt.=' where idnum2='.$idnum.' and itnum='.$itnum;

		}
		
		$sql_calc ='update '.$tabla.'2  ';
		$sql_calc.=' left join item0 on item=item_codigo ';
		$sql_calc.=' set cant=lmns*(mm_l+mm_a)*2/1000 ';
		$sql_calc.=' where idnum2='.$idnum;
		$sql_calc.=' and item_umdd like "MT%"';
		$res_calc=msql_query($sql_calc,$link_vcrx);

		$sql_calc ='update '.$tabla.'2  ';
		$sql_calc.=' left join item0 on item=item_codigo ';
		$sql_calc.=' set cant=lmns*(mm_l*mm_a)/1000000 ';
		$sql_calc.=' where idnum2='.$idnum;
		$sql_calc.=' and item_umdd like "MT2%"';
		$res_calc=msql_query($sql_calc,$link_vcrx);
		
		$sql_calc ='update '.$tabla.'2  ';
		$sql_calc.=' left join item0 on item=item_codigo ';
		$sql_calc.=' set puni=item_puni,';
		$sql_calc.=' dcto="'.$docu_dcto.'", ';
		$sql_calc.=' ice=item_ice, ';
		$sql_calc.=' iva=item_iva, ';
		$sql_calc.=' irbp=item_irbp, ';
		$sql_calc.=' prop=0 ';
		$sql_calc.=' where idnum2='.$idnum;
		$res_calc=msql_query($sql_calc,$link_vcrx);


    $sql_calc ='update '.$tabla.'2  ';
		$sql_calc.=' left join item0 on item=item_codigo ';
		$sql_calc.=' set dcto=item_dcto ';
		$sql_calc.=' where idnum2='.$idnum;
		$sql_calc.=' and item_dcto>=99';
		$res_calc=msql_query($sql_calc,$link_vcrx);

    if($docu_dcto==0)
    {
      $sql_calc ='update '.$tabla.'2  ';
      $sql_calc.=' left join '.$tabla.'1  on idnum1=idnum2 ';
      $sql_calc.=' left join enti0 on enti=enti_codigo ';
      $sql_calc.=' set dcto="'.$enti_dcto.'"';
      $sql_calc.=' where idnum2='.$idnum;
      $sql_calc.=' and dcto<90';
      $res_calc=msql_query($sql_calc,$link_vcrx);
    }

		$sql_calc ='update '.$tabla.'2  ';
		$sql_calc.=' set dcto=0 ';
		$sql_calc.=' where idnum2='.$idnum;
		$sql_calc.=' and dcto is null';
		$res_calc=msql_query($sql_calc,$link_vcrx);

		$vtotal=r_fiel($tabla.'1',$idnum,'total');
		if($vtotal<$valmin)
		{
			$sql_calc ='update '.$tabla.'2  ';
			$sql_calc.=' set puni=1/1000, dcto=0 ';
			$sql_calc.=' where idnum2='.$idnum;
			$res_calc=msql_query($sql_calc,$link_vcrx);

			$sql_calc ='select sum(cant) as tcant from '.$tabla.'12 ';
			$sql_calc.=' where idnum2='.$idnum;
			$sql_calc.=' and item_descri not like "VIDR%" ';
			$res_calc=msql_query($sql_calc,$link_vcrx);
			$tcanti=msql_result($res_calc,0,'tcant');  

			$sql_calc ='update item0,'.$tabla.'2 ';
			$sql_calc.=' set puni='.$valmin.'/'.$tcanti;
			$sql_calc.=' where idnum2='.$idnum;
			$sql_calc.=' and item=item_codigo ';
			$sql_calc.=' and item_descri not like "VIDR%" ';
			$res_calc=msql_query($sql_calc,$link_vcrx);
      
		}
		
	}
  
	function ecgl_calc1_bk($docu,$idnum)
	{
		include('../_cfg/config.php');
// 		return;
		if($cfg_empr!='ecgl' and $_SESSION['user_vcrx_empr']!='ecgl'){return;}
    if(!mb_stristr('PF.PC.OP',substr($docu,0,2))){return;}
// 		echo '*/*/*';
// 		echo Date('h:i:s',strtotime('now'));
    $valmin=20;
    $perfinc=4;
    $saquinc=4;
    
    //22 agosto eliminamos incluidos
//     $perfinc=0;
//     $saquinc=0;
    
		$tabla=r_fiel('docu',$docu,'tabla');
    
//     if(!mb_stristr('vtas.clie.prod',$tabla)){return;}
    
    $sql_docu='select * from '.$tabla.'1 where idnum1='.$idnum;
    $res_docu=msql_query($sql_docu,$link_vcrx);
    $enti=msql_result($res_docu,0,'enti');
    $fecha=msql_result($res_docu,0,'fecha');
    
    $enti_dcto=r_fiel('enti',$enti,'enti_dcto');
    
//     echo $fecha;
    if($fecha<"2018-07-01"){return;}

		$sql_calc ='delete from '.$tabla.'2 ';
		$sql_calc.='where idnum2="'.$idnum.'"';
		$sql_calc.=' and (item not in(select item_codigo from item0 where item_lsub like "VIDR%") or item is null)';
		$res_calc=msql_query($sql_calc,$link_vcrx);
    
//     if($tabla=='prod'){return;}
		
		$sql_vidr ='select * from '.$tabla.'2 left join item0 ';
		$sql_vidr.=' on item=item_codigo ';
		$sql_vidr.=' where idnum2='.$idnum;
		$sql_vidr.=' and item_lsub like "VIDR%"';
		$sql_vidr.=' order by idnum2 ';
		
		$iddet=0;
		$ccont=0;
		$res_vidr=msql_query($sql_vidr,$link_vcrx);
		while($aso_vidr=msql_fetch_assoc($res_vidr))
		{
			$ccont++;
      $docu_dcto=$aso_vidr['dcto'];

      $perfor1=$aso_vidr['perfor1'];
      $perfor1=alltrim($perfor1);
      $perfor1=strtoupper($perfor1);
      $perfor1=str_replace(')','),',$perfor1);
      $perfor1=str_replace(';',',',$perfor1);
      $perfor1=str_replace('/',',',$perfor1);
      $perfor1=str_replace('-',',',$perfor1);
      $perfor1=str_replace(',,',',',$perfor1);
      $perfor1=str_replace(',',', ',$perfor1);

      $saques1=$aso_vidr['saques1'];
      $saques1=alltrim($saques1);
      $saques1=strtoupper($saques1);
      $saques1=str_replace(')','),',$saques1);
      $saques1=str_replace(';',',',$saques1);
      $saques1=str_replace('/',',',$saques1);
      $saques1=str_replace('-',',',$saques1);
      $saques1=str_replace(',,',',',$saques1);
      $saques1=str_replace(',',', ',$saques1);
        
			$sql_updt ='update '.$tabla.'2 ';
			$sql_updt.=' set perfor1="'.$perfor1.'",saques1="'.$saques1.'", itnum= '.$ccont;
			$sql_updt.=' where idnum2='.$idnum. ' and iddet='.$aso_vidr['iddet'];
			msql_query($sql_updt,$link_vcrx);
			$iddet=$aso_vidr['iddet'];
		}
		
		$ccont=0;
//     msql_data_seek($res_vidr,0);
		$res_vidr=msql_query($sql_vidr,$link_vcrx);
		while($aso_vidr=msql_fetch_assoc($res_vidr))
		{
			$itnum=$aso_vidr['itnum'];
			$lmns=$aso_vidr['lmns'];
			$mm_l=$aso_vidr['mm_l'];
			$mm_a=$aso_vidr['mm_a'];
			$mrca=r_fiel('item',$aso_vidr['item'],'mrca');
			$mm_e=r_fiel('item',$aso_vidr['item']);
			$mm_e=trim(substr($mm_e,strpos($mm_e,'MM')-2,2));

			//TEMPLADO
			if($mm_e>0)
			{
				$sql_xitem ='select * from item0 where item_descri like "TEMPLA%" and item_descri like "%'.$mm_e.'MM%"';
				$res_xitem=msql_query($sql_xitem,$link_vcrx);
//         echo explain_sql($sql_xitem);

				$xitem=msql_result($res_xitem,0,'item_codigo');
				
				$iddet++;
				$sql_inse ='insert into '.$tabla.'2(idnum2,iddet,itnum,item)';
				$sql_inse.='value("'.$idnum.'","'.$iddet.'","'.$itnum.'","'.$xitem.'") ';
				$res_inse=msql_query($sql_inse,$link_vcrx);
			}
			
			//PULIDO
			// pulido si es vidrio ecgl va incluido
			if($aso_vidr['puli']>0)
			{
				$xitem="8818000252";
				if($aso_vidr['puli']==1 and $mrca!='ECGL'){$xitem="8818000328";}
// 				if($aso_vidr['puli']==1){$xitem="8818000328";}
				if($aso_vidr['puli']==2){$xitem="8818000260";}
				
				$iddet++;
				$sql_inse ='insert into '.$tabla.'2(idnum2,iddet,itnum,item)';
				$sql_inse.='value("'.$idnum.'","'.$iddet.'","'.$itnum.'","'.$xitem.'") ';
				$res_inse=msql_query($sql_inse,$link_vcrx);
			}
			
      $perf0=0;
      $perf1=0;
      $perf2=0;
      $saqu0=0;
      $saqu1=0;
      $saqu2=0;
      $perfor1=$aso_vidr['perfor1'];
      $saques1=$aso_vidr['saques1'];
      
			if($perfor1!='')
			{
        $perfor1=explode(',',$perfor1);
        $dperf0='';
        $dperf1='';
        $dperf2='';
        foreach($perfor1 as $rperfor1)
        {
          $rperfor1=trim($rperfor1);
          $rperf1=intval($rperfor1);
          if($rperf1>0 and $rperf1<=26)
          {
            $perf1++;
            $dperf0.=$rperfor1.', ';
          }
          if($rperf1>26)
          {
            $perf2++;
            $dperf2.=$rperfor1.', ';
          }
        }
        if($perf1>$perfinc){$perf0=$perfinc;}else{$perf0=$perf1;}
        $perf1=$perf1-$perfinc;
        if($perf1<0){$perf1=0;}
			}

			if($saques1!='')
			{
        $saques1=explode(',',$saques1);
        $dsaqu0='';
        $dsaqu1='';
        $dsaqu2='';
        foreach($saques1 as $rsaques1)
        {
          $rsaques1=trim($rsaques1);
          $rsaqu1=substr($rsaques1,0,1);
//           echo '*/*/*'.$rsaqu1;
          if($rsaqu1!='')
          {
          if(mb_stristr('R.C',$rsaqu1))
          {
            $saqu1++;
            if($saqu1<=$saquinc)
            {$dsaqu0.=$rsaques1.', ';}
            else
            {$dsaqu1.=$rsaques1.', ';}
          }
          if(mb_stristr('S.T',$rsaqu1))
          {
            $saqu2++;
            $dsaqu2.=$rsaques1.', ';
          }
          }
        }
        if($saqu1>$saquinc){$saqu0=$saquinc;}else{$saqu0=$saqu1;}
        $saqu1=$saqu1-$saquinc;
        if($saqu1<0){$saqu1=0;}
			}

			if($perf0>0)
			{
				$xitem="8818000278";
				$iddet++;
				$sql_inse ='insert into '.$tabla.'2(idnum2,iddet,itnum,item,cant,perfor1)';
				$sql_inse.='value("'.$idnum.'","'.$iddet.'","'.$itnum.'","'.$xitem.'","'.$perf0.'","'.$dperf0.'") ';
				$res_inse=msql_query($sql_inse,$link_vcrx);
			}

			if($perf1>0)
			{
				$xitem="8818000286";
				$iddet++;
				$sql_inse ='insert into '.$tabla.'2(idnum2,iddet,itnum,item,cant)';
				$sql_inse.='value("'.$idnum.'","'.$iddet.'","'.$itnum.'","'.$xitem.'","'.$perf1.'") ';
				$res_inse=msql_query($sql_inse,$link_vcrx);
			}

			if($perf2>0)
			{
				$xitem="8818000294";
				$iddet++;
				$sql_inse ='insert into '.$tabla.'2(idnum2,iddet,itnum,item,cant,perfor1)';
				$sql_inse.='value("'.$idnum.'","'.$iddet.'","'.$itnum.'","'.$xitem.'","'.$perf2.'","'.$dperf2.'") ';
				$res_inse=msql_query($sql_inse,$link_vcrx);
			}

//       echo '*/*/*'.$saqu0;
//       echo '*/*/*'.$saqu1;
//       echo '*/*/*'.$saqu2;
			if($saqu0>0)
			{
				$xitem="8818000229";
				$iddet++;
				$sql_inse ='insert into '.$tabla.'2(idnum2,iddet,itnum,item,cant,saques1)';
				$sql_inse.='value("'.$idnum.'","'.$iddet.'","'.$itnum.'","'.$xitem.'","'.$saqu0.'","'.$dsaqu0.'") ';
				$res_inse=msql_query($sql_inse,$link_vcrx);
//         echo $sql_inse;
			}

			if($saqu1>0)
			{
				$xitem="8818000237";
				$iddet++;
				$sql_inse ='insert into '.$tabla.'2(idnum2,iddet,itnum,item,cant,saques1)';
				$sql_inse.='value("'.$idnum.'","'.$iddet.'","'.$itnum.'","'.$xitem.'","'.$saqu1.'","'.$dsaqu1.'") ';
				$res_inse=msql_query($sql_inse,$link_vcrx);
			}

			if($saqu2>0)
			{
				$xitem="8818000245";
				$iddet++;
				$sql_inse ='insert into '.$tabla.'2(idnum2,iddet,itnum,item,cant,saques1)';
				$sql_inse.='value("'.$idnum.'","'.$iddet.'","'.$itnum.'","'.$xitem.'","'.$saqu2.'","'.$dsaqu2.'") ';
				$res_inse=msql_query($sql_inse,$link_vcrx);
			}

      
			$sql_updt ='update '.$tabla.'2 ';
			$sql_updt.=' set ';
			$sql_updt.=' lmns="'.$lmns.'",';
			$sql_updt.=' mm_l="'.$mm_l.'",';
			$sql_updt.=' mm_a="'.$mm_a.'",';
			$sql_updt.=' prop=0,';
			$sql_updt.=' iddet=iddet';
			$sql_updt.=' where idnum2='.$idnum.' and itnum='.$itnum;
			msql_query($sql_updt,$link_vcrx);

			$sql_updt ='update '.$tabla.'2 ';
			$sql_updt.=' set ';
			$sql_updt.=' prop=0,';
			$sql_updt.=' iddet=iddet';
			$sql_updt.=' where idnum2='.$idnum.' and itnum='.$itnum;
// 			msql_query($sql_updt,$link_vcrx);

		}
		
		$sql_calc ='update '.$tabla.'2  ';
		$sql_calc.=' left join item0 on item=item_codigo ';
		$sql_calc.=' set cant=lmns*(mm_l+mm_a)*2/1000 ';
		$sql_calc.=' where idnum2='.$idnum;
		$sql_calc.=' and item_umdd like "MT%"';
		$res_calc=msql_query($sql_calc,$link_vcrx);
//     echo $sql_calc;

		$sql_calc ='update '.$tabla.'2  ';
		$sql_calc.=' left join item0 on item=item_codigo ';
		$sql_calc.=' set cant=lmns*(mm_l*mm_a)/1000000 ';
		$sql_calc.=' where idnum2='.$idnum;
		$sql_calc.=' and item_umdd like "MT2%"';
		$res_calc=msql_query($sql_calc,$link_vcrx);
		
		$sql_calc ='update '.$tabla.'2  ';
		$sql_calc.=' left join item0 on item=item_codigo ';
		$sql_calc.=' set puni=item_puni,';
		$sql_calc.=' dcto="'.$docu_dcto.'", ';
		$sql_calc.=' ice=item_ice, ';
		$sql_calc.=' iva=item_iva, ';
		$sql_calc.=' irbp=item_irbp, ';
		$sql_calc.=' prop=0 ';
		$sql_calc.=' where idnum2='.$idnum;
		$res_calc=msql_query($sql_calc,$link_vcrx);


    $sql_calc ='update '.$tabla.'2  ';
		$sql_calc.=' left join item0 on item=item_codigo ';
		$sql_calc.=' set dcto=item_dcto ';
		$sql_calc.=' where idnum2='.$idnum;
		$sql_calc.=' and item_dcto>=99';
		$res_calc=msql_query($sql_calc,$link_vcrx);

    if($docu_dcto==0)
    {
      $sql_calc ='update '.$tabla.'2  ';
      $sql_calc.=' left join '.$tabla.'1  on idnum1=idnum2 ';
      $sql_calc.=' left join enti0 on enti=enti_codigo ';
      $sql_calc.=' set dcto="'.$enti_dcto.'"';
      $sql_calc.=' where idnum2='.$idnum;
      $sql_calc.=' and dcto<90';
      $res_calc=msql_query($sql_calc,$link_vcrx);
    }

		$sql_calc ='update '.$tabla.'2  ';
		$sql_calc.=' set dcto=0 ';
		$sql_calc.=' where idnum2='.$idnum;
		$sql_calc.=' and dcto is null';
		$res_calc=msql_query($sql_calc,$link_vcrx);

// 		$sql_vidr ='select * from '.$tabla.'11';
// 		$sql_vidr.=' where idnum2='.$idnum;
// 		$res_vidr=msql_query($sql_vidr,$link_vcrx);
		
		$vtotal=r_fiel($tabla.'1',$idnum,'total');
		if($vtotal<$valmin)
		{
			$sql_calc ='update '.$tabla.'2  ';
			$sql_calc.=' set puni=1/1000, dcto=0 ';
			$sql_calc.=' where idnum2='.$idnum;
			$res_calc=msql_query($sql_calc,$link_vcrx);

			$sql_calc ='select sum(cant) as tcant from '.$tabla.'12 ';
			$sql_calc.=' where idnum2='.$idnum;
// 			$sql_calc.=' and item_descri like "TEMPL%" ';
			$sql_calc.=' and item_descri not like "VIDR%" ';
			$res_calc=msql_query($sql_calc,$link_vcrx);
			$tcanti=msql_result($res_calc,0,'tcant');  

			$sql_calc ='update item0,'.$tabla.'2 ';
// 			$sql_calc ='update '.$tabla.'2 ';
			$sql_calc.=' set puni='.$valmin.'/'.$tcanti;
			$sql_calc.=' where idnum2='.$idnum;
			$sql_calc.=' and item=item_codigo ';
// 			$sql_calc.=' and item_descri like "TEMPL%" ';
			$sql_calc.=' and item_descri not like "VIDR%" ';
			$res_calc=msql_query($sql_calc,$link_vcrx);
      
		}
		
// 		echo '<br>';
// 		echo Date('h:i:s',strtotime('now'));
		
	}
  
  
  function ecgl_clie2prod($idnum)
  {
		include('../_cfg/config.php');
// 		include('php_gene1.php');
		
		if($cfg_empr!='ecgl' and $_SESSION['user_vcrx_empr']!='ecgl'){return;}
		
    $sql_clie='select * from clie1 where idnum1="'.$idnum.'"';
    $res_clie=msql_query($sql_clie,$link_vcrx);
    $docu=msql_result($res_clie,0,'docu');
    $status=msql_result($res_clie,0,'status');
    $newidnum=0;
    
    if(!mb_stristr('ROX',$status))
    {
      $newdocu='OP10';
    	$newnumero=nume_docu($newdocu);
      $newidnum=clon_docu($docu,$idnum,$newdocu);

      $sql_clie='update prod1 set fecha=now(), numero="'.$newnumero.'", iuser="'.$user_codigo.'",idate=now() where idnum1="'.$newidnum.'"';
      $res_clie=msql_query($sql_clie,$link_vcrx);
      
      $tabla='prod';
      $sql_calc ='delete from '.$tabla.'2 ';
      $sql_calc.='where idnum2="'.$newidnum.'"';
      $sql_calc.=' and (item not in(select item_codigo from item0 where item_lsub like "VIDR%") or item is null)';
//       $res_calc=msql_query($sql_calc,$link_vcrx);

      $sql_clie='update clie1 set status="R" where idnum1="'.$idnum.'"';
      $res_clie=msql_query($sql_clie,$link_vcrx);
    }
    
    return $newidnum;    
  }

  
  function ecgl_calc3($docu,$idnum)
  {
  	include('../_cfg/config.php');
// 		return;
		if($cfg_empr!='ecgl' and $_SESSION['user_vcrx_empr']!='ecgl'){return;}
    if(!mb_stristr('PF.PC.OP',substr($docu,0,2))){return;}
    
    $total1=0;
    $total2=0;
		$tabla=r_fiel('docu',$docu,'tabla');

    $sql_docu='select * from '.$tabla.'1 where idnum1='.$idnum;
    $res_docu=msql_query($sql_docu,$link_vcrx);
    $enti=msql_result($res_docu,0,'enti');
    $fecha=msql_result($res_docu,0,'fecha');
    $refnume=msql_result($res_docu,0,'refnume');
//     $total2=msql_result($res_docu,0,'total2');
    
    if($refnume*1>0)
    {
//      $sql_docu='select * from '.$tabla.'1 where docu="'.$docu.'" and numero="'.$refnume.'" and status!="O" and status!="X" order by fecha desc';
//      $res_docu=msql_query($sql_docu,$link_vcrx);
//      if(msql_num_rows($res_docu)!=0) 
       $sql_docu='update '.$tabla.'2 set dcto=0 where idnum2="'.$idnum.'"';
       $res_docu=msql_query($sql_docu,$link_vcrx);
     {
//        $refidnum=@msql_result($res_docu,0,'idnum1');
       $sql_docu='select sum(total) as total, sum(tdcto) as tdcto from '.$tabla.'12 where idnum1="'.$idnum.'"';
       $res_docu=msql_query($sql_docu,$link_vcrx);
       $total2=msql_result($res_docu,0,'total');
       $tdcto2=msql_result($res_docu,0,'tdcto');
       $total2=$total2-$tdcto2;
       
     }  
     
     $sql_docu='select * from '.$tabla.'1 where docu="'.$docu.'" and numero="'.$refnume.'" and status!="O" and status!="X" order by fecha desc';
     $res_docu=msql_query($sql_docu,$link_vcrx);
     if(msql_num_rows($res_docu)!=0) 
     {
       $refidnum=@msql_result($res_docu,0,'idnum1');
       $sql_docu='select sum(total) as total,  sum(tdcto) as tdcto from '.$tabla.'12 where idnum1="'.$refidnum.'"';
       $res_docu=msql_query($sql_docu,$link_vcrx);
       $total1=msql_result($res_docu,0,'total');
       $tdcto1=msql_result($res_docu,0,'tdcto');
       $total1=$total1-$tdcto1;
       
     }  
      
     if($total2>$total1)
     {
       $xdcto=(1-($total1/$total2))*100;
       $sql_docu='update '.$tabla.'2 set dcto="'.$xdcto.'" where idnum2="'.$idnum.'"';
       $res_docu=msql_query($sql_docu,$link_vcrx);
       
     }
     
    }
 
//     echo $total1.'***'.$total2;
    
    return;
  }
  
  
  
	function ecgl_dise1($docu,$idnum)
	{
		include('../_cfg/config.php');
		
		if($cfg_empr!='ecgl' and $_SESSION['user_vcrx_empr']!='ecgl'){return;}
    if(!mb_stristr('PF.PC.OP',substr($docu,0,2))){return;}
		
// 		echo Date('h:i:s',strtotime('now'));
    $valmin=20;
		$tabla=r_fiel('docu',$docu,'tabla');
    
//     if(!mb_stristr('clie.prod',$tabla)){return;}

		$sql_vidr ='select * from '.$tabla.'12 ';
		$sql_vidr.=' where idnum2='.$idnum;
		$sql_vidr.=' and item_descri like "VIDR%"';
		$sql_vidr.=' order by itnum ';

   echo '<p class="saltodepagina"></p>';
//    echo '<div></div>';
    
		$iddet=0;
		$ccont=0;
		$res_vidr=msql_query($sql_vidr,$link_vcrx);
		while($aso_vidr=msql_fetch_assoc($res_vidr))
		{
      ?>
<!--       <p class="saltodepagina"></p> -->
      <?php
        $item_descri=$aso_vidr['item_descri'];
        $mm_e=substr($item_descri,strpos($item_descri,'MM')-2,2);
        $mm_lo=$aso_vidr['mm_l'];
        $mm_ao=$aso_vidr['mm_a'];
        $mm_l=$mm_lo;
        $mm_a=$mm_ao;
        $mm_r=$mm_a/$mm_l;
//         $mm_a=150;
        $mm_a=75;
        $mm_l=round($mm_a/$mm_r,0);
        $puli=$aso_vidr['puli'];
        $tmpl=$aso_vidr['tmpl'];
        $pulido='NINGUNO';
        if($puli==1){$pulido='MATE';}
        if($puli==2){$pulido='BRILLANTE';}
        if($tmpl==1){$templar='* CRUDO *';}
        if($tmpl==2){$templar='TEMPLADO';}
      ?>
<!--       <hr style="border-width:4px;"> -->
      <div style="width:1mm; height:1mm; float:left; "></div>
<!--       <div style="width:200mm; height:290mm; float:left; border:dashed 1px;">  -->
      <div style="width:103mm; min-height:145mm; float:left; border:dashed 1px;"> 
        
      <br>
      <table width="98%" align="center" cellpadding="0" cellspacing="0" 
             style="border-collapse:collapse; font-size:14px; white-space:nowrap;">
        <tr style="height:7mm; vertical-align:text-top; ">
          <td style="font-weight:bolder;" width="1%">CLIENTE : </td>
          <td style="font-weight:;" colspan="3"><?php echo $aso_vidr['enti_descri'];?></td>
        </tr>
        <tr style="height:7mm; vertical-align:text-top; ">
          <td style="font-weight:bolder;">DESCRIP. : </td>
          <td style="font-weight:;"><?php echo $aso_vidr['item_descri'].'<br>'.$aso_vidr['lmns'].' LAM ('.$aso_vidr['mm_l'].' X '.$aso_vidr['mm_a'].')';?></td>
          <td style="font-weight:bolder;" align="right">FECHA :</td>
          <td style="font-weight:;"><?php echo $aso_vidr['fdian'].' - '.$aso_vidr['fechadmy'];?></td>
        </tr>
        <tr style="height:7mm; vertical-align:text-top; ">
          <td style="font-weight:bolder;" >PULIDO :</td>
          <td style="font-weight:;"><?php echo ''.$pulido.'<br> /'.$templar;?></td>
          <td style="font-weight:bolder;" align="right" width="10%">ORDEN :</td>
          <td style="font-weight:;" width="1%"><?php echo $aso_vidr['numero'].  '<br>ITEM #'.$aso_vidr['itnum'];;?></td>
        </tr>
      </table>
      <br>
      <table align="center" cellpadding="0" cellspacing="0" style="font-size:12px;border-collapse:collapse;white-space:nowrap;">
        <tr style="white-space:nowrap;">
          <td></td>
          <td align="center" valign="bottom">
              <?php echo $aso_vidr['mm_a'].'mm';?>
          </td>
          <td align="left" valign="bottom" style="font-weight:bolder">
            <?php echo $mm_e.'mm'; ?>
          </td>
        </tr>
        <tr style="white-space:nowrap;">
          <td align="center"><?php echo $aso_vidr['mm_l'].'<br>mm';?></td>
          <td>
            <div style="
                       position:relative;
                       width:<?php echo $mm_a?>mm;
                       height:<?php echo $mm_l?>mm;
                       <?php if($puli==0){echo 'border:solid 1px;';}?>
                       <?php if($puli==1){echo 'border:solid 4px;';}?>
                       <?php if($puli==2){echo 'border:double 8px;';}?>
                       ">
              <?php
              $perfor1=$aso_vidr['perfor1'];
              $saques1=$aso_vidr['saques1'];

              if($perfor1!='')
              {
                $perfor1=explode(',',$perfor1);
                $dperf0='';
                $dperf1='';
                $dperf2='';
                foreach($perfor1 as $rperfor1)
                {
                  $rperfor1=trim($rperfor1);
                  $rperf1=intval($rperfor1);
                  $dperf1=trim(substr($rperfor1,strpos($rperfor1,'(')));
                  $lperf1=intval(substr($dperf1,1));
                  $aperf1=intval(substr($dperf1,strpos($dperf1,'X')+1));
                  ?>
                  <?php if($rperf1>0){?>
                    <div style="
                                position:absolute;
                                height:<?php echo ceil($rperf1/5,0);?>mm;
                                width:<?php echo ceil($rperf1/5,0);?>mm;
                                bottom:<?php echo (($mm_l/$mm_lo)*$lperf1)-(1).'mm;'; ?>
                                left:<?php echo (($mm_a/$mm_ao)*$aperf1)-(1).'mm;'; ?>
                                border:solid 4px; 
                                -moz-border-radius: 50%;
                                -webkit-border-radius: 50%;
                                border-radius: 50%;
                                "
                     >
                    </div>
                    <div style="
                                position:absolute;
                                bottom:0mm;
                                left:0mm;
                                height:<?php echo (($mm_l/$mm_lo)*$lperf1).'mm;'; ?>
                                width:<?php echo (($mm_a/$mm_ao)*$aperf1).'mm;'; ?>
                                border-right:dashed 1px; 
                                border-top:dashed 1px; 
                                "
                     >
                    </div>
                    <div style="
                                position:absolute;
                                bottom:<?php echo (($mm_l/$mm_lo)*$lperf1)-(1).'mm;'; ?>
                                left:<?php echo (($mm_a/$mm_ao)*$aperf1)+(1).'mm;'; ?>
                                "
                     >
                      <?php echo '<b>'.$rperf1.'</b>'.$dperf1;?>
                    </div>
                  <?php }?>
                  <?php
                }
              }

              if($saques1!='')
              {
                $saques1=explode(',',$saques1);
                $dsaqu0='';
                $dsaqu1='';
                $dsaqu2='';
                foreach($saques1 as $rsaques1)
                {
                  $rsaques1=trim($rsaques1);
//                   $rsaqu1=intval($rsaques1);
                  $rsaqu1=substr($rsaques1,0,1);
                  $dsaqu1=trim(substr($rsaques1,strpos($rsaques1,'(')));
                  $lsaqu1=intval(substr($dsaqu1,1));
                  $asaqu1=intval(substr($dsaqu1,strpos($dsaqu1,'X')+1));
                  $slargo=10;
                  $sancho=5;
                  if($rsaqu1=='R' and ($lsaqu1==0 or $lsaqu1==$mm_lo))
                  {$tempo=$slargo;$slargo=$sancho;$sancho=$tempo;}
                  if($rsaqu1=='C' and ($lsaqu1>0 and $lsaqu1<$mm_lo))
                  {$tempo=$slargo;$slargo=$sancho;$sancho=$tempo;}
                  ?>
                  <?php if($rsaqu1!=''){?>
                  <?php if(mb_stristr('R.C.S.T',$rsaqu1)){?>
                    <div style="
                                position:absolute;
                                height:<?php echo $slargo;?>mm;
                                width:<?php echo $sancho;?>mm;
                                bottom:
                                <?php
                                  if($lsaqu1==0){echo '-1mm;';}
                                  if($lsaqu1==$mm_lo){echo ($mm_l-$slargo).'mm;';}
                                  if($lsaqu1>0 and $lsaqu1<$mm_lo){echo (($mm_l/$mm_lo)*$lsaqu1)-($slargo/2).'mm;';}
                                ?>
                                left:
                                <?php
                                  if($asaqu1==0){echo '-1mm;';}
                                  if($asaqu1==$mm_ao){echo ($mm_a-$sancho).'mm;';}
                                  if($asaqu1>0 and $asaqu1<$mm_ao){echo (($mm_a/$mm_ao)*$asaqu1)-($sancho/2).'mm;';}
                                ?>
                                border:dashed 2px; 
                                <?php
                                  $radius='';
                                  if($rsaqu1=='C')
                                  {
                                    if($lsaqu1==0){$radius='50% 50% 0% 0%';}
                                    if($lsaqu1==$mm_lo){$radius='0% 0% 50% 50%';}
                                    if($asaqu1==0){$radius='0% 50% 50% 0%';}
                                    if($asaqu1==$mm_ao){$radius='50% 0% 0% 50%';}
                                    if($radius!='')
                                    {echo '-moz-border-radius:'.$radius.';-webkit-border-radius:'.$radius.'; border-radius: '.$radius.';';}
                                  }
                                ?>
                                
                                "
                     >
                    </div>
                    <div style="
                                position:absolute;
                                bottom:
                                <?php
                                  if($lsaqu1==0){echo (($mm_l/$mm_lo)*$lsaqu1)+($slargo).'mm;';}
                                  if($lsaqu1==$mm_lo){echo (($mm_l/$mm_lo)*$lsaqu1)-($slargo).'mm;';}
                                  if($lsaqu1>0 and $lsaqu1<$mm_lo){echo (($mm_l/$mm_lo)*$lsaqu1)+($slargo/2).'mm;';}
                                ?>
                                left:
                                <?php
                                  if($asaqu1==0){echo (($mm_a/$mm_ao)*$asaqu1)+($sancho).'mm;';}
                                  if($asaqu1==$mm_ao){echo (($mm_a/$mm_ao)*$asaqu1)-(0).'mm;';}
                                  if($asaqu1>0 and $asaqu1<$mm_ao){echo (($mm_a/$mm_ao)*$asaqu1)+($sancho/2).'mm;';}
                                ?>
                                "
                     >
                      <?php echo '<b>'.$rsaqu1.'</b>'.$dsaqu1;?>
                    </div>
                  <?php }?>
                  <?php }?>
                  <?php
                }
              }

              ?>        
            </div>
          </td>
          <td align="center" style="font-weight:bolder; font-size:150%;"><?php echo 'X'.$aso_vidr['lmns'].'';?></td>
        </tr>
        <tr style="white-space:nowrap;">
          <td></td>
          <td></td>
          <td></td>
        </tr>
      </table>
      
      </div>


      <?php
		}
		
		$ccont=0;
		
	}
  

  function pcs1_calc1($docu,$idnum)
  {
       if($idnum==8649){return;}
       
    // recalcula descuentos para contratos
		include('../_cfg/config.php');

       
    if($cfg_empr!='pcs1' and $_SESSION['user_vcrx_empr']!='pcs1'){return;}
    if(!mb_stristr('FC',substr($docu,0,2))){return;}

// 		$tabla=r_fiel('docu',$docu,'tabla');
    $tabla='vtas';
    
    $sql_docu='select * from '.$tabla.'1 where idnum1='.$idnum;
    $res_docu=msql_query($sql_docu,$link_vcrx);
    $enti=msql_result($res_docu,0,'enti');
    $fecha=msql_result($res_docu,0,'fecha');
    $autornum=msql_result($res_docu,0,'autornum');
    if(!empty($autornum)){return;}

    
     $total = 2333.33;
    //solicitado el 15/mar/19
    if($fecha>="2019-03-01"){$total = 666.67;}
    
    if($fecha>="2019-06-01"){$total = 2333.33;}
    
    if($fecha>="2020-03-01"){$total = 746.67;}
    
    if($fecha>="2020-06-01"){$total = 2333.33;}
    
    if($fecha>="2021-03-24"){$total = 746.67;}

    if($fecha>="2021-06-01"){$total = 2333.33;}
       
    if($fecha>="2022-03-22"){$total = 746.67;}
       
    if($fecha>="2022-04-20"){$total = 3961.49;}

    if($fecha>="2023-01-01"){$total = 3873.33;}
       
    if($fecha>="2024-05-01"){$total = 3977.0795;}

    // avicola fernandez
    if(mb_stristr('0992142618001',$enti))
    {
      // contenedores con precio 0
//       $xcntns='"HDCR1127351","MORU5805134","VERU4752203"';
//       $xcntns='"HDCR1127351","MORU5805134","VERU4752203","GCEU66625054"';
         
//       if($fecha>="2020-05-01"){$xcntns='"HDCR1127351"';}
//       if($fecha>="2020-06-01"){$xcntns='"HDCR1127351","MORU5805134","VERU4752203"';;}
      if($fecha>="2021-06-01")
      {
           $xcntns ='';
           $xcntns.='"GCEU6625054",';
           $xcntns.='"VERU4761186",';
           $xcntns.='"TRIU8472285",';
           $xcntns.='"TRIU8366025",';
           $xcntns.='"APRU5024871",';
           $xcntns.='"TRIU8283830",';
           $xcntns.='"TOLU7831461",';
           $xcntns.='"GTIU8001250",';
           $xcntns.='"TRIU8284781",';
           $xcntns.='"ZZZZ9999999"';
      }
      
      
      // calcula descuento para ajustar al valor
//       $sql_docu='select * from '.$tabla.'2 where idnum2='.$idnum.' and cntn not in ('.$xcntns.')';
      $sql_docu='select * from '.$tabla.'2 where idnum2='.$idnum.' and cntn in ('.$xcntns.')';
      $res_docu=msql_query($sql_docu,$link_vcrx);
      $nitems = msql_num_rows($res_docu);
      $txitem = $total/$nitems;
         
//        echo '*/*/*/*'.date('H:i:s').$txitem;
      
      while($aso_docu=msql_fetch_assoc($res_docu))
      {
        $xdcto=(1-(($txitem/(1+$aso_docu['iva']/100))/($aso_docu['cant']*$aso_docu['puni'])))*100;
//         if($xdcto>0)
        if($xdcto>0 and $xdcto<99)
        {
          $sql_updt='update '.$tabla.'2 set dcto='.$xdcto.' where idnum2='.$idnum.' and iddet='.$aso_docu['iddet'];
          $res_updt=msql_query($sql_updt,$link_vcrx);
        } 
      }      
      
      
      // precio 0 para contenedores NO selecionados
//       $sql_docu='select * from '.$tabla.'2 where idnum2='.$idnum.' and cntn in ('.$xcntns.')';
      $sql_docu='select * from '.$tabla.'2 where idnum2='.$idnum.' and cntn not in ('.$xcntns.')';
      $res_docu=msql_query($sql_docu,$link_vcrx);
      $nitems = msql_num_rows($res_docu);
      $txitem = $total/$nitems;
         
      
      while($aso_docu=msql_fetch_assoc($res_docu))
      {
        $xdcto=(1-(($txitem/(1+$aso_docu['iva']/100))/($aso_docu['cant']*$aso_docu['puni'])))*100;
        $xdcto=99.99;
        if($xdcto>0)
        {
          $sql_updt='update '.$tabla.'2 set puni=0.000001, dcto='.$xdcto.' where idnum2='.$idnum.' and iddet='.$aso_docu['iddet'];
          $res_updt=msql_query($sql_updt,$link_vcrx);

          if($fecha>="2020-05-01")
          {
//             $sql_updt='update '.$tabla.'2 set puni=19.50, dcto=0 where idnum2='.$idnum.' and iddet='.$aso_docu['iddet'].' and cntn in("MORU5805134","VERU4752203")';
//             $res_updt=msql_query($sql_updt,$link_vcrx);
          }
        
          if($fecha>="2020-06-16")
          {
//             $sql_updt='update '.$tabla.'2 set puni=19.50, dcto=0 where idnum2='.$idnum.' and iddet='.$aso_docu['iddet'].' and cntn in('.$xcntns.')';
//             $res_updt=msql_query($sql_updt,$link_vcrx);
          }
        
          if($fecha>="2021-06-16")
          {
            $sql_updt='update '.$tabla.'2 set puni=19.50, dcto=0 where idnum2='.$idnum.' and iddet='.$aso_docu['iddet'].' and cntn not in('.$xcntns.')';
            $res_updt=msql_query($sql_updt,$link_vcrx);
          }
        
        
        } 
      }      
      
    }
    
  }
  
  
}
?>