 <?php 
// session_start();
foreach($_GET as $key => $value){${$key}=trim(str_replace("'","\'",str_replace('"','\"',$value))); $_PARAMS[$key]=${$key};} 
foreach($_POST as $key => $value){${$key}=trim(str_replace("'","\'",str_replace('"','\"',$value))); $_PARAMS[$key]=${$key};} 

foreach($_POST as $campo => $valor)
{
}

include_once('lib_main.php');
if(!empty($empr)){$link_data=msql_connect('','','',$empr); $cfg_empr=$empr;}
$user_codigo=$_SESSION['user_codigo'];
$user0=data_maes('user',$user_codigo);

$empr0=data_maes('empr',$cfg_empr);
$vhcl=strtoupper($vhcl);
$plvh=strtoupper($plvh);

$ipip=$_SERVER['REMOTE_ADDR'];


// echo '*/*/*/*/*/'.$fecha1.'---'.$hora1;


$dom = new DOMDocument();
     $dom->encoding = 'utf-8';
     $dom->xmlVersion = '1.0';
     $dom->formatOutput = true;

$error_save='';
$error_save2='';



$response=$dom->appendChild($dom->createElement("response"));
$msgswal=$response->appendChild($dom->createElement("msgswal")); 
$msgswal->setAttribute("id","msgswal");

// $link_data=msql_connect('','','',$empr);

$docu=strtoupper($docu);
$docu0=data_maes('docu',$docu);
$docu=$docu0['docu_codigo'];
$tabla=$docu0['docu_tabla'];
$tabla1=$tabla.'1';
$tabla2=$tabla.'2';


$ptoem=str_replace('.','',$ptoem);
$ptoem=str_replace('-','',$ptoem);

if(!empty($item)){if(!ctype_digit($item)){$error_save.='<br>Error en ITEM';}}
if(!empty($enti)){if(!ctype_digit($enti)){$error_save.='<br>Error en ENTIDAD';}}
if(!empty($ctas)){if(!ctype_digit($ctas)){$error_save.='<br>Error en CUENTA CONTABLE';}}


if(empty($tabla)){$error_save='Docu tabla 1 - '.$docu;}

$user_acce=0;
if(@mb_stristr2('.sist.supe',$user0['user_tipo']))
{$user_acce=1;}
else
{
     $sql_acce ='select * ';
     $sql_acce.='from acce1 ';
     $sql_acce.='left join acce2 on idnum1=idnum2 ';
     $sql_acce.='left join docu0 on docum=docu_codigo ';
     $sql_acce.='where numero='.$user_codigo.' ';
     $sql_acce.='and docu_tabla="'.$modu.'" ';
     $sql_acce.='and (nuev=1 or modi=1)';
     $res_acce=msql_query($sql_acce,$link_data);
     if(msql_num_rows($res_acce)!=0){$user_acce=1;}
}
$user_acce=1;

if(empty($user_acce)){$error_save.='/NO TIENE ACCESO A GRABAR ';}


if(empty($docu)){$error_save.='/DOCUMENTO NO VALIDO ';}
if($docu==$docum){$error_save.='/DOCUMENTO DETALLE NO VALIDO ';}

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

$fec1=date('Y-m-d',strtotime($fecha.' -1 month'));
$fec2=date('Y-m-d',strtotime($fecha.' +0 days'));



//carga items
{
     
if(!empty($enti) and !empty($numfac) and empty($item))
{
     $numfac_ok=0;
     if($docu=='EB10'){$tfac='vtas';}
     if($docu=='IB10'){$tfac='comp';}
     
     $sql_fact ='select * from '.$tfac.'1 ';
     $sql_fact.='where ifnull(status,"")not in("O","X") ';
     $sql_fact.='and enti like "'.substr($enti,0,10).'%" ';
     $sql_fact.='and numero*1 = '.$numfac.' ';
     $sql_fact.='order by fecha desc ';
     $sql_fact.='limit 1 ';
     $res_fact=msql_query($sql_fact,$link_data);
     
     if(msql_num_rows($res_fact)!=0)
     {
          $numfac_ok=1;
          $idfact=msql_result($res_fact,0,'idnum1');
          
          $sql_fact ='select * from '.$tfac.'1 ';
          $sql_fact.='left join '.$tfac.'2 on idnum1=idnum2 ';
          $sql_fact.='where idnum1="'.$idfact.'" ';
          $sql_fact.='and fecha>="'.$fec1.'" and fecha<="'.$fec2.'" ';
//           if($docu=='IB10')
//           {$sql_fact.='and item in(select item_codigo from item0 where item_codigo like "20%") ';}
          if($docu=='EB10')
          {$sql_fact.='and item in(select distinct item from inve1 left join inve2 on idnum1=idnum2 where status not in("O","X") and docu like "IB%") ';}
          $sql_fact.='order by iddet ';
          $res_fact=msql_query($sql_fact,$link_data);
          
          $item=msql_result($res_fact,0,'item');
          $cant=msql_result($res_fact,0,'cant');
          
     }
     
     if(empty($numero) and empty($item))
     {
          $item='9999999999';
          $cant=1;
     }
     
     
}

if(!empty($enti) and !empty($numtra))
{
     $numfac_ok=0;
     $tfac='inve';
     
     $sql_fact ='select * from '.$tfac.'1 ';
     $sql_fact.='where ifnull(status,"")not in("O","X") ';
     $sql_fact.='and enti like "'.substr($enti,0,10).'%" ';
     $sql_fact.='and docu = "EB13" ';
     $sql_fact.='and bode = "'.$bode2.'" ';
     $sql_fact.='and numero*1 = '.$numtra.' ';
     $sql_fact.='order by fecha desc ';
     $sql_fact.='limit 1 ';
     $res_fact=msql_query($sql_fact,$link_data);
     
     if(msql_num_rows($res_fact)!=0)
     {
          $numfac_ok=1;
          $idfact=msql_result($res_fact,0,'idnum1');
          
          $sql_fact ='select * from '.$tfac.'1 ';
          $sql_fact.='left join '.$tfac.'2 on idnum1=idnum2 ';
          $sql_fact.='where idnum1="'.$idfact.'" ';
          $sql_fact.='order by iddet ';
          $res_fact=msql_query($sql_fact,$link_data);
          
          $item=msql_result($res_fact,0,'item');
          $cant=msql_result($res_fact,0,'cant');
          
     }
     
     if(empty($numero) and empty($item))
     {
          $item='9999999999';
          $cant=1;
     }
}

if(!empty($enti) and !empty($numsol))
{
     $numfac_ok=0;
     $tfac='prov';
     
     $sql_fact ='select * from '.$tfac.'1 ';
     $sql_fact.='where ifnull(status,"")not in("O","X") ';
//      $sql_fact.='and status = "A" ';
     $sql_fact.='and docu like "PS%" ';
     $sql_fact.='and numero*1 = '.$numsol.' ';
     $sql_fact.='order by fecha desc ';
     $sql_fact.='limit 1 ';
     $res_fact=msql_query($sql_fact,$link_data);
     
     if(msql_num_rows($res_fact)!=0)
     {
          $numfac_ok=1;
          $idfact=msql_result($res_fact,0,'idnum1');
          
          $sql_fact ='select * from '.$tfac.'1 ';
          $sql_fact.='left join '.$tfac.'2 on idnum1=idnum2 ';
          $sql_fact.='where idnum1="'.$idfact.'" ';
          $sql_fact.='order by iddet ';
          $res_fact=msql_query($sql_fact,$link_data);
          
          $item=msql_result($res_fact,0,'item');
          $cant=msql_result($res_fact,0,'cant');
          
     }
     
     if(empty($numero) and empty($item))
     {
          $item='9999999999';
          $cant=1;
     }
}

}





if(!empty($docum) and !empty($numer))
{
     $numfac_ok=0;
     if(substr($docu,0,2)=='GR')
     {
          $numfac=$numer;
          $tfac='vtas';
          if(substr($docum,0,1)=='P'){$tfac='clie';}
          if(substr($docum,0,2)=='EB'){$tfac='inve';}
          
          $sql_fact ='select * from '.$tfac.'1 ';
//           $sql_fact.='left join '.$tfac.'2 on idnum1=idnum2 ';
          $sql_fact.='where ifnull(status,"")not in("O","X") ';
//           $sql_fact.='and enti like "'.substr($enti,0,10).'%" ';
          $sql_fact.='and docu ="'.$docum.'"';
          $sql_fact.='and numero*1 = '.$numfac.' ';
          $sql_fact.='order by fecha desc ';
          $sql_fact.='limit 1 ';
          $res_fact=msql_query($sql_fact,$link_data);
          

          if(msql_num_rows($res_fact)!=0)
          {
               $numfac_ok=1;
               $idfact=msql_result($res_fact,0,'idnum1');
               $sql_fact ='select * from '.$tfac.'1 ';
               $sql_fact.='left join '.$tfac.'2 on idnum1=idnum2 ';
               $sql_fact.='where idnum1="'.$idfact.'" ';
               $sql_fact.='order by iddet ';
               $res_fact=msql_query($sql_fact,$link_data);
               $item=msql_result($res_fact,0,'item');
               $cant=msql_result($res_fact,0,'cant');
          
          }

          if(empty($numero) and empty($item))
          {
               $item='9999999999';
               $cant=1;
          }
     }
}


// crea y/o actualiza maestros
if($user_acce==1)
{
//      if(mb_stristr2('.vtas.clie',$tabla))
     
     if(mb_stristr2('.vtas.clie.carr',$tabla) and substr($enti,0,2)!='99')
     {
          if(!empty($enti) and !empty($nombre))
          {
               $nombre=strtoupper($nombre);
               $sql_maes ='update enti0 set ';
               $sql_maes.='enti_descri=case ';
               $sql_maes.='when(ifnull(enti_descri,"")="" or enti_descri like "%***%") then "'.$nombre.'" else enti_descri end, ';     
               $sql_maes.='enti_razsoc=case ';
               $sql_maes.='when(ifnull(enti_razsoc,"")="" or enti_razsoc like "%***%") then "'.$nombre.'" else enti_razsoc end, ';     
               $sql_maes.='enti_nomcom=case ';
               $sql_maes.='when(ifnull(enti_nomcom,"")="" or enti_nomcom like "%***%") then "'.$nombre.'" else enti_nomcom end, ';     
               $sql_maes.='enti_status=enti_status ';
               $sql_maes.='where enti_codigo like "'.substr($enti,0,10).'%" ';
               $res_maes=msql_query($sql_maes,$link_data);
          }

          if(!empty($enti) and !empty($wsapp))
          {
               $sql_maes ='update enti0 set ';
               $sql_maes.='enti_wsapp=case ';
               $sql_maes.='when ifnull(enti_wsapp,"")="" or enti_wsapp not like "09%" then "'.$wsapp.'" else enti_wsapp end, ';     
               $sql_maes.='enti_status=enti_status ';
               $sql_maes.='where enti_codigo like "'.substr($enti,0,10).'%" ';
               $res_maes=msql_query($sql_maes,$link_data);
          }

          if(!empty($enti) and !empty($email))
          {
               $sql_maes ='update enti0 set ';
               $sql_maes.='enti_email=case ';
               $sql_maes.='when(ifnull(enti_email,"")="" or enti_email not like "%@%") then "'.$email.'" else enti_email end, ';     
               $sql_maes.='enti_status=enti_status ';
               $sql_maes.='where enti_codigo like "'.substr($enti,0,10).'%" ';
               $res_maes=msql_query($sql_maes,$link_data);
          }

          if(!empty($enti) and !empty($direccion))
          {
               $sql_maes ='update enti0 set ';
               $sql_maes.='enti_direccion=case ';
               $sql_maes.='when(ifnull(enti_direccion,"")="") then "'.$direccion.'" else enti_direccion end ';     
               $sql_maes.='where enti_codigo like "'.substr($enti,0,10).'%" ';
               $res_maes=msql_query($sql_maes,$link_data);
          }
     }

     if(mb_stristr2('.guia',$tabla) and substr($enti,0,2)!='99')
     {
          if(empty($cedula2)){$cedula2=$enti;}
          if(empty($nombre2))
          {
              $cedula20=data_maes('enti',$cedula2);
              $nombre2=$cedula20['enti_descri'];
          }
          
          if(!empty($cedula1) and !empty($nombre1))
          {
               $nombre1=strtoupper($nombre1);
               $sql_maes ='update enti0 set ';
               $sql_maes.='enti_descri=case ';
               $sql_maes.='when(ifnull(enti_descri,"")="" or enti_descri like "%***%") then "'.$nombre1.'" else enti_descri end, ';     
               $sql_maes.='enti_razsoc=case ';
               $sql_maes.='when(ifnull(enti_razsoc,"")="" or enti_razsoc like "%***%") then "'.$nombre1.'" else enti_razsoc end, ';     
               $sql_maes.='enti_nomcom=case ';
               $sql_maes.='when(ifnull(enti_nomcom,"")="" or enti_nomcom like "%***%") then "'.$nombre1.'" else enti_nomcom end, ';     
               $sql_maes.='enti_status=enti_status ';
               $sql_maes.='where enti_codigo like "'.substr($cedula1,0,10).'%" ';
               $res_maes=msql_query($sql_maes,$link_data);
          }
          if(empty($nombre1))
          {
              $cedula10=data_maes('enti',$cedula1);
              $nombre1=$cedula10['enti_descri'];
          }
          

     }

     
     if(1==2)
     if(mb_stristr2('.vtas.clie.carr.guia',$tabla) and substr($enti,0,2)!='99')
     {
          if(!empty($vhcl) and !empty($enti))
          {
               $sql_maes ='update vhcl0 set ';
               $sql_maes.='vhcl_enti="'.$enti.'" ';
               $sql_maes.='where vhcl_codigo like "'.$vhcl.'%" ';
               $res_maes=msql_query($sql_maes,$link_data);
          }
          
          if(!empty($vhcl) and !empty($iser))
          {
               $sql_maes ='update vhcl0 set ';
               $sql_maes.='vhcl_item="'.$iser.'" ';
               $sql_maes.='where vhcl_codigo like "'.$vhcl.'%" ';
               $res_maes=msql_query($sql_maes,$link_data);
          }
          
          if(!empty($vhcl) and !empty($wsapp))
          {
               $sql_maes ='update vhcl0 set ';
               $sql_maes.='vhcl_wsapp="'.$wsapp.'" ';
               $sql_maes.='where vhcl_codigo like "'.$vhcl.'%" ';
               $res_maes=msql_query($sql_maes,$link_data);
          }
          
     }
     
     if(mb_stristr2('.vtas.clie.carr.guia',$tabla) and substr($enti,0,2)!='99')
     {
          if(!empty($plvh) and !empty($enti))
          {
               $sql_maes ='update plvh0 set ';
               $sql_maes.='plvh_enti="'.$enti.'" ';
               $sql_maes.='where plvh_codigo like "'.$plvh.'%" ';
               $res_maes=msql_query($sql_maes,$link_data);
          }
          
          if(!empty($plvh) and !empty($iser))
          {
               $sql_maes ='update plvh0 set ';
               $sql_maes.='plvh_item="'.$iser.'" ';
               $sql_maes.='where plvh_codigo like "'.$plvh.'%" ';
               $res_maes=msql_query($sql_maes,$link_data);
          }
          
          if(!empty($plvh) and !empty($wsapp))
          {
               $sql_maes ='update plvh0 set ';
               $sql_maes.='plvh_wsapp="'.$wsapp.'" ';
               $sql_maes.='where plvh_codigo like "'.$plvh.'%" ';
               $res_maes=msql_query($sql_maes,$link_data);
          }
          
     }
     
     
     if(!empty($item) and !empty($producto))
     {
          $sql_maes ='update item0 set ';
          $sql_maes.='item_descri=case ';
          $sql_maes.='when(ifnull(item_descri,"")="") or item_descri like "%***%" then "'.$producto.'" else item_descri end ';     
          $sql_maes.='where item_codigo like "'.$item.'" ';
          $res_maes=msql_query($sql_maes,$link_data);
     }
}

