<?php



// ini_set('display_errors',1);

//error_reporting(E_ALL);





session_start();

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

require_once ("../app/guiaremision/cGuiaremision.php");

$oGuiaremision = new cGuiaremision();

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 = 'GUIA_PRODUCCION.wsdl';



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





 $headers = new CustomHeaders('2056019812519194457','Ferreteria19'); 



//echo $_POST['nomfile'];



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

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

//$fcs = $client->__getFunctions();





try {



///echo trim($_POST['name_zip']);



	$fileNam = $_POST['nomfile']; //'20479664341-09-G001-00000003.zip';

	$filezip = $_POST['name_zip'];

	

	$guia_id = $_POST['guia_id']; 

	$urlxml = $_POST['xmlarchivo'];

	$nomxml = $_POST['nomxml'];

		

	$params = array('fileName' =>$fileNam, 'contentFile' => file_get_contents($filezip));



	/* $status = $client->sendSummary($params); */

	/* $status = $client->__soapCall("sendBill",$params); */

	

	$status = $client->sendBill($params);

	

	$status = $status->applicationResponse;

	

	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['code'] = $code;

	$data['msj'] = $pro.' ::: '.$descrip;

	

	

	

if($code == '0'){	



if($guia_id!=""){

 $est = 1;

 $oGuiaremision->act_sunat($guia_id,$est);

 }

}

	$data['est'] = '1';

	echo json_encode($data);

	

} catch(Exception $e) {

	/* echo 'Error: ' . $e->getCode() . ', ' . utf8_decode($e->getMessage()); */

	$data['msj'] = 'Error: ' . 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);

	}

}



?>