<?php 
declare(strict_types=0);
// 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;

$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';


$error_prcs='';

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

if(!empty($idnum1))
{
     if($status=='N')
     {
//           $idncre=clon_docu($docu,$idnum1,'NC10');
//      $msgswal->appendChild($dom->createElement("icon","error")); 
//      $msgswal->appendChild($dom->createElement("title","Error")); 
//      $msgswal->appendChild($dom->createElement("text",'NOTA DE CREDITO')); 
     }
          
     if($status=='A')
     {
          if(substr($docu,0,2)=='PS')
          {
               $sql_docu='select * from '.$tabla2.' ';
               $sql_docu.='where idnum2="'.$idnum1.'" ';
               $sql_docu.='and item like "99%" ';
               $sql_docu.='and cant>1/100 ';
               $res_docu=msql_query($sql_docu,$link_data);
               if(msql_num_rows($res_docu)!=0){$error_prcs='ERROR en ITEMS "9999" ';}
               
          }
          
          if(empty($error_prcs))
          {
               $sql_query ='update '.$tabla1.' ';
               $sql_query.='set ';
               $sql_query.='adate=now(), ';
               $sql_query.='auser="'.$_SESSION['user_codigo'].'", ';
     //           $sql_query.='concepto=concat(ifnull(concepto,"")," :: ","'.$motivo.'"), ';
               $sql_query.='status="'.$status.'" ';
               $sql_query.='where idnum1="'.$idnum1.'" ';
               $sql_query.='limit 1 ';
               $res_query=msql_query($sql_query,$link_data);
          }
     }

     if($status=='R')
     {    
          $docu2='';
          if(substr($docu,0,2)=='PC'){$docu2='FC10';}
          if(substr($docu,0,2)=='PF'){$docu2='FC10';}
          if(substr($docu,0,2)=='PS'){$docu2='PP10';}
          $docu2=data_maes('docu',$docu2);
          
          if(!empty($docu2['docu_codigo']))
          {
               $iddocu2=clon_docu($docu,$idnum1,$docu2['docu_codigo']);
               
               $sql_query ='update '.$docu2['docu_tabla'].'1 ';
               $sql_query.='set ';
               $sql_query.='fecha=now(), ';
     //           $sql_query.='xdate=now(), ';
     //           $sql_query.='xuser="'.$_SESSION['user_codigo'].'", ';
     //           $sql_query.='concepto=concat(ifnull(concepto,"")," :: ","'.$motivo.'"), ';
               $sql_query.='status=status ';
               $sql_query.='where idnum1="'.$iddocu2.'" ';
               $sql_query.='limit 1 ';
               $res_query=msql_query($sql_query,$link_data);
               
               $msgswal->appendChild($dom->createElement("icon","info")); 
               $msgswal->appendChild($dom->createElement("title","Info")); 
               $msgswal->appendChild($dom->createElement("text",$docu2['docu_descri'])); 
//                $msgswal->appendChild($dom->createElement("text",$sql_query)); 
          }
          
     }
     
     
     if($status2=='X')
     {
//           $sql_query ='update '.$tabla2.' ';
//           $sql_query.='set status2="'.$status2.'" ';
//           $sql_query.='set cant=0, puni=0 ';
          
          if(intval($iddet)>0)
          {
               $sql_query ='delete from '.$tabla2.' ';
               $sql_query.='where idnum2="'.$idnum1.'" ';
               $sql_query.='and iddet="'.$iddet.'" ';
               $sql_query.='limit 1 ';
          }               
          
          
          if($iddet=='ALL')
          {
               $sql_query ='delete from '.$tabla2.' ';
               $sql_query.='where idnum2="'.$idnum1.'" ';
               $sql_query.='limit 20 ';
          }               
          
          
          $res_query=msql_query($sql_query,$link_data);
     }
}

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


     
//      $msgswal->appendChild($dom->createElement("icon","error")); 
//      $msgswal->appendChild($dom->createElement("title","Error")); 
//      $msgswal->appendChild($dom->createElement("text",$error)); 

// $consolelog=$response->appendChild($dom->createElement("consolelog",$sql_query));
// $consolelog=$response->appendChild($dom->createElement("msgalert",$sql_query));



echo $dom->saveXML();
     
     
     
?>