if($tabla=='liup')
  {
     if (empty($jugador) and empty($posi))
        {
           $error_save.='DATOS DE JUGADOR EN BLANCO ';
        }

     if (!empty($jugador) and empty($posi))
        {
           $error_save.='POSICION NO PUEDE ESTAR VACIA ';
        }

     if (empty($jugador) and !empty($posi))
        {
           $error_save.='JUGADOR NO PUEDE ESTAR VACIA ';
        }
  }


if(empty($enti) and !empty($codt))
{$enti=$codt;}

if(empty($item) and !empty($iser))
{$item=$iser;}

if(empty($item) and !empty($park))
{$item=$park;}


if(empty($enti)){$enti='9999999999999';}

if(!empty($enti))
{
     $sql_enti='select * from enti0 where left(enti_codigo,10) like "'.substr($enti,0,10).'%" order by enti_codigo limit 1';
//      $sql_enti='select * from enti0 where enti_codigo = "'.$enti.'" order by enti_codigo limit 1';
     $res_enti=msql_query($sql_enti,$link_data);
     $enti0=msql_result($res_enti,0,'enti_codigo');
     if(empty($enti0)){$error_save.='<br>Error en ENTI '.$enti;}          
}


if(!empty($item) and substr($docu,0,2)=='PS')
{
     $itemce=$item;
     $cant0=$cant;
     $cant1=$cant;
     $puni=1;
//      $puni=999999.99;
     if(substr($item,4)!='9999')
     {
          
          $sql_pcp1='select * from comp1 left join comp2 on idnum1=idnum2 where ifnull(status,"")not in("O","X") and item="'.$item.'" order by fecha desc limit 1';
          $res_pcp1=msql_query($sql_pcp1,$link_data);
          if(msql_num_rows($res_pcp1)==0)
          {
               $sql_pcp1='select * from inve1 left join inve2 on idnum1=idnum2 where ifnull(status,"")not in("O","X") and item="'.$item.'" order by fecha desc limit 1';
               $res_pcp1=msql_query($sql_pcp1,$link_data);
          }
          if(msql_num_rows($res_pcp1)!=0)
          {$puni=msql_result($res_pcp1,0,'puni');}
     }
     
}


if($docu=='IB17')
{
     $reval=0;
     if($puni>(1/1000000))
     {$reval=$puni;}
     
     $puni=(1/1000000);
//      $puni=$reval;
}

if(mb_stristr2('.IE10.ICH1.DC10.IV10',$docu))
{if(substr($docum,0,2)=='AC'){$docum='AC10';$numer=0;$ctas='';}}

if(mb_stristr2('.EE10.CH10.DP10.EV10',$docu))
{if(substr($docum,0,2)=='AP'){$docum='AP10';$numer=0;$ctas='';}}


$deta_ok=1;

if(mb_stristr2('.vtas.clie.carr.comp.prov.lpro.',$tabla) and (empty($cant) and empty($puni))){$deta_ok=0;}
if(mb_stristr2('.vtas.clie.carr.comp.prov.lpro.inve.infi.pduc.prXod.opro.ppro.otXbj',$tabla) and empty($item)){$deta_ok=0;}

// if(mb_stristr2('.caja.bcos.tsor.dxco.dxpa.',$tabla) and empty($docum) and empty($ctas)){$deta_ok=0;}
if(mb_stristr2('.caja.bcos.tsor.dxco.dxpa.',$tabla) and empty($cant)){$deta_ok=0;}

// if(mb_stristr2('.caja.bcos.tsor.dxco.dxpa.',$tabla) and empty($docum) and empty($ctas) and empty($caba2)){$deta_ok=0;}

if(mb_stristr2('.caja.bcos.tsor',$tabla))
{
     if(!mb_stristr2('.DP90',$docu))
     if(empty($docum) and empty($ctas) and empty($caba2)){$deta_ok=0;}
}

if(mb_stristr2('.dxco.dxpa.',$tabla))
{if(empty($docum) and empty($ctas)){$deta_ok=0;}}

// if(mb_stristr2('.DP10.DP90',$docu) and !empty($cant)){$deta_ok=1;}


if(mb_stristr2('.guia.',$tabla) and empty($item) and empty($docum) and empty($numer) ){$deta_ok=0;}
// if(mb_stristr2('.rmed.',$tabla) and empty($cant) and empty($frecuencia) and empty($duracion) and empty($prescripcion) ){$deta_ok=0;}
if(mb_stristr2('.rmed.',$tabla) and empty($cant) and empty($farmaco.$medicamento.$prescripcion) and empty($frecuencia) and empty($duracion)  ){$deta_ok=0;}

if(mb_stristr2('.jwrv.',$tabla) and empty($trty) and empty($bloq) and empty($ndpto) ){$deta_ok=0;}
if(mb_stristr2('.jwrt.',$tabla) and empty($trty) ){$deta_ok=0;}
if(mb_stristr2('.jwmc.',$tabla) and empty($lugar) ){$deta_ok=0;}

if($tonume*1>0){$tonume=str_pad($tonume,9,'0',STR_PAD_LEFT);}


if($tabla=='jwmc')
{
     $ptoe='001001';
     $lugar=strtoupper($lugar);
     $nombre1=strtoupper($nombre1);
     $nombre2=strtoupper($nombre2);
     if($docu!='MDC5' and empty($lugar)){$error_save.='<br>Error en LUGAR ';}
     if($docu=='MDC5' and empty($trty)){$error_save.='<br>Error en TERRITORIO ';}
//      if($modo!=4)
     {
     if(strlen($nombre1)<4){$error_save.='<br>Error en CONDUCTOR ';}
     if($nombre1==$nombre2){$error_save.='<br>Error en AUXILIAR ';}
     }
}


if($tabla=='jwrv' or $tabla=='jwrt')
{
     $ptoe='001001';
     $publicador=strtoupper($publicador);
     $asignado=strtoupper($asignado);
     if(empty($cong)){$cong=$_SESSION['cong_codigo'];}
     
     if(empty($cong)){$error_save.='<br>Error en CONGREGACION ';}
     if(empty($trty)){$error_save.='<br>Error en TERRITORIO ';}
     
     if($tabla=='jwrt')
     {
          if(strlen($asignado)<4){$error_save.='<br>Error en ASIGNADO ';}
     }
     
     if($tabla=='jwrv')
     {
//           if($visita=='NC'){$etario='N/A';}
          if(mb_stristr2('.NC.DH.NT',$visita)){$etario='N/A';}
          if(strlen($publicador)<4){$error_save.='<br>Error en PUBLICADOR ';}
          if(empty($bloq)){$error_save.='<br>Error en MZ / BLQ ';}
          if(empty($ndpto)){$error_save.='<br>Error en NUMERO / DPTO ';}
     }
}



if($tabla=='btcr')
{
     $hora1=date('H:i:s');
     $hora=date('H:i:s');
//      $fechora=date('Y-m-d')
     
     $nombre1=strtoupper($nombre1);
//      if(substr($enti,0,4)=='9999'){$enti='';}
     if(substr($enti,0,4)=='9999'){$enti='9999999999';}
     
     if($docu=='BTC1')
     {
          if(empty($enti) and empty($plvh)){$error_save.='<br>Error en CEDULA / VEHICULO';}
          elseif(empty($enti)){$error_save.='<br>Error en CEDULA';}
          
          if(empty($ppdd)){$error_save.='<br>Error en DESTINO';}
     }
     
     if($docu=='BTC2')
     {
          if(empty($enti) and empty($plvh)){$error_save.='<br>Error en CEDULA / VEHICULO';}
//           elseif(empty($enti)){$error_save.='<br>Error en CEDULA';}
//           if(empty($ppdd)){$error_save.='<br>Error en DESTINO';}
     }
     
}

if($tabla=='avso')
{
//      $hora1=date('H:i:s');
     $nombre1=strtoupper($nombre1);
     if(substr($enti,0,4)=='9999'){$enti='';}
     if(empty($enti) and empty($plvh)){$error_save.='<br>Error en CEDULA / VEHICULO';}
     elseif(empty($enti)){$error_save.='<br>Error en CEDULA';}
     
}



if($tabla=='cont')
{
     $ptoe='001001';
     if(empty($ctas)){$error_save.='<br>Error en CUENTA CONTABLE';}
     else
     {
          $sql_ctas='select * from ctas0 where ctas_codigo like "'.$ctas.'%" order by ctas_codigo limit 1';
          $res_ctas=msql_query($sql_ctas,$link_data);
          $ctas0=msql_result($res_ctas,0,'ctas_codigo');
          if(empty($ctas0)){$error_save.='<br>Error en CUENTA CONTABLE '.$ctas;}          
     }

     if(strlen($ctas)<5){$error_save.='<br>Error en CUENTA CONTABLE : '.$ctas.' NO es cuenta de DETALLE';}
}    



if($tabla=='vtas' or $tabla=='clie')
{
     if(empty($ptov)){$error_save.='<br>Error en PTO. VENTA';}
     else
     {
          $sql_ptov='select * from ptov0 where ptov_codigo like "'.$ptov.'%" order by ptov_codigo limit 1';
          $res_ptov=msql_query($sql_ptov,$link_data);
          $ptov0=msql_result($res_ptov,0,'ptov_codigo');
          if(empty($ptov0)){$error_save.='<br>Error en PTO. VENTA '.$ptov;}          
     }
     
     if(empty($enti)){$error_save.='<br>Error en ENTIDAD ';}
     else
     {
          $sql_enti='select * from enti0 where left(enti_codigo,10) = "'.substr($enti,0,10).'" order by enti_codigo limit 1';
          $res_enti=msql_query($sql_enti,$link_data);
          $enti0=msql_result($res_enti,0,'enti_codigo');
          if(empty($enti0)){$error_save.='<br>Error en ENTIDAD '.$enti;}          
     }
     
     if(empty($item)){$error_saveX.='<br>Error en ITEM ';}
     else
     {
          $item0=data_maes('item',$item);
          if(empty($item0['item_codigo'])){$error_save.='<br>Error en ITEM '.$item;}
          $item=$item0['item_codigo'];
     }
     
     if($envio>0)
     {
          $st1=$cant*$puni*(1-$dcto/100)+$envio;
          $st2=$st1+($cant*$puni*$dcto/100);
          $dcto=(1-($st1/$st2))*100;
          $puni=$st2/$cant;
//           $sql_updt='';
//           $sql_updt.='update '.$tabla.'2 set ';
//           $sql_updt.='puni="'.$puni.'",';
//           $sql_updt.='dcto="'.$dcto.'",';
//           $sql_updt.='idate2=now() ';
//           $sql_updt.='where idnum2='.$idnum1;
//           $sql_updt.=' and iddet='.$iddet;
//           $res_updt=@msql_query($sql_updt,$link_vcrx);
     }
     
     
}    



if($tabla=='comp' or $tabla=='prov' or $tabla=='lpro')
{
    if($docu=='DA16'){$ptoe='001001';}
    
     $csub0=data_maes('csub',$csub);
//      if(substr($docu,0,2)=='LP')
     if($tabla=='lpro')     
     {
          $csub1=$csub;
          $toenti=$enti;
          $ptoe=$csub0['csub_ptoe'];
          if(empty($ptoe)){$ptoe='001001';}
     }
     
     if(!empty($fecha2))
     {$dias=days_diff($fecha1,$fecha2);}
     

     if($docu=='FP15')
     {
          if(empty($puni)){$puni=$puni1;}
          if(empty($puni1)){$puni1=$puni;}
          
          if(empty($impor)){$error_save.='<br>Error en NUM.IMPORTACION';}
          
          if(!empty($impor))
          {
               $sql_impo='select * from comp1 where ifnull(status,"")not in ("O","X") and docu ="'.$docu.'" and impor="'.$impor.'" ';
               $res_impo=msql_query($sql_impo,$link_data);
               if(msql_num_rows($res_impo)>1)
               {$error_save.='<br>Error NUM.IMPORTACION YA EXSITE';}
          }
          
     }
     
     
     if(empty($ptoe)){$error_save.='<br>Error en PTO. EMISION';}
     
     if(empty($enti)){$error_save.='<br>Error en ENTIDAD ';}
     else
     {
          $enti=substr($enti,0,10);     
          if($docu!='FP03' and $docu!='FP11' and substr($enti,0,2)!='99'){$enti.='001';}

          $sql_enti='select * from enti0 where left(enti_codigo,10) = "'.substr($enti,0,10).'" order by enti_codigo desc limit 1';
          $res_enti=msql_query($sql_enti,$link_data);
          $enti0=msql_result($res_enti,0,'enti_codigo');
          if(empty($enti0)){$error_save.='<br>Error en ENTIDAD '.$enti;}          
     }
     
//      if($docu!='FP90' and substr($enti,0,10)==str_repeat('9',10))
     if(!mb_stristr2('.FP90.PS10',$docu) and substr($enti,0,10)==str_repeat('9',10))
     {$error_save.='<br>Error en ENTIDAD Consumidor Final ';}
     
     if($tabla=='comp' and $docu!='FP03' and $docu!='FP90')
     {if(empty($numero)){$error_save.='<br>Error en NUMERO ';}}
}    


