<?php
//ini_set('display_errors',1);
session_start();
require_once ("../config/Cado.php");
require_once ("../app/venta/cVenta.php");
$oVenta = new cVenta();
require_once ("../app/notadebito/cNotadebito.php");
$oNotadebito = new cNotadebito();
require_once ("../app/notacredito/cNotacredito.php");
$oNotacredito = new cNotacredito();
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'];


//$headers = new CustomHeaders('20560198125MODDATOS','MODDATOS'); 

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


if($_POST['emp_ruc']=="20608474031"){
	
	$headers = new CustomHeaders('20608474031PERU2024','Peru1076');
	
}else if($_POST['emp_ruc']=="20612796620"){
	
	$headers = new CustomHeaders('20612796620PERU2024','Peru1076');
	
}else if($_POST['emp_ruc']=="20601924006"){
	
	$headers = new CustomHeaders('20601924006PERU2024','Peru1076');
	
}else if($_POST['emp_ruc']=="20613547771"){
	
	$headers = new CustomHeaders('20613547771PERU2024','Peru1076');
	
}


$client = new SoapClient($service, ['cache_wsdl' => WSDL_CACHE_NONE, 'trace' => true , 'soap_version' => SOAP_1_1 ]);
$client->__setSoapHeaders([$headers]);
//$fcs = $client->__getFunctions();

	$fileNam = $_POST['nomfile']; //'20479987948-01-F001-00000053.zip';
	$filezip = $_POST['name_zip']; 
	$venid = $_POST['id_ven']; 
	$urlxml = $_POST['xmlarchivo'];
	$nomxml = $_POST['nomxml'];
		
		
try {
	

	$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 /::';
	}
	
	$men = $descrip;
	$data['code'] = $code;
	$data['msj'] = $pro.' ::: '.$descrip;
	
	unlink('../doccdrfiles/R-'.$nomxml.'.xml');
	
	
	//$data['men'] = $men;
	
	$data['est'] = '1';
	echo json_encode($data);
	
	
} catch(Exception $e) {
	/* echo 'Error: ' . $e->getCode() . ', ' . utf8_decode($e->getMessage()); */
	$data['msj'] = '' . utf8_decode($e->getMessage());
    $men = utf8_decode($e->getMessage());
	$data['est'] = '0';
	$data['code'] = '';
	echo json_encode($data);
}
 
 
$menbus1 = 'El comprobante fue registrado previamente con otros datos';
$ban1 = strpos($men, $menbus1);
		
 if($ban1 === false ){}
 else{

	if($venid>0){

	$est = 1;
	if($_POST['tipcom']=='08'){
	$oNotadebito->act_sunat($venid,$est);
	}else if($_POST['tipcom']=='07'){
	$oNotacredito->act_sunat($venid,$est);
	}else{
	$oVenta->act_sunat($venid,$est);
	
	}
	}
}
	
 
$menbus2 = 'ha sido aceptada';
$ban2 = strpos($men, $menbus2);

 if($ban2 === false ){}
 else{

	if($venid>0){

	$est = 1;
	if($_POST['tipcom']=='08'){
	$oNotadebito->act_sunat($venid,$est);
	}else if($_POST['tipcom']=='07'){
	$oNotacredito->act_sunat($venid,$est);
	}else{
	$oVenta->act_sunat($venid,$est);
	
	}
	}
}	
	
	

 
 
 /*
 
$menbus3 = 'ya esta informado y se encuentra con estado anulado o rechazado';
$ban3 = strpos($men, $menbus3);

 if($ban3 === false ){}
 else{

	if($venid>0){

	$est = 2;
	if($_POST['tipcom']=='08'){
	$oNotadebito->act_sunat($venid,$est);
	}else if($_POST['tipcom']=='07'){
	$oNotacredito->act_sunat($venid,$est);
	}else{
	$oVenta->act_sunat($venid,$est);
	
	}
	}
}	
 
 */
 
 
 
 
 
 
 
 
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);
	}
}
?>