<?php 
session_start();
foreach($_GET as $campo => $valor){${$campo}=trim($valor); $_PARAMS[$campo]=trim($valor);} 
foreach($_POST as $campo => $valor){${$campo}=trim($valor); $_PARAMS[$campo]=trim($valor);} 

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

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

$error_save='';

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

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

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

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

if(!empty($enti) and !empty($nombre))
{
     $sql_maes='';
     $sql_maes.='update enti0 set enti_descri="'.$nombre.'" ';
     $sql_maes.='where enti_codigo like "'.substr($enti,0,10).'%" ';
     $sql_maes.='and (enti_descri is null or trim(enti_descri)="" or enti_descri like "%***%") ';     
     $res_maes=msql_query($sql_maes,$link_data);
}

if(!empty($item) and !empty($producto))
{
     $sql_maes='';
     $sql_maes.='update item0 set item_descri="'.$producto.'" ';
     $sql_maes.='where item_codigo like "'.$producto.'" ';
     $sql_maes.='and (item_descri is null or trim(item_descri)="" or item_descri like "%***%") ';     
     $res_maes=msql_query($sql_maes,$link_data);
}

$error_save='';
if($tabla=='vtas')
{
//      if(empty($enti)){$enti='9999999999999';}
//      if(empty($item)){$item='9999999999';}
     if(empty($enti)){$error_save.='/Error en ENTIDAD ';}
//      if(empty($item)){$error_save.='/Error en ITEM ';}
}    
     
if(!empty($error_save))
{
     $msgswal->appendChild($dom->createElement("icon","error")); 
     $msgswal->appendChild($dom->createElement("title","Error")); 
     $msgswal->appendChild($dom->createElement("text",$error_save)); 
}

if(empty($error_save))
{
     
     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 not in("O","X") ';
          $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=='vtas')
     {
          if(empty($puni)){$puni=1;}

          $ptov0=data_maes('ptov',$ptov);
          $ptoe=$ptov0['ptov_ptoe'];

//           if(empty($numero)){$numero=new_numero($_PARAMS);}

          $sql_query ='select * from '.$tabla1.' ';
          $sql_query.='where status not in("O","X") ';
          $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.='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)
          {
               $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,ptov,numero,fecha,enti,numero,status,iuser,idate)';
               $sql_inse.='value("'.$docu.'","'.$ptoe.'","'.$ptov.'","'.$numero.'","'.$fecha.'","'.$enti.'","'.$numero.'","-","'.$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=='inve')
     {
          if(empty($puni)){$puni=1;}
          $bode0=data_maes('bode',$bode);
          $ptoe=$bode0['bode_ptoe'];

          $sql_query ='select * from '.$tabla1.' ';
          $sql_query.='where status not in("O","X") ';
          $sql_query.='and docu="'.$docu.'" ';
          $sql_query.='and bode="'.$bode.'" ';
          $sql_query.='and fecha="'.$fecha.'" ';
          $sql_query.='and left(enti,10)="'.substr($enti,0,10).'" ';
          if(!empty($numfac))
          {$sql_query.='and numfac="'.$numfac.'" ';}
          $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,bode,numero,fecha,enti,numfac,status,iuser,idate)';
               $sql_inse.='value("'.$docu.'","'.$ptoe.'","'.$bode.'","'.$numero.'","'.$fecha.'","'.$enti.'","'.$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');
          }
     }

     $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,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_stristr('FP10.FP15.RCLI.',$docu))
               $sql_docu.='and enti like "'.substr($enti,0,10).'%" ';
          //      if(mb_stristr('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)); 
     }
     
     if(!empty($idnum1))
     {
          $deta_ok=1;
          //      cambiar true=1 false=0
          if(empty($cant) and empty($puni)){$deta_ok=0;}
          if(mb_stristr('vtas.clie.carr.comp.prov.inve.prod.',$tabla) and empty($item)){$deta_ok=0;}
          
          if($deta_ok==1)
          {
               $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.'", ';
//                          $deta_ok=1;
                    }
               }
               
          }

          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_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_updt='';
     //     $sql_updt.='update '.$tabla.'2 set ';
     //     $sql_updt.='idate2=now() ';
     //     $sql_updt.='where idnum2='.$idnum1;
     //     $sql_updt.=' and iddet='.$iddet;
     //     $res_updt=@msql_query($sql_updt,$link_data);

     }     
}


echo $dom->saveXML();
     
     
     
?>