if(@mb_stristr2('.IE10.DC10',$docu))
{
     if(empty($rcdd)){$rcdd=get_rcdd($enti);}
}

if(@mb_stristr2('.AC11.AP11.FP16',$docu))
{
     $caja='001001';
     $ptoe='001001';
}


if($tabla=='dxco' or $tabla=='dxpa')
{
     $ptoe='001001';
}     

if($tabla=='caja')
{
     
     if(empty($caja)){$error_save.='<br>Error en CAJA';}
     else
     {
          $caja0=data_maes('caja',$caja);
          if(empty($caja0['caja_codigo'])){$error_save.='<br>Error en CAJA: '.$caja;}
     }
     
     if(substr($docu,2,13)=='13' and empty($caba2)){$error_save.='<br>Error en DESTINO '.$caba2;}
     
     if(!empty($caba2))
     {if($caja==$caba2){$error_save.='<br>Error en CAJA/BCOS '.$caba2;}}
}    

if($tabla=='bcos')
{
     if(empty($ctab)){$error_save.='<br>Error en CTA.BANC.';}
     else
     {
          $ctab0=data_maes('ctab',$ctab);
          if(empty($ctab0['ctab_codigo'])){$error_save.='<br>Error en CTA.BANC. '.$ctab;}
          $ptoe=$ctab0['ctab_ptoe'];
     }
     
     if(substr($docu,2,13)=='13' and empty($caba2)){$error_save.='<br>Error en DESTINO '.$caba2;}
     
     if(!empty($caba2))
     {if($ctab==$caba2){$error_save.='<br>Error en CAJA/BCOS '.$caba2;}}
     
     
     if(substr($docum,0,2)=='FC' and (int)$numer>0)
     {
          $sql_fact='select * from vtas1 where ifnull(status,"")not in("X") and docu like "FC%" and left(enti,10)="'.substr($enti,0,10).'" and numero*1="'.(int)$numer.'" limit 1';
          $res_fact=msql_query($sql_fact,$link_data);
//           if(msql_num_rows($res_fact)==0)
//           {$error_save.='<br>Error en CLIENTE / FACTURA ';}
     }
     
     if(substr($docum,0,2)=='FP' and (int)$numer>0)
     {
          $sql_fact='select * from vtas1 where ifnull(status,"")not in("X") and docu like "FP%" and left(enti,10)="'.substr($enti,0,10).'" and numero*1="'.(int)$numer.'" limit 1';
          $res_fact=msql_query($sql_fact,$link_data);
//           if(msql_num_rows($res_fact)==0)
//           {$error_save.='<br>Error en PROVEEDOR / FACTURA ';}
     }
     
     
}    

if($tabla=='caja' or $tabla=='bcos' )
{
     
     if(empty($docum)){$ptoem='';$numer='';}
     
     if(!empty($docum) and !empty($ctas))
     {$error_save.='<br>Error en DETALLE ==> Documento ? o Cuenta ?';}
     
     
     if(empty($enti)){$error_save.='<br>Error en ENTIDAD ';}
     else
     {
          $sql_enti='select * from enti0 where left(enti_codigo,10) = "'.substr($enti,0,10).'" order by enti_codigo limit 1';
          $res_enti=msql_query($sql_enti,$link_data);
          $enti0=msql_result($res_enti,0,'enti_codigo');
          if(empty($enti0)){$error_save.='<br>Error en ENTIDAD '.$enti;}          
     }
}    

if($tabla=='dxco'  or $tabla=='dxpa' )
{
     
     if(empty($docum)){$ptoem='';$numer='';}
     
     if(!empty($docum) and !empty($ctas))
     {$error_save.='<br>Error en DETALLE ==> Documento ? o Cuenta ?';}
     
     
     if(empty($enti)){$error_save.='<br>Error en ENTIDAD ';}
     else
     {
          $sql_enti='select * from enti0 where left(enti_codigo,10) = "'.substr($enti,0,10).'" order by enti_codigo limit 1';
          $res_enti=msql_query($sql_enti,$link_data);
          $enti0=msql_result($res_enti,0,'enti_codigo');
          if(empty($enti0)){$error_save.='<br>Error en ENTIDAD '.$enti;}          
     }
}    




if($tabla=='inve')
{
     if(empty($bode)){$error_save.='<br>Error en BODEGA';}
     
     if($docu=='EB13' and ((!empty($bode) and empty($bode2)) or $bode==$bode2 ) )
     {$error_save.='<br>Error en BODEGA DESTINO ';}
     
//      if($numfac_ok==0)
     if(empty($numero) and empty($item) and empty($cant))
     {
          $error_save.='<br>Error NO HAY DETALLE ';
          if(empty($item)){$error_save.='/ITEM ';}
          if(empty($cant)){$error_save.='/CANTIDAD ';}
     }
     
     if(!empty($plvh))
     if(empty($enti) or substr($enti,0,4)=='9999')
     {
          $sql_dato ='select * from tabla1 left join tabla2 on idnum1=idnum2 ';
          $sql_dato.='where ifnull(status,"")not in ("O","X") and plvh="'.$plvh.'" order by idnum1 desc limit 1';
          
          $sql_plvh=str_replace('tabla','vtas',$sql_dato);
          $res_plvh=msql_query($sql_plvh,$link_data);
          
          if(msql_num_rows($res_plvh)==0)
          {
               $sql_plvh=str_replace('tabla','clie',$sql_dato);
               $res_plvh=msql_query($sql_plvh,$link_data);
          }
          
          if(msql_num_rows($res_plvh)==0)
          {
               $sql_plvh=str_replace('tabla','inve',$sql_dato);
               $res_plvh=msql_query($sql_plvh,$link_data);
          }
               
          if(msql_num_rows($res_plvh)!=0)
          {$enti=msql_result($res_plvh,0,'enti');}
          
     }
     
     
}

if($tabla=='infi')
{
     if(empty($bode)){$error_save.='<br>Error en BODEGA';}

     $enti=$user0['user_cedula'];
     if(empty($enti)){$enti=$empr0['empr_ruc'];}
     if(empty($enti)){$enti='9999999999';}
     
     if(empty($numero) and empty($item) and empty($cant))
     {
          $error_save.='<br>Error NO HAY DETALLE ';
          if(empty($item)){$error_save.='/ITEM ';}
          if(empty($cant)){$error_save.='/CANTIDAD ';}
     }
}


if($tabla=='pduc')
{
     $ptoe='001001';
//      if($entrada!=0){$cant=abs($entrada);}
//      if($salida!=0){$cant=abs($salida);}
     
     $cant=abs((float)$salida)-abs((float)$entrada);
          
//      if(empty($ctas)){$error_save.='<br>Error en CUENTA CONTABLE';}
//      else
//      {
//           $sql_ctas='select * from ctas0 where ctas_codigo like "'.$ctas.'%" order by ctas_codigo limit 1';
//           $res_ctas=msql_query($sql_ctas,$link_data);
//           $ctas0=msql_result($res_ctas,0,'ctas_codigo');
//           if(empty($ctas0)){$error_save.='<br>Error en CUENTA CONTABLE '.$ctas;}          
//      }

//      if(strlen($ctas)<5){$error_save.='<br>Error en CUENTA CONTABLE : '.$ctas.' NO es cuenta de DETALLE';}
}    





if($tabla=='susa')
{
    if(empty($enti)){$error_save.='<br>Error en EMPLEADO';}
    if(empty($csub)){$error_save.='<br>Error en CENTRO / COSTO';}
    if(empty($sueldo)){$error_save.='<br>Error EN SUELDO ';}
    $cant=1;
    $puni=$sueldo;
}


if($tabla=='nomi')
{
//      $fecha=date('Y-m-j')
     if(empty($empl)){$error_save.='<br>Error en EMPLEADO';}
//     if(empty($csub)){$error_save.='<br>Error en CENTRO / COSTO';}
//     if(empty($sueldo)){$error_save.='<br>Error EN SUELDO ';}
//     $cant=1;
//     $puni=$sueldo;
}


if($tabla=='opro')
{
     if(empty($numero) and empty($item) and empty($cant))
     {
          $error_save.='<br>Error NO HAY DETALLE ';
          if(empty($item)){$error_save.='/ITEM ';}
          if(empty($cant)){$error_save.='/CANTIDAD ';}
     }
}


if($tabla=='hcli')
{
     if(empty($fase)){$error_save.='/FASE NO VALIDA ';}
//      if(empty($numero) and empty($item) and empty($cant))
//      {
//           $error_save.='<br>Error NO HAY DETALLE ';
//           if(empty($item)){$error_save.='/ITEM ';}
//      }
}

if($tabla=='otbj')
{
     if(empty($ptov)){$error_save.='<br>Error en PTO. VENTA';}
     else
     {
          $sql_ptov='select * from ptov0 where ptov_codigo like "'.$ptov.'%" order by ptov_codigo limit 1';
          $res_ptov=msql_query($sql_ptov,$link_data);
          $ptov0=msql_result($res_ptov,0,'ptov_codigo');
          if(empty($ptov0)){$error_save.='<br>Error en PTO. VENTA '.$ptov;}          
     }
     
     $ptoe=$ptov0['ptov_ptoe'];
     $bode=$ptov0['ptov_bode'];
     
     if($fase=='trabajo' and empty($item)){$error_save.='Error en Trabajo ';}
          
     
     if(empty($fase)){$error_save.='/FASE NO VALIDA ';}
//      if(!empty($mcnc) and !ctype_digit($cadena)){$error_save.='/MECANICO NO VALIDO ';}
     if(empty($plvh) and empty($vinn)){$error_save.='/PLACA - VIN ';}
     
     if(strlen(trim($vinn))<17){$error_save.='/VIN ';}

     if(!empty($plvh) and !empty($vinn))
     {
          $sql_updt='update vinn0 set vinn_plvh="'.$plvh.'" where vinn_codigo="'.$vinn.'" and length(trim(ifnull(vinn_plvh,"")))<'.strlen($plvh).'';
          $res_updt=msql_query($sql_updt,$link_data);
          
          $sql_updt='update plvh0 set plvh_vinn="'.$vinn.'" where plvh_codigo="'.$plvh.'" and length(trim(ifnull(plvh_vinn,"")))<'.strlen($vinn).'';
          $res_updt=msql_query($sql_updt,$link_data);
          
          $plvh0=data_maes('plvh',$plvh);
          $vwmi=substr($vinn,0,3);
          inse_maes('vwmi',$vwmi,$plvh0['plvh_mrca']);
          
//           $error_save= '==== '.$wmi.' ** **** '.$plvh0['plvh_mrca'];
          
          
     }
     
     
//      if(empty($numero) and empty($item) and empty($cant))
//      {
//           $error_save.='<br>Error NO HAY DETALLE ';
//           if(empty($item)){$error_save.='/ITEM ';}
//      }
}




if($tabla=='guia')
{
     if(empty($ptoe)){$error_save.='<br>Error en PTO. EMISION';}
     else
     {
          $sql_ptoe='select * from ptoe0 where ptoe_codigo like "'.$ptoe.'%" order by ptoe_codigo limit 1';
          $res_ptoe=msql_query($sql_ptoe,$link_data);
          $ptoe0=msql_result($res_ptoe,0,'ptoe_codigo');
          if(empty($ptoe0)){$error_save.='<br>Error en PTO. EMISION '.$ptoe;}          
     }
     
     if(empty($enti)){$error_save.='<br>Error en ENTIDAD Destinatario ';}
     if(empty($cedula1)){$error_save.='<br>Error en ENTIDAD Conductor ';}
//      if(empty($docum) and empty($numer) and empty($item))
     if(empty($numero) and empty($docum) and empty($numer) and empty($item))
     {
          $error_save2.='<br>Error NO HAY DETALLE ';
          if(empty($docum)){$error_save2.='/Tipo Comp. ';}
          if(empty($numer)){$error_save2.='/# Comp. ';}
          if(empty($item)){$error_save2.='/ITEM ';}
     }
}    

