<?php

  //ini_set('display_errors',1);
  
  //error_reporting(E_ALL);
  

session_start();
require_once ("../config/Cado.php");

require_once ("../app/comunicacionbaja/cComunicacionbaja.php");
$oComunicacionbaja = new cComunicacionbaja();

date_default_timezone_set('America/Lima');
//$service = 'https://www.sunat.gob.pe/ol-ti-itemision-otroscpe-gem-beta/billService?wsdl'; //comprobante de percepcion
/* $service = 'https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService?wsdl'; */
/* $service = 'https://e-beta.sunat.gob.pe/ol-ti-itemision-otroscpe-gem-beta/billService?wsdl'; */
//$service = 'FACTURA_PRODUCCION.wsdl';
//echo 'tip -'.$_POST['tipcom'];

//$service = 'https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService?wsdl';

//$service = 'FACTURA_PRODUCCION.wsdl';

 $headers = new CustomHeaders('20602247075PERU2021','Peru1076'); 

$client = new SoapClient($service, ['cache_wsdl' => WSDL_CACHE_NONE, 'trace' => true , 'soap_version' => SOAP_1_1 ]);

$client->__setSoapHeaders([$headers]);

//echo  $client->__getFunctions();

	$fileNam = $_POST['nomfile']; //'20479987948-01-F001-00000053.zip';
	$filezip = $_POST['name_zip']; 
	$combajid = $_POST['combajid']; 
	$urlxml = $_POST['xmlarchivo'];
	$nomxml = $_POST['nomxml'];
		
	$params = array('fileName' => $fileNam, 'contentFile' => file_get_contents($filezip));
	
	
	
	
	

try {
	
	$result = $client->sendSummary($params);
	
	$ticket = $result->ticket;
	
	//echo 'RESPUESTA: '.$ticket;
	
	$men = $ticket;
	
	$ban = substr($men, 0,55);
	
 if($ban == 'La numeracion o nombre del documento ya ha sido enviado' ){

	$est = '2';
	$oComunicacionbaja->act_sunat($combajid,$est);
 
}	
	
	
	/* $status = $client->sendSummary($params); */
	/* $status = $client->__soapCall("sendBill",$params); */
	//$status = $client->sendBill($params);
	//$result = $client->__soapCall("sendSummary",$params);
	//echo $result;
		
	
	
	//print_r($client->__getFunctions());	
	//$status = $status->applicationResponse;
	

	
	//echo 'RESPUESTA: '.$client; 
	
	
	//file_put_contents('../doccdr/R-'.$fileNam,$status);
	
	

	
	/* $zip = new ZipArchive();
	$zip->open('../doccdr/R-'.$fileNam);
	$zip->extractTo('../doccdrfiles/');
	$zip->close(); */
	
	/*
	$xml1 = new DOMDocument();
	$xml1->preserveWhiteSpace = FALSE;
	$xml1->load('../doccdrfiles/R-'.$nomxml.'.xml');
	
	$s= $xml1->getElementsByTagName("Description");
	foreach($s as $node1){
		$descrip =  $node1->nodeValue;
	}
	
	$c= $xml1->getElementsByTagName("ResponseCode");
	foreach($c as $node2){
		$code =  $node2->nodeValue;
	}
	
	$sv= $xml1->getElementsByTagName("SignatureValue");
	foreach($sv as $node3){
		$sigval =  $node3->nodeValue;
	}
	if($sigval == "*Private key 'BetaPublicCert' not up*"){
		$pro = 'BetaPublicCert ::/ SERVIDOR DE PRUEBAS /:: ';
	}else{
		$pro = '::/ DESDE EL SERVIDOR DE PRODUCCION /::';
	} */
	
	//$data['msj'] = $pro.' ::: '.$descrip;
	$data['msj'] = 'TICKET : '.$ticket;
	
	//unlink('../doccdrfiles/R-'.$nomxml.'.xml');
	

if($ticket>0){
	$oComunicacionbaja->modificar_campo($combajid,'tic',$ticket);
}

	$data['est'] = '1';
	echo json_encode($data);
	
} catch(Exception $e) {
	/* echo 'Error: ' . $e->getCode() . ', ' . utf8_decode($e->getMessage()); */
	$data['msj'] = '' . utf8_decode($e->getMessage());
	$data['est'] = '0';
	echo json_encode($data);
}
 
class CustomHeaders extends SoapHeader {
	private $wss_ns = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';
	
	function __construct($user, $pass, $ns = null) {
		if($ns){
			$this->wss_ns = $ns;
		}
		
		$auth = new stdClass();
		$auth->Username = new SoapVar($user, XSD_STRING, null, $this->wss_ns, null, $this->wss_ns);
		$auth->Password = new SoapVar($pass, XSD_STRING, null, $this->wss_ns, null, $this->wss_ns);
		
		$username_token = new stdClass();
		$username_token->UsernameToken = new SoapVar($auth, SOAP_ENC_OBJECT, null, $this->wss_ns, 'UsernameToken', $this->wss_ns);
		$security_sv = new SoapVar(new SoapVar($username_token, SOAP_ENC_OBJECT, null, $this->wss_ns, 'UsernameToken', $this->wss_ns)
								 , SOAP_ENC_OBJECT, null, $this->wss_ns, 'Security', $this->wss_ns);
		
		parent::__construct($this->wss_ns, 'Security', $security_sv, true);
	}
}

?>
