<?php
/*
Códigos de error MySQL:
1062 : Duplicado
*/
require_once('wsConstantes.php');

class wsCOFIDIExec extends wsConstantes
{
	var $classQuery;
	var $conn;
	
	var $errno = '';
	var $error = '';

	function __construct($dbNum = 1) {
		wsCOFIDIExec::r();
		/*switch ($dbNum) {
			case 1:
				$this->conn = new mysqli($c['servidor'], $c['usuario'], $c['contrasena'], $c['nombredb'][1]);
				break;
			
			default:
				$this->conn = new mysqli($c['servidor'], $c['usuario'], $c['contrasena'], $c['nombredb'][2]);
				break;
		}*/
	}

	function r(){
		global $GUID, $Country, $NITEmpresa, $UserName, $URL;
		$c = wsConstantes::getConstantes();
		$URL = $c['URL'];
		$GUID = $c['GUID'];
		$Country = $c['Country'];
		$NITEmpresa = $c['NITEmpresa'];
		$UserName = $c['UserName'];

		return $c;
	}

	// INSERTAR (INSERT) CON EXCEPCION
	function iEx($base64, $ID) {
		global $GUID, $Country, $NITEmpresa, $UserName;

		$xml = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"
		    xmlns:ws=\"http://www.fact.com.mx/schema/ws\" >
		    <SOAP-ENV:Header/>
		    <SOAP-ENV:Body>
		        <ws:RequestTransaction>
		            <ws:Requestor>$GUID</ws:Requestor>
		            <ws:Transaction>SYSTEM_REQUEST</ws:Transaction>
		            <ws:Country>$Country</ws:Country>
		            <ws:Entity>$NITEmpresa</ws:Entity>
		            <ws:User>$GUID</ws:User>
		            <ws:UserName>$UserName</ws:UserName>
		            <ws:Data1>POST_DOCUMENT_SAT</ws:Data1>
		            <ws:Data2>$base64</ws:Data2>
		            <ws:Data3>$ID</ws:Data3>
		        </ws:RequestTransaction>
		    </SOAP-ENV:Body>
		</SOAP-ENV:Envelope>";

		return wsCOFIDIExec::ex($xml);
	}

	function sExInternalID($ID) {
		global $GUID, $Country, $NITEmpresa, $UserName;

		$xml = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"
		    xmlns:ws=\"http://www.fact.com.mx/schema/ws\" >
		    <SOAP-ENV:Header/>
		    <SOAP-ENV:Body>
		        <ws:RequestTransaction>
		            <ws:Requestor>$GUID</ws:Requestor>
		            <ws:Transaction>LOOKUP_ISSUED_INTERNAL_ID</ws:Transaction>
		            <ws:Country>$Country</ws:Country>
		            <ws:Entity>$NITEmpresa</ws:Entity>
		            <ws:User>$GUID</ws:User>
		            <ws:UserName>$UserName</ws:UserName>
		            <ws:Data1>$ID</ws:Data1>
		            <ws:Data2></ws:Data2>
		            <ws:Data3></ws:Data3>
		        </ws:RequestTransaction>
		    </SOAP-ENV:Body>
		</SOAP-ENV:Envelope>";

		return wsCOFIDIExec::ex($xml);
	}

	function sExDocumentGUID($DocumentGUID) {
		global $GUID, $Country, $NITEmpresa, $UserName;

		$xml = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"
		    xmlns:ws=\"http://www.fact.com.mx/schema/ws\" >
		    <SOAP-ENV:Header/>
		    <SOAP-ENV:Body>
		        <ws:RequestTransaction>
		            <ws:Requestor>$GUID</ws:Requestor>
		            <ws:Transaction>GET_DOCUMENT</ws:Transaction>
		            <ws:Country>$Country</ws:Country>
		            <ws:Entity>$NITEmpresa</ws:Entity>
		            <ws:User>$GUID</ws:User>
		            <ws:UserName>$UserName</ws:UserName>
		            <ws:Data1>$DocumentGUID</ws:Data1>
		            <ws:Data2></ws:Data2>
		            <ws:Data3>XML</ws:Data3>
		        </ws:RequestTransaction>
		    </SOAP-ENV:Body>
		</SOAP-ENV:Envelope>";
		
		return wsCOFIDIExec::ex($xml);
	}

	function uExAnulacion($base64) {
		global $GUID, $Country, $NITEmpresa, $UserName;

		$xml = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ws=\"http://www.fact.com.mx/schema/ws\" >
		    <SOAP-ENV:Header/>
		    <SOAP-ENV:Body>
		        <ws:RequestTransaction>
		            <ws:Requestor>$GUID</ws:Requestor>
		            <ws:Transaction>SYSTEM_REQUEST</ws:Transaction>
		            <ws:Country>$Country</ws:Country>
		            <ws:Entity>$NITEmpresa</ws:Entity>
		            <ws:User>$GUID</ws:User>
		            <ws:UserName>$UserName</ws:UserName>
		            <ws:Data1>VOID_DOCUMENT</ws:Data1>
		            <ws:Data2>$base64</ws:Data2>
		            <ws:Data3></ws:Data3>
		        </ws:RequestTransaction>
		    </SOAP-ENV:Body>
		</SOAP-ENV:Envelope>";
		
		return wsCOFIDIExec::ex($xml);
	}

	private function ex($xml) {
		global $URL;
		// La URL a la que desea enviar su XML. (The URL that you want to send your XML to.)
		//$URL = 'https://portal.cofidiguatemala.com/webservicefrontfeltest/factwsfront.asmx';

		//Iniciar cURL (Initiate cURL)
		$curl = curl_init($URL);

		// Establecer el Tipo de contenido en text / xml. (Set the Content-Type to text/xml.)
		curl_setopt ($curl, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));

		// Establecer CURLOPT_POST en verdadero para enviar una solicitud POST. (Set CURLOPT_POST to true to send a POST request.)
		curl_setopt($curl, CURLOPT_POST, true);

		// Adjunte la cadena XML al cuerpo de nuestra solicitud. (Attach the XML string to the body of our request.)
		curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);

		// Dígale a cURL que queremos que la respuesta se devuelva como una cadena en lugar de ser volcada a la salida. (Tell cURL that we want the response to be returned as a string instead of being dumped to the output.)
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

		// Ejecute la solicitud POST y envíe nuestro XML. (Execute the POST request and send our XML.)
		$soapResp = curl_exec($curl);

		// Haga una comprobación básica de errores. (Do some basic error checking.)
		if(curl_errno($curl)){
		    throw new Exception(curl_error($curl));
		}

		// Cierre la manija de cURL. (Close the cURL handle.)
		curl_close($curl);

		$sxe = new SimpleXMLElement($soapResp);
		$r = $sxe->xpath('//soap:Body');

		return $r;
	}
}
?>