if($tabla=='rmed')
{
     $ptoe='001001';
     
//      if(empty($ptoe)){$error_save.='<br>Error en PTO. EMISION';}
//      else
//      {
//           $sql_ptoe='select * from ptoe0 where ptoe_codigo like "'.$ptoe.'%" order by ptoe_codigo limit 1';
//           $res_ptoe=msql_query($sql_ptoe,$link_data);
//           $ptoe0=msql_result($res_ptoe,0,'ptoe_codigo');
//           if(empty($ptoe0)){$error_save.='<br>Error en PTO. EMISION '.$ptoe;}          
//      }
     
     $mdco=$user0['user_cedula'];
     $paciente=$nombre;
     
     if(!empty($enti))
     if(substr($enti,0,2)!='99')
     {
          $sql_updt='update enti0 ';
          $sql_updt.='set enti_descri="'.$nombre.'" ';
          $sql_updt.='where enti_codigo ="'.$enti.'" and (ifnull(enti_descri,"")="" or enti_descri like "%nueva%") ';
          $sql_updt.='limit 1';
          $res_updt=msql_query($sql_updt,$link_data);
          
          $sql_updt='update enti0 ';
          $sql_updt.='set enti_razsoc="'.$nombre.'" ';
          $sql_updt.='where enti_codigo ="'.$enti.'" and (ifnull(enti_razsoc,"")="" or enti_razsoc like "%nueva%") ';
          $sql_updt.='limit 1';
          $res_updt=msql_query($sql_updt,$link_data);
          
          $sql_updt='update enti0 ';
          $sql_updt.='set enti_nomcom="'.$nombre.'" ';
          $sql_updt.='where enti_codigo ="'.$enti.'" and (ifnull(enti_nomcom,"")="" or enti_nomcom like "%nueva%") ';
          $sql_updt.='limit 1';
          $res_updt=msql_query($sql_updt,$link_data);
          
          
          $sql_updt='update enti0 ';
          $sql_updt.='set enti_fechanac="'.$fechanac.'" ';
          $sql_updt.='where enti_codigo like "'.substr($enti,0,10).'%" ';
          $sql_updt.='limit 1';
          $res_updt=msql_query($sql_updt,$link_data);
          
          $sql_updt='update enti0 ';
          $sql_updt.='set enti_sexo="'.$sexo.'" ';
          $sql_updt.='where enti_codigo like "'.substr($enti,0,10).'%" ';
          $sql_updt.='limit 1';
          $res_updt=msql_query($sql_updt,$link_data);
          
     }
     
     $via=ucfirst2($via);
     $frecuencia=ucfirst2($frecuencia);
     $duracion=ucfirst2($duracion);
     
     $via=str_replace('Via ','',$via);
     $frecuencia=str_replace('Cada ','c/.',$frecuencia);
     
     if(empty($enti)){$error_save.='<br>Error en CEDULA PACIENTE  ';}
     if(empty($paciente)){$error_save.='<br>Error en NOMBRE PACIENTE  ';}
//      if(empty($farmaco.$medicamento.$prescripcion)){$error_save.='<br>Error en PRESCRIPCION  ';}
     if(empty($mdco)){$error_save.='<br>Error en MEDICO  ';}
     if(empty($ciex)){$error_save.='<br>Error en CIE-10  ';}
     if(empty($numero) and empty($docum) and empty($numer) and empty($item))
     {
//           $error_save2.='<br>Error NO HAY DETALLE ';
//           if(empty($docum)){$error_save2.='/Tipo Comp. ';}
//           if(empty($numer)){$error_save2.='/# Comp. ';}
//           if(empty($item)){$error_save2.='/ITEM ';}
     }
}    

if(!empty($item))
{
     
     $sql_item='select * from item0 where item_codigo like "'.$item.'%" order by item_codigo limit 1';
     $res_item=msql_query($sql_item,$link_data);
     $item0=msql_result($res_item,0,'item_codigo');
     
     if(empty($item0))
     {
          if(val_ean($item))
          {
               new_ean($item,$detalle);
               $item0=$item;
          }
     }
     
     if(empty($item0))
     {
          $sql_item='select * from item0 where item_ean = "'.$item.'" order by item_codigo limit 1';
          $res_item=msql_query($sql_item,$link_data);
          $item0=msql_result($res_item,0,'item_codigo');
          $item=msql_result($res_item,0,'item_codigo');
     }
     
     
     $item0=data_maes('item',$item);
     if(empty($item0['item_codigo'])){$error_save.='<br>Error en ITEM '.$item;}          
     
     if(!empty($item0['item_umdd']))
     {$umdd=$item0['item_umdd'];}
     
}
     
