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

// ini_set('memory_limit', '8192M');
// $fecha1='';
// $fecha2='';

$maxsize=30;
if(empty($maxsize)){$maxsize=5;}
$maxbytes=$maxsize*1024*1024;

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

$tabla='fdoc';
$docu='CONT';
$idnum=1;
$tabla0=$tabla.'0';


if(empty($docu))
     {
          $msgswal->appendChild($dom->createElement("icon","error")); 
          $msgswal->appendChild($dom->createElement("title","Error")); 
          $msgswal->appendChild($dom->createElement("text","Documento NO VALIDO")); 
     }
elseif(empty($idnum))
     {    
          $msgswal->appendChild($dom->createElement("icon","error")); 
          $msgswal->appendChild($dom->createElement("title","Error")); 
          $msgswal->appendChild($dom->createElement("text","ID NO VALIDO")); 
     }
elseif(empty($_FILES['xfile']['name']))
     {    
          $msgswal->appendChild($dom->createElement("icon","error")); 
          $msgswal->appendChild($dom->createElement("title","Error")); 
          $msgswal->appendChild($dom->createElement("text","Archivo NO VALIDO")); 
     }
elseif($_FILES['xfile']['size']>$maxbytes)
     {    
          $msgswal->appendChild($dom->createElement("icon","error")); 
          $msgswal->appendChild($dom->createElement("title","Error")); 
          $msgswal->appendChild($dom->createElement("text","Archivo Mayor al limite permitido : ".$maxsize."MB")); 
     }
else
     {
          if(empty($iddet)){$iddet='';}
          $filetype=$_FILES['xfile']['type'];
     
          if(mb_stristr($filetype,'excel')){$exte='xls';}
          if(mb_stristr($filetype,'text')){$exte='csv';}
     
          $filetype=$exte;
     
          $xpath='../'.$empr.'/'.$tabla.'/';
          $xcad=date("is").rand(0,9);
          $xname2=$docu.'_'.$idnum.'_'.$iddet.'_'.$xcad.'.'.$filetype;
          $move_ok=move_uploaded_file($_FILES['xfile']['tmp_name'],$xpath.$xname2);
          
          $sql_fdoc ='insert into ';
          $sql_fdoc.=''.$tabla0.'('.$tabla.'_descri,'.$tabla.'_status,'.$tabla.'_docu,'.$tabla.'_idnum,'.$tabla.'_iddet,'.$tabla.'_file,'.$tabla.'_iuser,'.$tabla.'_idate)';
          $sql_fdoc.='value("'.$_FILES['xfile']['name'].' - '.date('Y-m-d H:i:s').'","-","'.$docu.'","'.$idnum.'","'.$iddet.'","'.$xname2.'","'.$user_codigo.'",now())';
          $res_fdoc=msql_query($sql_fdoc,$link_data);

          $msgswal->appendChild($dom->createElement("icon","success")); 
          $msgswal->appendChild($dom->createElement("title","Archivo Cargado...".$msg_ok)); 
          $msgswal->appendChild($dom->createElement("text",$exte.'=='.$docu.'-'.$idnum.'-'.$iddet.' = '.($_FILES['xfile']['name']))); 
     
          $msgswal->appendChild($dom->createElement("isConfirmed",'{location.reload();}'));
     
     }
echo $dom->saveXML();


?>
