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

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=='O' or $status=='X')
     {
          $sql_query ='update '.$tabla1.' ';
          $sql_query.='set ';
          if($status=='O')
          {
               $sql_query.='odate=now(), ';
               $sql_query.='ouser="'.$_SESSION['user_codigo'].'", ';
          }
          if($status=='X')
          {
               $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="'.$idnum1.'" ';
          $sql_query.='limit 1 ';
          $res_query=msql_query($sql_query,$link_data);
          
          if($docu0['docu_codigo']=='EB13')
          {
               $sql_doc2='select * from inve1 where docu="IB13" and idfrom="'.$idnum1.'" order by idnum1 desc';
               $res_doc2=msql_query($sql_doc2,$link_data);
               $iddoc2=msql_result($res_doc2,0,'idnum1');
               
               $sql_query2=str_replace('"'.$idnum1.'"','"'.$iddoc2.'"',$sql_query);
               $res_query2=msql_query($sql_query2,$link_data);
          }
          
     }
     
     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);
     }
}
     
//      $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();
     
?>