if(empty($error_save))
{
     if($tabla=='jwmc')
     {
          if($prms=='MES'){$fecha1=$mes.'-01'; $fecha2=date('Y-m-t',strtotime($fecha1));}
          if($prms=='SEM'){$fecha1=get_1diasema($sema); $fecha2=date('Y-m-d',strtotime($fecha1.'+6 days'));}
          
          $fechax=$fecha1;
          while($fechax<=$fecha2)
          {
//                $dia_ok=0;
//                if(date('N',strtotime($fechax))==$ndia){$dia_ok=1;}
               if(mb_stristr2($ndia,date('N',strtotime($fechax))))
               {
                    $sql_vali ='select * from jwmc1 ';
                    $sql_vali.='where ifnull(status,"")!="X" ';
                    $sql_vali.='and docu="'.$docu.'" ';
                    $sql_vali.='and cong="'.$_SESSION['user_cong'].'" ';
                    $sql_vali.='and fecha="'.$fechax.'" ';
                    $sql_vali.='and lugar like "'.substr($lugar,0,20).'%" ';
                    if($docu=='MDC5')
                    {$sql_vali.='and trty="'.$trty.'" ';}
                    $sql_vali.='and hora="'.$hora.'" ';
                    $res_vali=msql_query($sql_vali,$link_data);
                    
//                     echo '*/*/*/* '.$sql_vali;
                    
                    if(msql_num_rows($res_vali)==0)
                    {
                         $numero=new_numero($_PARAMS);
                         $sql_inse='insert into ';
                         $sql_inse.='jwmc1(docu,ptoe,numero,fecha,cong,lugar,trty,hora,iuser,idate) ';
                         $sql_inse.='value("'.$docu.'","'.$ptoe.'","'.$numero.'","'.$fechax.'","'.$_SESSION['user_cong'].'","'.$lugar.'","'.$trty.'","'.$hora.'","'.$user_codigo.'",now()) ';
                         $res_inse=msql_query($sql_inse,$link_data);

                         $res_vali=msql_query($sql_vali,$link_data);
                    }
                    
                    if(msql_num_rows($res_vali)!=0)
                    {
                         $idnum1=msql_result($res_vali,0,'idnum1');
                         $numero=msql_result($res_vali,0,'numero');
                    }
                    
                    $sql_updt='update jwmc1 set ';
                    $sql_updt.='nombre1=case when ifnull(nombre1,"")="" then "'.$nombre1.'" else nombre1 end, ';
                    $sql_updt.='nombre2=case when ifnull(nombre2,"")="" then "'.$nombre2.'" else nombre2 end, ';
                    $sql_updt.='where idnum1="'.$idnum1.'" ';
                    $res_updt=msql_query($sql_updt,$link_data);
//                     echo ' ==== '.$sql_updt;
                    
                    $deta_ok=0;
                    $sql_dele='delete from '.$tabla2.' where idnum2="'.$idnum1.'" ';
                    $res_dele=msql_query($sql_dele,$link_data);
                    
               }
               $fechax=date('Y-m-d',strtotime($fechax.'+1 day'));
          }
          
          return;
     }
     
     if($tabla=='jwrv' or $tabla=='jwrt')
     {
          if(empty($ptoe)){$ptoe='001001';}
          if(empty($puni)){$puni=1;}
          
          $sql_query ='select * from '.$tabla1.' ';
          $sql_query.='where status!="X" ';
          $sql_query.='and ifnull(numero,0)*1>0 ';
          $sql_query.='and docu="'.$docu.'" ';
          $sql_query.='and fecha="'.$fecha.'" ';
          $sql_query.='and cong="'.$cong.'" ';
          $sql_query.='and trty="'.$trty.'" ';
          if($tabla=='jwrv')
          {
               $sql_query.='and bloq="'.$bloq.'" ';
               $sql_query.='and ndpto="'.$ndpto.'" ';
               $sql_query.='and publicador="'.$publicador.'" ';
          }
          $sql_query.='order by idnum1 desc limit 1 ';
          $res_query=msql_query($sql_query,$link_data);

          if(msql_num_rows($res_query)==0)
          {
               $numero=new_numero($_PARAMS);
               $sql_inse ='insert into '.$tabla1.'(docu,ptoe,numero,fecha,enti,status,iuser,idate)';
               $sql_inse.='value("'.$docu.'","'.$ptoe.'","'.$numero.'","'.$fecha.'","'.$enti.'","-","'.$user_codigo.'",now())';
               $res_inse=msql_query($sql_inse,$link_data);
               $res_query=msql_query($sql_query,$link_data);
          }

          if(msql_num_rows($res_query)!=0)
          {
               $idnum1=msql_result($res_query,0,'idnum1');
               $numero=msql_result($res_query,0,'numero');
          }

          $deta_ok=0;
          $sql_dele='delete from '.$tabla2.' where idnum2='.$idnum1.' ';
          $res_dele=msql_query($sql_dele,$link_data);
     }
     
     
     if($tabla=='btcr' or $tabla=='avso')
     {
          if(empty($hora1)){$hora1=date('H:i');}
          if(empty($ptoe)){$ptoe='001001';}
          if(empty($puni)){$puni=1;}
          if(empty($numero) and $deta_ok==1){$numero=new_numero($_PARAMS);}
          
          $sql_query ='select * from '.$tabla1.' ';
          $sql_query.='where status!="X" ';
          $sql_query.='and ifnull(numero,0)*1>0 ';
          $sql_query.='and docu="'.$docu.'" ';
          $sql_query.='and ptoe="'.$ptoe.'" ';
//           $sql_query.='and fecha="'.$fecha.'" ';
          $sql_query.='and left(enti,10)="'.substr($enti,0,10).'" ';
          $sql_query.='and numero*1="'.(int)$numero.'" ';
          $sql_query.='order by idnum1 desc limit 1 ';
          $res_query=msql_query($sql_query,$link_data);

          
          if(msql_num_rows($res_query)==0)
          {
               $numero=new_numero($_PARAMS);
               $sql_inse ='insert into '.$tabla1.'(docu,ptoe,numero,fecha,enti,status,iuser,idate)';
               $sql_inse.='value("'.$docu.'","'.$ptoe.'","'.$numero.'","'.$fecha.'","'.$enti.'","-","'.$user_codigo.'",now())';
               $res_inse=msql_query($sql_inse,$link_data);
               $res_query=msql_query($sql_query,$link_data);
          }

          if(msql_num_rows($res_query)!=0)
          {
               $idnum1=msql_result($res_query,0,'idnum1');
               $numero=msql_result($res_query,0,'numero');
          }
     }

     if($tabla=='plan')
     {
          if(empty($ptoe)){$ptoe='001001';}
          if(empty($puni)){$puni=1;}
          if(empty($numero) and $deta_ok==1){$numero=new_numero($_PARAMS);}
          
          $sql_query ='select * from '.$tabla1.' ';
          $sql_query.='where status!="X" ';
          $sql_query.='and ifnull(numero,0)*1>0 ';
          $sql_query.='and docu="'.$docu.'" ';
          $sql_query.='and ptoe="'.$ptoe.'" ';
//           $sql_query.='and fecha="'.$fecha.'" ';
          $sql_query.='and left(enti,10)="'.substr($enti,0,10).'" ';
          $sql_query.='and numero*1="'.(int)$numero.'" ';
          $sql_query.='order by idnum1 desc limit 1 ';
          $res_query=msql_query($sql_query,$link_data);

          
          if(msql_num_rows($res_query)==0)
          {
               $numero=new_numero($_PARAMS);
               $sql_inse ='insert into '.$tabla1.'(docu,ptoe,numero,fecha,enti,status,iuser,idate)';
               $sql_inse.='value("'.$docu.'","'.$ptoe.'","'.$numero.'","'.$fecha.'","'.$enti.'","-","'.$user_codigo.'",now())';
               $res_inse=msql_query($sql_inse,$link_data);
               $res_query=msql_query($sql_query,$link_data);
          }

          if(msql_num_rows($res_query)!=0)
          {
               $idnum1=msql_result($res_query,0,'idnum1');
               $numero=msql_result($res_query,0,'numero');
          }
     }

     if($tabla=='jwmc')
     {
          $enti='9999999999';
          $sql_query ='select * from '.$tabla1.' ';
          $sql_query.='where status!="X" ';
          $sql_query.='and ifnull(numero,0)*1>0 ';
          $sql_query.='and docu="'.$docu.'" ';
          $sql_query.='and fecha="'.$fecha.'" ';
          $sql_query.='and lugar="'.$lugar.'" ';
          $sql_query.='and modo="'.$modo.'" ';
          if($docu=='MDC0')
          {$sql_query.='and ndia="'.$ndia.'" ';}
          $sql_query.='order by idnum1 desc limit 1 ';
          $res_query=msql_query($sql_query,$link_data);
          
          if(msql_num_rows($res_query)==0)
          {
               $numero=new_numero($_PARAMS);
               $sql_inse ='insert into '.$tabla1.'(docu,fecha,ndia,lugar,modo,status,iuser,idate)';
               $sql_inse.='value("'.$docu.'","'.$fecha.'","'.$ndia.'","'.$lugar.'","'.$modo.'","-","'.$user_codigo.'",now())';
               $res_inse=msql_query($sql_inse,$link_data);
               $res_query=msql_query($sql_query,$link_data);
          }

          if(msql_num_rows($res_query)!=0)
          {
               $idnum1=msql_result($res_query,0,'idnum1');
               $numero=msql_result($res_query,0,'numero');
          }
          $deta_ok=0;
     }

     if($tabla=='atvd')
     {
          if(empty($docum)){$docum='PLA1';}
          if(empty($ptoem)){$ptoem='001001';}
          if(empty($numer)){$numer=$plan;}
          if(empty($enti2)){$enti2=$clie;}
          if(empty($puni)){$puni=1;}
          $numerpla=$numer;

          $sql_query ='select * from '.$tabla1.' ';
          $sql_query.='where status!="X" ';
          $sql_query.='and ifnull(numero,0)*1>0 ';
          $sql_query.='and docu="'.$docu.'" ';
          $sql_query.='and fecha="'.$fecha.'" ';
          $sql_query.='and left(enti,10)="'.substr($enti,0,10).'" ';
          $sql_query.='order by idnum1 desc limit 1 ';
          $res_query=msql_query($sql_query,$link_data);

          if(msql_num_rows($res_query)==0)
          {
               $numero=new_numero($_PARAMS);
               $sql_inse ='insert into '.$tabla1.'(docu,ptoe,numero,fecha,enti,status,iuser,idate)';
               $sql_inse.='value("'.$docu.'","'.$ptoe.'","'.$numero.'","'.$fecha.'","'.$enti.'","-","'.$user_codigo.'",now())';
               $res_inse=msql_query($sql_inse,$link_data);
               $res_query=msql_query($sql_query,$link_data);
          }

          if(msql_num_rows($res_query)!=0)
          {
               $idnum1=msql_result($res_query,0,'idnum1');
               $numero=msql_result($res_query,0,'numero');
          }
     }

     if($tabla=='hcli')
     {
          if(empty($ptoe)){$ptoe='001001';}
          if(empty($puni)){$puni=1;}

          $sql_query ='select * from '.$tabla1.' ';
          $sql_query.='where status!="X" ';
          $sql_query.='and ifnull(numero,0)*1>0 ';
          $sql_query.='and docu="'.$docu.'" ';
          $sql_query.='and fecha="'.$fecha.'" ';
          $sql_query.='and left(enti,10)="'.substr($enti,0,10).'" ';
          $sql_query.='order by idnum1 desc limit 1 ';
          $res_query=msql_query($sql_query,$link_data);
          
          
          if(msql_num_rows($res_query)==0)
          {
               $numero=new_numero($_PARAMS);
               $sql_inse ='insert into '.$tabla1.'(docu,ptoe,numero,fecha,enti,status,iuser,idate)';
               $sql_inse.='value("'.$docu.'","'.$ptoe.'","'.$numero.'","'.$fecha.'","'.$enti.'","-","'.$user_codigo.'",now())';
               $res_inse=msql_query($sql_inse,$link_data);
               $res_query=msql_query($sql_query,$link_data);
          }

          if(msql_num_rows($res_query)!=0)
          {
               $idnum1=msql_result($res_query,0,'idnum1');
               $numero=msql_result($res_query,0,'numero');
          }
     }

     if($tabla=='otbj')
     {
//           if(empty($ptoe)){$ptoe='001001';}
          if(empty($ptov)){$ptov='001001';}
          if(empty($puni)){$puni=1;}
          if(empty($numero) and $deta_ok==1){$numero=new_numero($_PARAMS);}          
//           if(empty($numero) and $deta_ok==1 and $format!=3){$numero=new_numero($_PARAMS);}
          
          $sql_query ='select * from '.$tabla1.' ';
//           $sql_query.='left join '.$tabla2.' on idnum1=idnum2 ';
          $sql_query.='where status!="X" ';
          $sql_query.='and ifnull(numero,0)*1>0 ';
          $sql_query.='and docu="'.$docu.'" ';
//           $sql_query.='and ptoe="'.$ptoe.'" ';
          $sql_query.='and ptov="'.$ptov.'" ';
          if(!empty($numero))
          {
               $sql_query.='and numero*1="'.(int)$numero.'" ';
          }
          else
          {
               $sql_query.='and (plvh="'.$plvh.'" or vinn="'.$vinn.'")';
          }
          
          $sql_query.='order by idnum1 desc limit 1 ';
          $res_query=msql_query($sql_query,$link_data);
          
          
          if(msql_num_rows($res_query)==0)
          {
               if(empty($fecha)){$fecha=$fecha1;}
               $numero=new_numero($_PARAMS);
               $sql_inse ='insert into '.$tabla1.'(docu,ptoe,ptov,numero,fecha,enti,status,iuser,idate)';
               $sql_inse.='value("'.$docu.'","'.$ptoe.'","'.$ptov.'","'.$numero.'","'.$fecha.'","'.$enti.'","-","'.$user_codigo.'",now())';
               $res_inse=msql_query($sql_inse,$link_data);
               $res_query=msql_query($sql_query,$link_data);
          }

          if(msql_num_rows($res_query)!=0)
          {
               $idnum1=msql_result($res_query,0,'idnum1');
               $numero=msql_result($res_query,0,'numero');
          }
     }

     if($tabla=='cont')
     {
          if(empty($ptoe)){$ptoe='001001';}
          if(empty($numero) and $deta_ok==1){$numero=new_numero($_PARAMS);}
          
          $sql_query ='select * from '.$tabla1.' ';
          $sql_query.='where status!="X" ';
          $sql_query.='and ifnull(numero,0)*1>0 ';
          $sql_query.='and docu="'.$docu.'" ';
          $sql_query.='and ptoe="'.$ptoe.'" ';
//           $sql_query.='and fecha="'.$fecha.'" ';
//           $sql_query.='and left(enti,10)="'.substr($enti,0,10).'" ';
          $sql_query.='and numero*1="'.(int)$numero.'" ';
          $sql_query.='order by idnum1 desc limit 1 ';
          $res_query=msql_query($sql_query,$link_data);

          
          if(msql_num_rows($res_query)==0)
          {
               $numero=new_numero($_PARAMS);
               $sql_inse ='insert into '.$tabla1.'(docu,ptoe,numero,fecha,enti,status,iuser,idate)';
               $sql_inse.='value("'.$docu.'","'.$ptoe.'","'.$numero.'","'.$fecha.'","'.$enti.'","-","'.$user_codigo.'",now())';
               $res_inse=msql_query($sql_inse,$link_data);
               $res_query=msql_query($sql_query,$link_data);
          }

          if(msql_num_rows($res_query)!=0)
          {
               $idnum1=msql_result($res_query,0,'idnum1');
               $numero=msql_result($res_query,0,'numero');
          }
     }

     if($tabla=='vtas' or $tabla=='clie')
     {
          $ptov0=data_maes('ptov',$ptov);
          $ptoe=$ptov0['ptov_ptoe'];
          
          if($empr0['empr_ceuser']==1)
          {
               $ptoe=$user0['user_ptov'];
               $ptov=$user0['user_ptov'];
               $vndd=$user0['user_cedula'];
               if(empty($ptoe)){$ptoe='001101';}
               if(empty($ptov)){$ptov='001101';}
               if(empty($vndd))
               {$error_save.='/REGISTRE SU NUMERO DE CEDULA O RUC ';}          
          }
          
          
          if(empty($tarjeta) and empty($credito) and empty($fpag)){$fpag='01';}

          if(empty($numero) and $deta_ok==1 and $format!=3){$numero=new_numero($_PARAMS);}

          $sql_query ='select * from '.$tabla1.' ';
          $sql_query.='where status!="X" ';
          $sql_query.='and ifnull(numero,0)*1>0 ';
          $sql_query.='and docu="'.$docu.'" ';
          $sql_query.='and ptov="'.$ptov.'" ';
          if(!empty($numero))
          {
               $sql_query.='and numero*1="'.(int)$numero.'" ';
          }
          else
          {
               $sql_query.='and fecha="'.$fecha.'" ';
               $sql_query.='and left(enti,10)="'.substr($enti,0,10).'" ';
               $sql_query.='and enti not like "9999%" ';
          }

          if($empr0['empr_ceuser']==1)
          {$sql_query.='and vndd ="'.$vndd.'" ';}
          
          $sql_query.='order by idnum1 desc limit 1 ';
          $res_query=msql_query($sql_query,$link_data);
//           $alert_save=$sql_query;
          
//           if(msql_num_rows($res_query)==0)
//           if(msql_num_rows($res_query)==0 and $deta_ok==0){$error_save.=$error_save2;}
          
          
//           die('fdgf');   
          
          if(msql_num_rows($res_query)==0 and $deta_ok==1)
          {
               $numero=new_numero($_PARAMS);
               $sql_inse ='insert into '.$tabla1.'(docu,ptoe,ptov,numero,fecha,enti,status,iuser,idate)';
               $sql_inse.='value("'.$docu.'","'.$ptoe.'","'.$ptov.'","'.$numero.'","'.$fecha.'","'.$enti.'","-","'.$user_codigo.'",now())';
               $res_inse=msql_query($sql_inse,$link_data);
               $res_query=msql_query($sql_query,$link_data);
          }

          if(msql_num_rows($res_query)!=0)
          {
               $idnum1=msql_result($res_query,0,'idnum1');
               $numero=msql_result($res_query,0,'numero');
          }

          
          if(empty($puni))
          {
               $sql_query='select * from item0 where item_codigo like "'.trim($item).'%" order by item_codigo limit 1 ';
               $res_query=msql_query($sql_query,$link_data);
               $puni=msql_result($res_query,0,'item_puni1');
               $iva=msql_result($res_query,0,'item_iva');
          }
          
          if(empty($puni))
          {
               $sql_query='select * from vtas2 where item like "'.trim($item).'%" order by idnum2 desc limit 1 ';
               $res_query=msql_query($sql_query,$link_data);
               $puni=msql_result($res_query,0,'puni');
               $iva=msql_result($res_query,0,'iva');
          }
          
          if(empty($puni)){$puni=1;}
          
          if($puni>0 and !empty($item))
          {
               $sql_query='update item0 set item_puni="'.$puni.'", item_puni1="'.$puni.'" where item_codigo="'.$item.'" and ifnull(item_puni,0)=0 limit 1';
               $res_query=msql_query($sql_query,$link_data);
          }
          
          if(!empty($ptov) and !empty($user_codigo))
          {
               $sql_query='update user0 set user_ptov="'.$ptov.'" where user_codigo="'.$user_codigo.'" and ifnull(user_ptov,"")="" limit 1';
               $res_query=msql_query($sql_query,$link_data);
          }
          
          if(!empty($caja) and !empty($user_codigo))
          {
               $sql_query='update user0 set user_caja="'.$caja.'" where user_codigo="'.$user_codigo.'" and ifnull(user_caja,"")="" limit 1';
               $res_query=msql_query($sql_query,$link_data);
          }
          
          if(!empty($vndd) and !empty($user_codigo))
          {
               $sql_query='update user0 set user_cedula="'.$vndd.'" where user_codigo="'.$user_codigo.'"and ifnull(user_cedula,"")="" limit 1';
               $res_query=msql_query($sql_query,$link_data);
          }
          
          if(!empty($pcnt))
          {
//                $sql_query='update enti0 set enti_factura="'.$enti.'" where enti_codigo="'.$pcnt.'"  limit 1';
               $sql_query='update enti0 set enti_factura="'.$enti.'" where enti_codigo="'.$pcnt.'" and ifnull(enti_factura,"")=""  limit 1';
               $res_query=msql_query($sql_query,$link_data);
          }
          
          
     }

     if($tabla=='comp' or $tabla=='prov' or $tabla=='lpro')
     {
          $ptoe0=data_maes('ptoe',$ptoe);
//           $ptoe=$ptov0['ptov_ptoe'];
          
          if($tabla=='comp')
          {if(empty($tarjeta) and empty($credito) and empty($fpag)){$fpag='20';}}

          if(mb_stristr2('.FP03.FP90',$docu) or $tabla=='prov')
          {
               if(empty($numero) and $deta_ok==1 and $format!=3){$numero=new_numero($_PARAMS); }
          }

          $sql_query ='select * from '.$tabla1.' ';
//           $sql_query.='where status not in("O","X") ';
          $sql_query.='where status!="X" ';
          $sql_query.='and ifnull(numero,0)*1>0 ';
          $sql_query.='and docu="'.$docu.'" ';
          $sql_query.='and ptoe="'.$ptoe.'" ';
          
          if($format!=3)
          {$sql_query.='and numero*1="'.(int)$numero.'" ';}
          
          if($format!=3)
          {$sql_query.='and left(enti,10) like "'.substr($enti,0,10).'%" ';}
          
          if($format==3)
          {$sql_query.='and csub1="'.$csub1.'" ';}
          
          if($format==3)
          {$sql_query.='and fecha="'.date('Y-m-d').'" ';}
          
          $sql_query.='order by idnum1 desc limit 1 ';
          $res_query=msql_query($sql_query,$link_data);
//           echo '<br>==='.$sql_query;
               
//           $error_save=$sql_query;     
//           echo '***'.$docu.$ptov.$numero;

          if(msql_num_rows($res_query)==0)
          {
               if($format==3){$numero=new_numero($_PARAMS);}
               
//                $sql_inse ='insert into '.$tabla1.'(docu,ptoe,numero,fecha,enti,status,iuser,idate)';
//                $sql_inse.='value("'.$docu.'","'.$ptoe.'","'.$numero.'","'.$fecha.'","'.$enti.'","-","'.$user_codigo.'",now())';
               $sql_inse ='insert into '.$tabla1.'(docu,ptoe,numero,fecha,enti,csub1,status,iuser,idate)';
               $sql_inse.='value("'.$docu.'","'.$ptoe.'","'.$numero.'","'.$fecha.'","'.$enti.'","'.$csub1.'","-","'.$user_codigo.'",now())';
               $res_inse=msql_query($sql_inse,$link_data);
//           $error_save=$sql_inse;     
               
               
               $res_query=msql_query($sql_query,$link_data);
               
//           $error_save.=' ==== '.$sql_query;     
          }

          if(msql_num_rows($res_query)!=0)
          {
               $idnum1=msql_result($res_query,0,'idnum1');
               $numero=msql_result($res_query,0,'numero');
//                $error_save='==='.$idnum1.'==='.$numero;
          }
          
          if(empty($puni))
          {
               $sql_query='select * from item0 where item_codigo like "'.trim($item).'%" order by item_codigo limit 1 ';
               $res_query=msql_query($sql_query,$link_data);
               $puni=msql_result($res_query,0,'item_pcp1');
               $iva=msql_result($res_query,0,'item_iva');
          }
          
          if(empty($puni))
          {
               $sql_query='select * from comp1 left join comp2 on idnum1=idnum2 where left(enti,10)="'.substr($enti,0,10).'" item like "'.trim($item).'%" order by idnum2 desc limit 1 ';
               $res_query=msql_query($sql_query,$link_data);
               $puni=msql_result($res_query,0,'puni');
               $iva=msql_result($res_query,0,'iva');
          }
          
          if(empty($puni)){$puni=1;}
          
          if($puni>0 and !empty($item))
          {
               $sql_query='update item0 set item_pcp1="'.$puni.'" where item_codigo="'.$item.'" and ifnull(item_puni,0)=0 limit 1';
               $res_query=msql_query($sql_query,$link_data);
          }
          
          if(!empty($caja) and !empty($user_codigo))
          {
               $sql_query='update user0 set user_caja="'.$caja.'" where user_codigo="'.$user_codigo.'" and ifnull(user_caja,"")="" limit 1';
               $res_query=msql_query($sql_query,$link_data);
          }
          
          
     }

     if($tabla=='caja' or $tabla=='bcos')
     {
          $caja0=data_maes('caja',$caja);
          
//           echo '*/*/*/*/-----'.$numero;
          
          
          if(empty($numero) and $deta_ok==1){$numero=new_numero($_PARAMS);}
//           echo '*/*/*/*/======'.$numero;

          $sql_query ='select * from '.$tabla1.' ';
          $sql_query.='where ifnull(status,"")!="X" ';
          $sql_query.='and docu="'.$docu.'" ';
          $sql_query.='and caja="'.$caja.'" ';
          $sql_query.='and ifnull(numero,0)*1>0 ';
          $sql_query.='and numero*1="'.(int)$numero.'" ';
          $sql_query.='and left(enti,10)="'.substr($enti,0,10).'" ';
          $sql_query.='order by idnum1 desc limit 1 ';
          $res_query=msql_query($sql_query,$link_data);

          
//           echo '*/*/*/*/*'.$sql_query;
          
          if(msql_num_rows($res_query)==0)
          {
               $sql_inse ='insert into '.$tabla1.'(docu,caja,numero,fecha,enti,status,iuser,idate)';
               $sql_inse.='value("'.$docu.'","'.$caja.'","'.$numero.'","'.$fecha.'","'.$enti.'","-","'.$user_codigo.'",now())';
               $res_inse=msql_query($sql_inse,$link_data);
               $res_query=msql_query($sql_query,$link_data);
//           echo '*/*/*/*/*'.$sql_inse;
          }

          if(msql_num_rows($res_query)!=0)
          {
               $idnum1=msql_result($res_query,0,'idnum1');
               $numero=msql_result($res_query,0,'numero');
               $stfin=msql_result($res_query,0,'stfin');
          }

          if(empty($cant)){$cant=1;}
          if(empty($puni)){$puni=1;}
          
          if(!empty($caja) and !empty($user_codigo))
          {
               $sql_query='update user0 set user_caja="'.$caja.'" where user_codigo="'.$user_codigo.'" and ifnull(user_caja,"")="" limit 1';
               $res_query=msql_query($sql_query,$link_data);
          }
     }

     if($tabla=='dxco' or $tabla=='dxpa')
     {
          
          if(empty($numero) and $deta_ok==1){$numero=new_numero($_PARAMS);}

          $sql_query ='select * from '.$tabla1.' ';
          $sql_query.='where ifnull(status,"")!="X" ';
          $sql_query.='and docu="'.$docu.'" ';
          $sql_query.='and ptoe="'.$ptoe.'" ';
          $sql_query.='and ifnull(numero,0)*1>0 ';
          $sql_query.='and numero*1="'.(int)$numero.'" ';
          $sql_query.='and left(enti,10)="'.substr($enti,0,10).'" ';
          $sql_query.='order by idnum1 desc limit 1 ';
          $res_query=msql_query($sql_query,$link_data);

          if(msql_num_rows($res_query)==0)
          {
               $sql_inse ='insert into '.$tabla1.'(docu,ptoe,numero,fecha,enti,status,iuser,idate)';
               $sql_inse.='value("'.$docu.'","'.$ptoe.'","'.$numero.'","'.$fecha.'","'.$enti.'","-","'.$user_codigo.'",now())';
               $res_inse=msql_query($sql_inse,$link_data);
               $res_query=msql_query($sql_query,$link_data);
          }

          if(msql_num_rows($res_query)!=0)
          {
               $idnum1=msql_result($res_query,0,'idnum1');
               $numero=msql_result($res_query,0,'numero');
               $stfin=msql_result($res_query,0,'stfin');
          }

          if(empty($cant)){$cant=1;}
          if(empty($puni)){$puni=1;}
          
//           if(!empty($caja) and !empty($user_codigo))
//           {
//                $sql_query='update user0 set user_caja="'.$caja.'" where user_codigo="'.$user_codigo.'" and ifnull(user_caja,"")="" limit 1';
//                $res_query=msql_query($sql_query,$link_data);
//           }
     }

     
     
     if($tabla=='retp')
     {
          $ptoe0=data_maes('ptoe',$ptoe);

          if(empty($numero) and $deta_ok==1){$numero=new_numero($_PARAMS);}
          
          $sql_query ='select * from '.$tabla1.' ';
          $sql_query.='where ifnull(status,"")!="X" ';
          $sql_query.='and docu="'.$docu.'" ';
          $sql_query.='and ptoe="'.$ptoe.'" ';
          $sql_query.='and ifnull(numero,0)*1>0 ';
//           if(!empty($numero))
          {$sql_query.='and numero*1="'.(int)$numero.'" ';}
//           else
//           {$sql_query.='and fecha="'.$fecha.'" ';}
          $sql_query.='order by idnum1 desc limit 1 ';
          
          $res_query=msql_query($sql_query,$link_data);
          
          
//      echo '===== '.$ptoe;
//      echo '===== '.$deta_ok;
//      echo '===== '.$sql_query;
//      echo '===== '.msql_num_rows($res_query);
//      die();
          
          if(msql_num_rows($res_query)==0 and $deta_ok==1)
          {
               $numero=new_numero($_PARAMS);
               $sql_inse ='insert into '.$tabla1.'(docu,ptoe,numero,fecha,enti,status,iuser,idate)';
               $sql_inse.='value("'.$docu.'","'.$ptoe.'","'.$numero.'","'.$fecha.'","'.$enti.'","-","'.$user_codigo.'",now())';
               $res_inse=msql_query($sql_inse,$link_data);
               $res_query=msql_query($sql_query,$link_data);
          }

          if(msql_num_rows($res_query)!=0)
          {
               $idnum1=msql_result($res_query,0,'idnum1');
               $numero=msql_result($res_query,0,'numero');
          }

          
//           $puni=0.000001;
          
          if($cant==0 and $puni==0){$deta_ok=0;}
          
          
          
     }

     if($tabla=='inve' or $tabla=='pduc')
     {
          $lote=trim($lote);
          if(strlen($lote)>=4)
          {
               $lote_descri=$lote;
               inse_maes('lote',$lote,$lote_descri);
//                if(strlen($item)>=6)
               $sql_lote ='update lote0 set ';
               $sql_lote.='lote_fabrica="'.$fabrica.'" ';
//                $sql_lote.='lote_caduca="'.$caduca.'" ';
               $sql_lote.='where lote_codigo="'.$lote.'" ';
               $sql_lote.='and (ifnull(lote_fabrica,"")=""';
               $sql_lote.='or substring(lote_idate,1,10)="'.date('Y-m-d').'") ';
               $res_lote=msql_query($sql_lote,$link_data);
               
               $sql_lote ='update lote0 set ';
               $sql_lote.='lote_caduca="'.$caduca.'" ';
//                $sql_lote.='lote_caduca="'.$caduca.'" ';
               $sql_lote.='where lote_codigo="'.$lote.'" ';
               $sql_lote.='and (ifnull(lote_caduca,"")=""';
               $sql_lote.='or substring(lote_idate,1,10)="'.date('Y-m-d').'") ';
               $res_lote=msql_query($sql_lote,$link_data);
               
          }
          
     }
     
     
     if($tabla=='inve' or $tabla=='infi')
     {
          if(empty($enti)){$enti='9999999999999';}
          if(empty($puni)){$puni=1;}
          $bode0=data_maes('bode',$bode);
          $plvh0=data_maes('plvh',$plvh);
          $ptoe=$bode0['bode_ptoe'];
          
          if(!empty($plvh) and substr($enti,0,4)=='9999')
          {
//                $sql_vh
          }
          
//           if(empty($numero) and $format!=3){$numero=new_numero($_PARAMS);}
          if(empty($numero) and $deta_ok==1 and $format!=3){$numero=new_numero($_PARAMS);}

          $sql_query ='select * from '.$tabla1.' ';
          $sql_query.='where status!="X" ';
          $sql_query.='and ifnull(numero,0)*1>0 ';
          $sql_query.='and docu="'.$docu.'" ';
          $sql_query.='and bode="'.$bode.'" ';
//           $sql_query.='and fecha="'.$fecha.'" ';
          if(!empty($numero))
          {$sql_query.='and numero*1="'.(int)$numero.'" ';}
          else
          {
               $sql_query.='and fecha="'.$fecha.'" ';
               
               if(!empty($enti))
               {$sql_query.='and left(enti,10)="'.substr($enti,0,10).'" ';}
               
//                31/marzo/2025
               if(empty($plvh))
               {$sql_query.='and enti not like "9999%" ';}
          }
          
          if($format==3)
          {
               if(empty($plvh))
               {$sql_query.='and left(enti,10) like "'.substr($enti,0,10).'%" ';}
               
               if(!empty($plvh))
               {$sql_query.='and plvh="'.$plvh.'" ';}
               
               if(!empty($numfac))
               {$sql_query.='and numfac*1="'.(int)$numfac.'" ';}
          }
          $sql_query.='order by idnum1 desc limit 1 ';
          $res_query=msql_query($sql_query,$link_data);
          
//           echo '*/*/*/*/'.$sql_query;

          if(msql_num_rows($res_query)==0)
          {
//                var_dump2($_PARAMS);
               
               $numero=new_numero($_PARAMS);
//                echo '-+-+-+-'.$numero;
//                die();
               
               $sql_inse ='insert into '.$tabla1.'(docu,ptoe,bode,numero,fecha,enti,numfac,status,iuser,idate)';
               $sql_inse.='value("'.$docu.'","'.$ptoe.'","'.$bode.'","'.$numero.'","'.$fecha.'","'.$enti.'","'.$numfac.'","-","'.$user_codigo.'",now())';
               if(!empty($plvh))
               {
                    $sql_inse ='insert into '.$tabla1.'(docu,ptoe,bode,numero,fecha,enti,plvh,numfac,status,iuser,idate)';
                    $sql_inse.='value("'.$docu.'","'.$ptoe.'","'.$bode.'","'.$numero.'","'.$fecha.'","'.$enti.'","'.$plvh.'","'.$numfac.'","-","'.$user_codigo.'",now())';
               }
               $res_inse=msql_query($sql_inse,$link_data);
               $res_query=msql_query($sql_query,$link_data);
          }

          if(msql_num_rows($res_query)!=0)
          {
               $idnum1=msql_result($res_query,0,'idnum1');
               $numero=msql_result($res_query,0,'numero');
          }
     }

     
     if($tabla=='pduc')
     {
          if(empty($ptoe)){$ptoe='001001';}
          if(empty($numero) and $deta_ok==1){$numero=new_numero($_PARAMS);}
          
          $sql_query ='select * from '.$tabla1.' ';
          $sql_query.='where status!="X" ';
          $sql_query.='and ifnull(numero,0)*1>0 ';
          $sql_query.='and docu="'.$docu.'" ';
          $sql_query.='and ptoe="'.$ptoe.'" ';
          $sql_query.='and numero*1="'.(int)$numero.'" ';
          $sql_query.='order by idnum1 desc limit 1 ';
          $res_query=msql_query($sql_query,$link_data);

          
          if(msql_num_rows($res_query)==0)
          {
               $numero=new_numero($_PARAMS);
               $sql_inse ='insert into '.$tabla1.'(docu,ptoe,numero,fecha,enti,status,iuser,idate)';
               $sql_inse.='value("'.$docu.'","'.$ptoe.'","'.$numero.'","'.$fecha.'","'.$enti.'","-","'.$user_codigo.'",now())';
               $res_inse=msql_query($sql_inse,$link_data);
               $res_query=msql_query($sql_query,$link_data);
          }

          if(msql_num_rows($res_query)!=0)
          {
               $idnum1=msql_result($res_query,0,'idnum1');
               $numero=msql_result($res_query,0,'numero');
          }
     }

     
     if($tabla=='susa')
     {
          if(empty($enti)){$enti='9999999999999';}
          if(empty($numero) and $deta_ok==1){$numero=new_numero($_PARAMS);}

          $sql_query ='select * from '.$tabla1.' ';
          $sql_query.='where status!="X" ';
          $sql_query.='and ifnull(numero,0)*1>0 ';
          $sql_query.='and docu="'.$docu.'" ';
          if(!empty($numero))
          {$sql_query.='and numero*1="'.(int)$numero.'" ';}
          else
          {
               $sql_query.='and fecha="'.$fecha.'" ';
               $sql_query.='and left(enti,10)="'.substr($enti,0,10).'" ';
               $sql_query.='and enti not like "9999%" ';
          }
          
          if($format==3)
          {
               $sql_query.='and left(enti,10) like "'.substr($enti,0,10).'%" ';
               if(!empty($numfac))
               {$sql_query.='and numfac*1="'.(int)$numfac.'" ';}
          }
          $sql_query.='order by idnum1 desc limit 1 ';
          $res_query=msql_query($sql_query,$link_data);
          
//           echo '*/*/*/*/'.$sql_query;

          if(msql_num_rows($res_query)==0)
          {
               $numero=new_numero($_PARAMS);
//                echo '-+-+-+-'.$numero;
               $sql_inse ='insert into '.$tabla1.'(docu,ptoe,numero,fecha,enti,status,iuser,idate)';
               $sql_inse.='value("'.$docu.'","'.$ptoe.'","'.$numero.'","'.$fecha.'","'.$enti.'","-","'.$user_codigo.'",now())';
               $res_inse=msql_query($sql_inse,$link_data);
               $res_query=msql_query($sql_query,$link_data);
          }

          if(msql_num_rows($res_query)!=0)
          {
               $idnum1=msql_result($res_query,0,'idnum1');
               $numero=msql_result($res_query,0,'numero');
          }
     }

     if($tabla=='nomi')
     {
//           if(empty($enti)){$enti='9999999999999';}
          $enti=$empr0['empr_ruc'];
//           if(empty($numero) and $deta_ok==1){$numero=new_numero($_PARAMS);}

          $sql_query ='select * from '.$tabla1.' ';
          $sql_query.='where status!="X" ';
          $sql_query.='and fecha="'.$fecha.'" ';
          $sql_query.='and docu="'.$docu.'" ';
          $sql_query.='and ifnull(numero,0)*1>0 ';
          $sql_query.='and numero*1="'.(int)$numero.'" ';
          
          if(!empty($numero))
          {
//                $sql_query.='and numero*1="'.(int)$numero.'" ';
          }
          else
          {
//                $sql_query.='and fecha="'.$fecha.'" ';
//                $sql_query.='and left(enti,10)="'.substr($enti,0,10).'" ';
//                $sql_query.='and enti not like "9999%" ';
          }
          
          if($format==3)
          {
//                $sql_query.='and left(enti,10) like "'.substr($enti,0,10).'%" ';
//                if(!empty($numfac))
//                {$sql_query.='and numfac*1="'.(int)$numfac.'" ';}
          }
          $sql_query.='order by idnum1 desc limit 1 ';
          $res_query=msql_query($sql_query,$link_data);
          
//           echo '*/*/*/*/'.$sql_query;

          if(msql_num_rows($res_query)==0)
          {
//                $numero=new_numero($_PARAMS);
//                echo '-+-+-+-'.$numero;
               $sql_inse ='insert into '.$tabla1.'(docu,ptoe,numero,fecha,enti,status,iuser,idate)';
               $sql_inse.='value("'.$docu.'","'.$ptoe.'","'.$numero.'","'.$fecha.'","'.$enti.'","-","'.$user_codigo.'",now())';
               $res_inse=msql_query($sql_inse,$link_data);
               $res_query=msql_query($sql_query,$link_data);
          }

          if(msql_num_rows($res_query)!=0)
          {
               $idnum1=msql_result($res_query,0,'idnum1');
               $numero=msql_result($res_query,0,'numero');
          }
     }

     if($tabla=='opro')
     {
          if(empty($enti)){$enti=$empr0['empr_ruc'];}
          if(empty($puni)){$puni=1;}
          if(empty($ptoe)){$ptoe='001001';}
          if(empty($numero) and $deta_ok==1 and $format!=3){$numero=new_numero($_PARAMS);}

          $sql_query ='select * from '.$tabla1.' ';
          $sql_query.='where status!="X" ';
          $sql_query.='and ifnull(numero,0)*1>0 ';
          $sql_query.='and docu="'.$docu.'" ';
//           $sql_query.='and bode="'.$bode.'" ';
//           $sql_query.='and fecha="'.$fecha.'" ';
          if(!empty($numero))
          {$sql_query.='and numero*1="'.(int)$numero.'" ';}
          else
          {
               $sql_query.='and fecha="'.$fecha.'" ';
               $sql_query.='and left(enti,10)="'.substr($enti,0,10).'" ';
               $sql_query.='and enti not like "9999%" ';
          }
          
          if($format==3)
          {
//                $sql_query.='and left(enti,10) like "'.substr($enti,0,10).'%" ';
//                if(!empty($numfac))
//                {$sql_query.='and numfac*1="'.(int)$numfac.'" ';}
          }
          $sql_query.='order by idnum1 desc limit 1 ';
          $res_query=msql_query($sql_query,$link_data);
          
//           echo '*/*/*/*/'.$sql_query;

          if(msql_num_rows($res_query)==0)
          {
               $numero=new_numero($_PARAMS);
//                echo '-+-+-+-'.$numero;
               $sql_inse ='insert into '.$tabla1.'(docu,ptoe,numero,fecha,enti,status,iuser,idate)';
               $sql_inse.='value("'.$docu.'","'.$ptoe.'","'.$numero.'","'.$fecha.'","'.$enti.'","-","'.$user_codigo.'",now())';
               $res_inse=msql_query($sql_inse,$link_data);
               $res_query=msql_query($sql_query,$link_data);
          }

          if(msql_num_rows($res_query)!=0)
          {
               $idnum1=msql_result($res_query,0,'idnum1');
               $numero=msql_result($res_query,0,'numero');
          }
     }
     
     if($tabla=='guia')
     {
          $ptoe0=data_maes('ptoe',$ptoe);
          
          if(empty($numero) and $deta_ok==1 and $format!=3){$numero=new_numero($_PARAMS);}

          $sql_query ='select * from '.$tabla1.' ';
//           $sql_query.='where status not in("O","X") ';
          $sql_query.='where status !="X" ';
          $sql_query.='and docu="'.$docu.'" ';
          $sql_query.='and ptoe="'.$ptoe.'" ';
          $sql_query.='and ifnull(numero,0)*1>0 ';
          if(!empty($numero))
          {$sql_query.='and numero*1="'.(int)$numero.'" ';}
          else
          {
               $sql_query.='and fecha="'.$fecha.'" ';
               $sql_query.='and left(enti,10)="'.substr($enti,0,10).'" ';
               $sql_query.='and enti not like "9999%" ';
          }
          $sql_query.='order by idnum1 desc limit 1 ';
          $res_query=msql_query($sql_query,$link_data);
          
//           echo '***'.$docu.$ptov.$numero;

//           if(msql_num_rows($res_query)==0)
//           if(msql_num_rows($res_query)==0 and $deta_ok==0){$error_save.=$error_save2;}
          
          if(msql_num_rows($res_query)==0 and $deta_ok==1)
          {
               $numero=new_numero($_PARAMS);
//                $sql_inse ='insert into '.$tabla1.'(docu,ptoe,ptov,numero,fecha,enti,numfac,status,iuser,idate)';
//                $sql_inse.='value("'.$docu.'","'.$ptoe.'","'.$ptov.'","'.$numero.'","'.$fecha.'","'.$enti.'","'.$numfac.'","-","'.$user_codigo.'",now())';
               $sql_inse ='insert into '.$tabla1.'(docu,ptoe,numero,fecha,enti,status,iuser,idate)';
               $sql_inse.='value("'.$docu.'","'.$ptoe.'","'.$numero.'","'.$fecha.'","'.$enti.'","-","'.$user_codigo.'",now())';
               $res_inse=msql_query($sql_inse,$link_data);
               $res_query=msql_query($sql_query,$link_data);
          }

          if(msql_num_rows($res_query)!=0)
          {
               $idnum1=msql_result($res_query,0,'idnum1');
               $numero=msql_result($res_query,0,'numero');
          }

          
//           if(empty($puni))
          {$puni=0.000001;}
          
          if($puni>0 and !empty($item))
          {
//                $sql_query='update item0 set item_puni="'.$puni.'", item_puni1="'.$puni.'" where item_codigo="'.$item.'" and ifnull(item_puni,"")="" limit 1';
//                $res_query=msql_query($sql_query,$link_data);
          }
          
          
          
     }

     if($tabla=='rmed')
     {
          $ptoe0=data_maes('ptoe',$ptoe);
          
//           if(empty($numero) and $deta_ok==1 and $format!=3){$numero=new_numero($_PARAMS);}
          if(empty($numero) and $deta_ok==1){$numero=new_numero($_PARAMS);}

          $sql_query ='select * from '.$tabla1.' ';
//           $sql_query.='where status not in("O","X") ';
          $sql_query.='where status !="X" ';
          $sql_query.='and docu="'.$docu.'" ';
          $sql_query.='and ptoe="'.$ptoe.'" ';
          $sql_query.='and ifnull(numero,0)*1>0 ';
          $sql_query.='and mdco ="'.$mdco.'" ';
          if(!empty($numero))
          {
               $sql_query.='and numero*1="'.(int)$numero.'" ';
          }
          else
          {
               $sql_query.='and fecha="'.$fecha.'" ';
               $sql_query.='and left(enti,10)="'.substr($enti,0,10).'" ';
//                $sql_query.='and mdco ="'.$mdco.'" ';
//                $sql_query.='and enti not like "9999%" ';
          }
          $sql_query.='order by idnum1 desc limit 1 ';
          $res_query=msql_query($sql_query,$link_data);
          
          
          if(msql_num_rows($res_query)==0 and $deta_ok==1)
          {
               $numero=new_numero($_PARAMS);
               $sql_inse ='insert into '.$tabla1.'(docu,ptoe,numero,fecha,enti,status,iuser,idate)';
               $sql_inse.='value("'.$docu.'","'.$ptoe.'","'.$numero.'","'.$fecha.'","'.$enti.'","-","'.$user_codigo.'",now())';
               $res_inse=msql_query($sql_inse,$link_data);
               $res_query=msql_query($sql_query,$link_data);
          }

          if(msql_num_rows($res_query)!=0)
          {
               $idnum1=msql_result($res_query,0,'idnum1');
               $numero=msql_result($res_query,0,'numero');
          }

          
          
//           if(empty($puni))
          {$puni=0.000001;}
          
          
          
     }

     
     
     if($tabla=='soft')
     {
       
          $docum='PASB';
          $docum='';
          $ptoe='001001';
          
          $sql_query ='select * from '.$tabla1.' ';
          $sql_query.='where status not in("O","X") ';
          $sql_query.='and docu="'.$docu.'" ';
          //$sql_query.='and fecha="'.$fecha.'" ';
          $sql_query.='and jornada="'.$jornada.'" ';
          $sql_query.='and liga="'.$liga.'" ';
          $sql_query.='and torn="'.$torn.'" ';
          $sql_query.='and tipo="'.$tipo.'" ';
          $sql_query.='and team1="'.$team1.'" ';
          $sql_query.='and team2="'.$team2.'" ';
          $sql_query.='and tipo="'.$tipo.'" ';
          $sql_query.='order by idnum1 desc limit 1 ';
          $res_query=msql_query($sql_query,$link_data);

          if(msql_num_rows($res_query)==0)
          {
               $numero=new_numero($_PARAMS);
               $sql_inse ='insert into '.$tabla1.'(docu,ptoe,numero,fecha,enti,status,iuser,idate, liga, torn, jornada, horag, tipo, team1, team2)';
               $sql_inse.='value("'.$docu.'","'.$ptoe.'","'.$numero.'","'.$fecha.'","'.$enti.'","-","'.$user_codigo.'",now(),"'.$liga.'","'.$torn.'","'.$jornada.'","'.$horag.'","'.$tipo.'","'.$team1.'","'.$team2.'")';
               $res_inse=msql_query($sql_inse,$link_data);
               $res_query=msql_query($sql_query,$link_data);
          }

          if(msql_num_rows($res_query)!=0)
          {
               $idnum1=msql_result($res_query,0,'idnum1');
               $numero=msql_result($res_query,0,'numero');
          }
       
     //$alert_save = "GRABANDO EN SOFT".$idnum1."-".$sql_query.$hora ; 

     }     

     if($tabla=='liup')
     {
       
          $docum='PASB';
          $docum='';
          $ptoe='001001';
          
          $sql_query ='select * from '.$tabla1.' ';
          $sql_query.='where status not in("O","X") ';
          $sql_query.='and docu="'.$docu.'" ';
          //$sql_query.='and fecha="'.$fecha.'" ';
          $sql_query.='and team="'.$team.'" ';
          $sql_query.='and idsoft="'.$idsoft.'" ';
          $sql_query.='order by idnum1 desc limit 1 ';
          $res_query=msql_query($sql_query,$link_data);

          if(msql_num_rows($res_query)==0)
          {
               $numero=new_numero($_PARAMS);
               $sql_inse ='insert into '.$tabla1.'(docu,ptoe,numero,fecha,enti,status,iuser,idate, team, idsoft)';
               $sql_inse.='value("'.$docu.'","'.$ptoe.'","'.$numero.'","'.$fecha.'","'.$enti.'","-","'.$user_codigo.'",now(),"'.$team.'",'.$idsoft.')';
               $res_inse=msql_query($sql_inse,$link_data);
               $res_query=msql_query($sql_query,$link_data);
          }

          if(msql_num_rows($res_query)!=0)
          {
               $idnum1=msql_result($res_query,0,'idnum1');
               $numero=msql_result($res_query,0,'numero');
          }
       
     //$alert_save = "GRABANDO EN SOFT".$idnum1."-".$sql_query.$hora ; 

     }     
     
     $consolelog=$response->appendChild($dom->createElement("consolelog"));
     // $div=$consolelog->appendChild($dom->createElement("sql",$sql_query)); 
     // $div=$consolelog->appendChild($dom->createElement("empr",$empr)); 
     // $div=$consolelog->appendChild($dom->createElement("idnum1",$idnum1)); 
     // $div=$consolelog->appendChild($dom->createElement("numero",$numero)); 

     $ptoe=str_pad($ptoe,6,'0',STR_PAD_LEFT);
     $numero=str_pad($numero,9,'0',STR_PAD_LEFT);
     $ptoem=str_pad($ptoem,6,'0',STR_PAD_LEFT);
     $numer=str_pad($numer,9,'0',STR_PAD_LEFT);
     
     $response->appendChild($dom->createElement("docu",$docu));
     $response->appendChild($dom->createElement("idnum1",$idnum1));
     $response->appendChild($dom->createElement("numero",$numero));
     

     if(empty($idnum1)){$idnum1=$idnum;}

     if(empty($idnum1))
     {    
          if(!empty($docu) and !empty($ptoe) and !empty($numero))
          {
               $sql_docu ='select idnum1,docu,ptoe,numero,enti,status ';
               $sql_docu.='from '.$tabla1.' ';
               $sql_docu.='where status!="X" ';
               $sql_docu.='and idnum1>0 ';
               $sql_docu.='and docu="'.$docu.'" ';
               $sql_docu.='and ptoe='.$ptoe.' ';
               $sql_docu.='and numero='.$numero.' ';
               if(mb_stristr2('.FP10.FP15.RCLI.',$docu))
               {$sql_docu.='and enti like "'.substr($enti,0,10).'%" ';}
          //      if(mb_stristr2('.ICH1',$docu))
          //      $sql_docu.='and enti like "'.substr($enti,0,10).'%" ';
               $sql_docu.='order by fecha desc,idnum1 desc ';
               $sql_docu.='limit 1';
               $res_docu=msql_query($sql_docu,$link_data);
               $idnum1=msql_result($res_docu,0,'idnum1');
          }
     }


     if(empty($idnum1))
     {
          //insert
          $sql_inse ='insert into '.$tabla1.'(docu,ptoe,numero,enti,status,iuser,idate) ';
          $sql_inse.='value("'.$docu.'","'.$ptoe.'","'.$numero.'","'.$enti.'","-","'.$user_codigo.'",now())';
          $res_inse=msql_query($sql_inse,$link_data);

          $res_docu=msql_query($sql_docu,$link_data);
          $idnum1=msql_result($res_docu,0,'idnum1');
     
     }

     if(!empty($idnum1))
     {
          $sql_stru='describe '.$tabla.'1';
          $res_stru=msql_query($sql_stru,$link_data);
          $sql_updt='update '.$tabla.'1 set ';
          while($aso_stru=@msql_fetch_assoc($res_stru))
          { 
               $xvalor=${$aso_stru['Field']};
               $xtype.=$aso_stru['Type'];
               if($xvalor!='' and $aso_stru['Field']!='idnum1')
               {$sql_updt.=$aso_stru['Field'].'="'.$xvalor.'", ';}
          }
          $sql_updt.='uuser="'.$user_codigo.'",';
          $sql_updt.='udate=now(),';
          $sql_updt.='status=status ';
          $sql_updt.='where idnum1='.$idnum1;
          // cambiar true=1 false=0
          //   $res_updt=msql_query($sql_updt,$link_data);
          $res_updt=msql_query($sql_updt,$link_data);
          $div=$consolelog->appendChild($dom->createElement("sqlupdt1",$sql_updt)); 
//           echo '*/*/*/*'.$sql_updt;
     }
     
     if(!empty($idnum1))
     {
          if($deta_ok==1 and !empty($idnum1) and !empty($item))
          {
               if(mb_stristr2('.vtas.clie.comp.prov.carr',$tabla) and ($cant<0 or $puni<0))
               {
                    // descuento directo
//                     if(substr($item,0,4)!='9999')
                    {
                         $sql_docu='select * from '.$tabla1.' left join '.$tabla2.' on idnum1=idnum2 ';
                         $sql_docu=calc_fields($tabla,$sql_docu);
                         $sql_docu.='where idnum1='.$idnum1.' ';
                         if(substr($item,0,4)!='9999')
                         {$sql_docu.='and item="'.$item.'" ';}
                         $res_docu=msql_query($sql_docu,$link_data);
                         $tsubt=msql_result($res_docu,0,'tsubt');
                         $tdctod=abs($cant*$puni);
//                          $pdctod=$tsubt/$tdctod;
                         $pdctod=$tdctod/$tsubt*100;
                         
                         $sql_updt='update '.$tabla2.' set dcto='.$pdctod.' where idnum2='.$idnum1.' ';
                         if(substr($item,0,4)!='9999')
                         {$sql_updt.='and item="'.$item.'" ';}
                         $res_updt=msql_query($sql_updt,$link_data);
                         
                    }
                    
                    if(substr($item,0,4)=='9999')
                    {
                         
                    }
                    $deta_ok=0;
               }
               
          }               
          
          if($numfac_ok==1){$deta_ok=0;}
          
          if($deta_ok==1)
          {
               if(empty($iddet))
               {
                    $iddet=get_iddet($docu,$idnum1);
                    $sql_inse='insert into '.$tabla2.'(idnum2,iddet)value("'.$idnum1.'","'.$iddet.'")';
                    $res_inse=msql_query($sql_inse,$link_data);
               }
               
               $sql_stru='describe '.$tabla2;
               $res_stru=msql_query($sql_stru,$link_data);

               $sql_updt ='';
               $sql_updt.='update '.$tabla2.' set ';
               while($aso_stru=@msql_fetch_assoc($res_stru))
               { 
                    $xvalor=${$aso_stru['Field']};
                    $xtype=$aso_stru['Type'];
                    if($xvalor!='')
                    {$sql_updt.=$aso_stru['Field'].'="'.$xvalor.'", ';}
               }
               
//                echo '======'.$sql_updt;
               
               if ($tabla2 == 'liup2'){$sql_updt.='bateo=iddet, ';}
               
               $sql_updt.='iuser2='.$user_codigo.', ';
               $sql_updt.='idate2=now(), ';
               $sql_updt.='iddet=iddet ';
               $sql_updt.='where idnum2='.$idnum1.' ';
               $sql_updt.='and iddet='.$iddet;
               $res_updt=msql_query($sql_updt,$link_data);
               $div=$consolelog->appendChild($dom->createElement("sqlupdt2",$sql_updt)); 
               
               if($tabla=='inve' and substr($docu,0,2)=='EB')
               {cost_prom($item);}
               
          }

          if($numfac_ok==1 and !empty($idnum1))
          {
               $sql_updt='update '.$tabla1.' set mdfrom="'.$tfac.'", idfrom="'.$idfact.'" where idnum1='.$idnum1.' ';
               $res_updt=msql_query($sql_updt,$link_data);
               
               msql_data_seek($res_fact);
               while($aso_fact=msql_fetch_assoc($res_fact))
               {
                    $item=$aso_fact['item'];
                    $cant=$aso_fact['cant'];
                    $puni=$aso_fact['puni'];
                    $lote=$aso_fact['lote'];
                    $seri=$aso_fact['seri'];
                    $numserie=$aso_fact['numserie'];
                    $detalle=$aso_fact['detalle'];
                    
//                     $puni=1.5;
                    
                    $sql_seek='select * from '.$tabla2.' where idnum2='.$idnum1.' and item="'.$item.'" ';
                    $res_seek=msql_query($sql_seek,$link_data);
                    
                    if(msql_num_rows($res_seek)!=0)
                    {$iddet=msql_result($sql_seek,0,'iddet');}
                    
                    if(msql_num_rows($res_seek)==0)
                    {
                         $iddet=get_iddet($docu,$idnum1);
                         $sql_inse='insert into '.$tabla2.'(idnum2,iddet)value("'.$idnum1.'","'.$iddet.'")';
                         $res_inse=msql_query($sql_inse,$link_data);
                    
                         if($cant>0)
                         {
                              $sql_stru='describe '.$tabla2;
                              $res_stru=msql_query($sql_stru,$link_data);

                              $sql_updt ='';
                              $sql_updt.='update '.$tabla2.' set ';
                              while($aso_stru=@msql_fetch_assoc($res_stru))
                              { 
                                   $xvalor=${$aso_stru['Field']};
                                   $xtype=$aso_stru['Type'];
                                   if($xvalor!='')
                                   {$sql_updt.=$aso_stru['Field'].'="'.$xvalor.'", ';}
                              }

                              $sql_updt.='iuser2='.$user_codigo.', ';
                              $sql_updt.='idate2=now(), ';
                              $sql_updt.='iddet=iddet ';
                              $sql_updt.='where idnum2='.$idnum1.' ';
                              $sql_updt.='and iddet='.$iddet;
                              $res_updt=msql_query($sql_updt,$link_data);
                              $div=$consolelog->appendChild($dom->createElement("sqlupdt2",$sql_updt)); 

                         }
                    }
                    
//                     $sql_inse                    
               }
               
//                $sql_dele='delete from '.$tabla2.' where idnum2='.$idnum1.' and cant*puni=0';
//                $res_dele=msql_query($sql_dele,$link_data);
               
          }
          
          
          if($tabla=='retp')
          {
               $sql_dele='delete from retp2 where idnum2="'.$idnum1.'" and ifnull(cant,0)=0 ';
               $res_dele=msql_query($res_dele,$link_data);
               
               for($xid=1;$xid<=9;$xid++)
               {
                    $cair=${'cair_codi_'.$xid};
                    $cant=${'cair_base_'.$xid};
                    $puni=${'cair_porc_'.$xid};
                    
                    // if(!empty($cair) and !empty($cant))
                    if(!empty($cair) and !empty($cant) and !empty($puni))
                    {
                         {
                              $cair0=data_maes('cair',$cair);
                              $puni=$cair0['cair_porc'];
                         }
                         
                         $sql_rete='select * from retp2 where idnum2="'.$idnum1.'" and cair="'.$cair.'" and cant="'.$cant.'" ';
                         $res_rete=msql_query($res_rete,$link_data);
                         
                         if(msql_num_rows($res_rete)==0)
                         {
                              $iddet=get_iddet($docu,$idnum1);
                              $sql_inse='insert into '.$tabla2.'(idnum2,iddet)value("'.$idnum1.'","'.$iddet.'")';
                              $res_inse=msql_query($sql_inse,$link_data);
                         }
                         
                         if(msql_num_rows($res_rete)!=0)
                         {$iddet=msql_result($res_rete,0,'iddet');}

                         
                         $sql_updt='update '.$tabla2.' set ';
                         $sql_updt.='cant="'.$cant.'", ';
                         $sql_updt.='puni="'.$puni.'", ';
                         $sql_updt.='cair="'.$cair.'", ';
                         $sql_updt.='docum="'.$docum.'", ';
                         $sql_updt.='ptoem="'.$ptoem.'", ';
                         $sql_updt.='numer="'.$numer.'", ';
                         $sql_updt.='idate2=now(), ';
                         $sql_updt.='where idnum2="'.$idnum1.'" and iddet="'.$iddet.'"';
                         $res_updt=msql_query($sql_updt,$link_data);

                         
                         $sql_updt='update comp1 ';
                         $sql_updt.='left join comp2 on idnum1=idnum2 ';
                         $sql_updt.='set cair="'.$cair.'", ';
                         $sql_updt.='where ';
                         $sql_updt.='ifnull(cair,"")="" ';
                         $sql_updt.='and docu="'.$docum.'" ';
                         $sql_updt.='and numero="'.$numer.'" ';
                         $sql_updt.='and enti like "'.substr($enti,0,10).'%" ';
                         $sql_updt.='and fecha>="'.date('Y-m-01',strtotime('-5 days')).'" ';
                         $res_updt=msql_query($sql_updt,$link_data);
                         
                         
//                          $alert_save=$sql_updt;
//                          echo '*/*/* '.$sql_updt;
                         
                         
                         
//                          echo '*/*/*'.$sql_updt;
                    }
                    
               }
               
               for($xid=1;$xid<=9;$xid++)
               {
                    $cair=${'iva_codi_'.$xid};
                    $cant=${'iva_base_'.$xid};
                    $puni=${'iva_porc_'.$xid};
                    
                    $cair="-";
                    if(!empty($cant) and !empty($puni))
                    {
                         {
//                               $cair0=data_maes('cair',$cair);
//                               $puni=$cair0['cair_porc'];
                         }
                         
                         $sql_rete='select * from retp2 where idnum2="'.$idnum1.'" and cair="'.$cair.'" and cant="'.$cant.'" ';
                         $res_rete=msql_query($res_rete,$link_data);
                         
                         if(msql_num_rows($res_rete)==0)
                         {
                              $iddet=get_iddet($docu,$idnum1);
                              $sql_inse='insert into '.$tabla2.'(idnum2,iddet)value("'.$idnum1.'","'.$iddet.'")';
                              $res_inse=msql_query($sql_inse,$link_data);
                         }
                         
                         if(msql_num_rows($res_rete)!=0)
                         {$iddet=msql_result($res_rete,0,'iddet');}
                         
                         $sql_updt='update '.$tabla2.' set ';
                         $sql_updt.='cant="'.$cant.'", ';
                         $sql_updt.='puni="'.$puni.'", ';
                         $sql_updt.='cair="'.$cair.'", ';
                         $sql_updt.='docum="'.$docum.'", ';
                         $sql_updt.='ptoem="'.$ptoem.'", ';
                         $sql_updt.='numer="'.$numer.'", ';
                         $sql_updt.='idate2=now(), ';
                         $sql_updt.='where idnum2="'.$idnum1.'" and iddet="'.$iddet.'"';
                         $res_updt=msql_query($sql_updt,$link_data);
                    }
                    
               }
               
          }
          
          
          if($tabla=='hcli')
          {
               // vacunas
               $sql_vacu='select * from item0 ';
               $sql_vacu.='where item_status not in ("I","X") ';
               $sql_vacu.='and (item_descri like "%VACUN%" or item_lsub like "%VAC%")';
               $sql_vacu.='order by item_lsub, item_codigo ';
               $res_vacu=msql_query($sql_vacu,$link_data);
               
               while($aso_vacu=msql_fetch_assoc($res_vacu))
               {
                    $vacu_codigo=$aso_vacu['item_codigo'];
                    $vacu_descri=$aso_vacu['item_descri'];
                    if(${$vacu_codigo}=='true')
                    {
//                          $iddet=get_iddet($docu,$idnum1);
                         $sql_inse='insert into '.$tabla2.'(idnum2,iddet)value("'.$idnum1.'","'.$iddet.'")';
                         $res_inse=msql_query($sql_inse,$link_data);

                         $sql_updt ='';
                         $sql_updt.='update '.$tabla2.' set ';
                         $sql_updt.='fase="'.$fase.'", ';
                         $sql_updt.='item="'.$vacu_codigo.'", ';
                         $sql_updt.='cant=1, ';
                         $sql_updt.='iuser2='.$user_codigo.', ';
                         $sql_updt.='idate2=now(), ';
                         $sql_updt.='iddet=iddet ';
                         $sql_updt.='where idnum2='.$idnum1.' ';
                         $sql_updt.='and iddet='.$iddet;
                         $res_updt=msql_query($sql_updt,$link_data);
                         
                         $iddet++;
                    }
               }
          }
         
          
     }     
     
     if(!empty($idnum1) and $docu=='PLA1')
     {
          plan_atvd($idnum1);
     }
     
     
}

if(!empty($error_save2)){$error_save.=$error_save2;}

if(empty($error_save) and !empty($alert_save))
{
     $msgswal->appendChild($dom->createElement("icon","warning")); 
     $msgswal->appendChild($dom->createElement("title","Advertencia")); 
     $msgswal->appendChild($dom->createElement("text",$alert_save)); 
     $msgswal->appendChild($dom->createElement("timeout","10000")); 
}


if(!empty($error_save))
{
     $msgswal->appendChild($dom->createElement("icon","error")); 
     $msgswal->appendChild($dom->createElement("title","Error")); 
     $msgswal->appendChild($dom->createElement("text",$error_save)); 
}


echo $dom->saveXML();


if(!empty($docu) and !empty($idnum1))
{$claveacc=gen_clacc($docu,$idnum1);}
     
     
     
?>
