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

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',$empr);

$docu0=data_maes('docu',$docu1);


$error='';
$docuo=data_docu($docu1,$idnum1);


$xplazo=$docuo['plazo'];
if($xplazo==0){$xplazo=3;}
if($empr0['empr_codigo']=='imcp'){$xplazo=30;}


if($docuo['status']=='F')
{$error='Documento : '.$docuo['docu'].'-'.$docuo['numero'].' YA ESTA FACTURADO';}
// elseif($vence<=date('Y-m-d'))
// {$error='Documento : '.$docuo['docu'].'-'.$docuo['numero'].' VENCIDO';}



if($empr0['empr_codigo']=='demo')
{
     $vence=date('Y-m-d',strtotime($docuo['fecha'].' + '.$xplazo.' days'));
     if($docuo['status']=='F')
     {$error='Documento : '.$docuo['docu'].'-'.$docuo['numero'].' YA ESTA FACTURADO';}
     elseif($vence<=date('Y-m-d'))
     {$error='Documento : '.$docuo['docu'].'-'.$docuo['numero'].' VENCIDO';}
}



// echo '====='.$docu1.$idnum1.$docu2.$ptov;

if(empty($error))
{
     $newid=clon_docu($docu1,$idnum1,$docu2,$ptov);
     $docu2=data_docu($docu2,$newid);
}

$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");

     if(empty($error))
     {
          $msgswal->appendChild($dom->createElement("icon","question")); 
          $msgswal->appendChild($dom->createElement("title","Nuevo : ".$docu2['docu'].' - '.$docu2['numero'])); 
          $msgswal->appendChild($dom->createElement("text","Desea Editarlo ? ")); 
          $msgswal->appendChild($dom->createElement("confirmButtonText","Editar Ahora!")); 
          $msgswal->appendChild($dom->createElement("denyButtonText","Después")); 
          $msgswal->appendChild($dom->createElement("showDenyButton","true")); 
          $xregi=$docu0['docu_tabla'];
          if($xregi=='clie'){$xregi='vtas';}
          if($xregi=='otbj'){$xregi='vtas';}
          $msgswal->appendChild($dom->createElement("isConfirmed",'{window.open("?scri=regi_'.$xregi.'3&modu='.$docu2['docu_tabla'].'&docu='.$docu2['docu'].'&idnum1='.$docu2['idnum1'].'&tpos=3","_self");}')); 
     }

     if(!empty($error))
     {
          $msgswal->appendChild($dom->createElement("icon","error")); 
//           $msgswal->appendChild($dom->createElement("title","ERROR : ".$docu1['docu'].' - '.$docu1['numero'])); 
          $msgswal->appendChild($dom->createElement("title","ERROR")); 
          $msgswal->appendChild($dom->createElement("text",$error)); 
//           $msgswal->appendChild($dom->createElement("confirmButtonText","Editar Ahora!")); 
//           $msgswal->appendChild($dom->createElement("denyButtonText","Después")); 
//           $msgswal->appendChild($dom->createElement("showDenyButton","true")); 
//           $xregi=$docu0['docu_tabla'];
//           if($xregi=='clie'){$xregi='vtas';}
//           $msgswal->appendChild($dom->createElement("isConfirmed",'{window.open("?scri=regi_'.$xregi.'3&modu='.$docu0['docu_tabla'].'&docu='.$docu1['docu'].'&idnum1='.$docu1['idnum1'].'&tpos=3","_self");}')); 
     }

echo $dom->saveXML();
     
?>