<?php
//++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++
function GET_TOKEN($RUC,$MODO){
	//return file_get_contents("http://serviciosredcotea.com/lq/lq.php?FUN=GET_TOKEN&RUC=".$RUC);	
	$PAS="";
    $MODO="P";
	if($MODO=="D"){
		//DECORBAZAR-2025
		if($RUC=="20613831216"){
			$PAS="186197c43533ca79404232fa8bfc352cfd3e3c7a9d654e3481d55ee8d1389544";
		}
		//MERCAN GLOBAL-2025
		if($RUC=="20613861531"){
			$PAS="e1a267184793aa665f2f411904eb945a5b38bea9dedbce8cd6bf30ae8b7dcd9b";
		}
	    //JT
		if($RUC=="15604466927"){
			$PAS="7ec16c954cc6d9564db7f44cd17d657091e405b5d41787e37d15b0dd1e62d2d6";
		}
	    //PERU
		if($RUC=="20610441174"){
			$PAS="ee945511383d824a69e7a1713c2daf73f218f820f70517baeaa91b200a70cf3c";
		}
	    //DONATO EIRL
		if($RUC=="20610220593"){
			$PAS="85a4535f9f69dbf1c11484fee98e8889c49c6a0d17c2422fa16440e9b395ecd2";
		}
		//BAOMA EIRL
		if($RUC=="20603651155"){
			$PAS="c3e854c93a20a91939c6906d9ba6872bd024f2bc30b13df66963da8680d9b498";
		}
		//ZI HAO IMPORT EXPORT SAC
		if($RUC=="20601600286"){
			$PAS="57848794c0eab7dc191e51acf12c447322b54bc67f24ead488722ab9e9462f94";
		}
		//LOU & LOU IMPORT EXPORT SAC
		if($RUC=="20607998079"){
			$PAS="c0ce8f3aeac6314ff166ded6c96978f2939de57cb9ecfb9ac21c6076f3b7d322";
		}
		//GRUPO L&Q
		if($RUC=="20608115065"){
			$PAS="1e5b86d36630171288ad08d93920530e6483cc9225571fd71a0042ed1b3242c3";
		}	
		//GRUPO LSQ-XX
		if($RUC=="20609849666"){
			//$PAS="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
		}	
	}else{
		//SX
	    if($RUC=="15615008629"){
			$PAS="9ff86ac5a40bd33ec13c686c99079a0dd91b8d23ba1e392b3d63efeea7f86bd8";
		}
	    //ZHENG WEIHUI
	    if($RUC=="15614601059"){
			$PAS="63ce2de6791963d0e3a9375327aabeae7a6d9eb1ae14f2bf7b76e9df9718f0d1";
		}
	    
	    //CHEN HANXIAO
	    if($RUC=="15614760742"){
			$PAS="8d723f7128d4597493490e47b815b1d4de9ed57af0ba5e9d232049b93a2819d6";
		}
	    
	    //LOU HAOYU
	     if($RUC=="15610094578"){
			$PAS="ace037832aeae5c1760b1a2bfe92a7442a0b7b03ac57433ca594af87d5a8465c";
		}
	    
	    
	    //CORPORACION COMERCIO GLOBAL S.A.C.
	    if($RUC=="20614090007"){
			$PAS="175acd41160d23dda51ffd4403fda9cc91ac35c09ae8f32e10cb51228b138d31";
		}
		//CORPORACION INTERCOMEX E.I.R.L.
		if($RUC=="20614132613"){
			$PAS="cb8c0ff4ceb8e1564411399a28f8c0c04914fe7afe30b5430b43872c9a36d29a";
		}
	    //QING HUA
		if($RUC=="20613183117"){
			$PAS="ae4f1eb75de0437239e54f42e0b4cc203dda90ff50bd678f06bb654669a6ef6e";
		}
		
		//DECORBAZAR-2025
		if($RUC=="20613831216"){
			$PAS="0d25e06241255867e822e131c45f8c0970c6c4bb3513a4d2e4fa8a519e6f2e92";
		}
		//MERCAN GLOBAL-2025
		if($RUC=="20613861531"){
			$PAS="cac6f19f7b90b840a5ac77b09accba9d831ed01002a3eec9c73ee3f911b782fd";
		}
		
	    //JT
		if($RUC=="15604466927"){
			$PAS="16048947c0d962518b3a24b134522653ffd28ecab8ebae6154e937819b33b1d1";
		}
	    //PERU
		if($RUC=="20610441174"){
			$PAS="5ab23999f8b9555954a66d283101b5e46f7ccad90d7dbc205f18b20c47f672ac";
		}
	    //DONATO EIRL
		if($RUC=="20610220593"){
			$PAS="1863b8b76bde2940acef05012092b17cdac4e81fd81fbf04d2667d4f166ac0b2";
		}
		//BAOMA EIRL
		if($RUC=="20603651155"){
			//$PAS="f313ab62ea67f767f1de9fe247d1df6ac43b5df6151f4c34dc58a1d02e972c7d";
			$PAS="70ba8fdb506356c5205132431bf9e19d737ddac6e2e689ac592509347ae84150";
		}
		//ZI HAO IMPORT EXPORT SAC
		if($RUC=="20601600286"){
			$PAS="17908d44fd71c86942557723f5389bc25f49a4f878d9c4b5a12903e8bf2b2246";
		}
		//LOU & LOU IMPORT EXPORT SAC
		if($RUC=="20607998079"){
			//$PAS="18017760bc0c632b80d1eafe28afdfc87efb4d30b960af623782f45103aa3bf9";
			$PAS="0e8b553bc597c3075e82577b2d6869dc52640b639996b4927491253e0e19aa79";
		}
		//GRUPO L&Q
		if($RUC=="20608115065"){
			//$PAS="405d3571810c0b0a3f1cc0caba9578355178655caafdea17106ad1de0fd8099c";
			$PAS="3622512a9ec657054b9ba5b1715dd0df9ca456775470623b306949f422752dfd";
			
		}
		//GRUPO LSQ-XX
		if($RUC=="20609849666"){
			//$PAS="a7fc1d9ff80fda8522a52d68e6e8551765e4a288545d8c2a67bcc67b24f5e813";
			$PAS="64f7e361cacfd9af3130dea684b2315486e6460bd44c7568796d38781a407d3e";
		}	
	}
	//+++++++
	if(strlen($PAS)==0){
		return "|MS|Numero de RUC no habilitado|";
	}
	//+++++++
	//https://ose-qa-rest.efact.pe/api-efact-ose/oauth/token
	//https://ose.efact.pe/api-efact-ose/oauth/token
	$curl = curl_init();
	curl_setopt_array($curl, array(
	  CURLOPT_URL => "https://ose.efact.pe/api-efact-ose/oauth/token",
	  CURLOPT_RETURNTRANSFER => true,
	  CURLOPT_ENCODING => "",
	  CURLOPT_MAXREDIRS => 10,
	  CURLOPT_TIMEOUT => 30,
	  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	  CURLOPT_CUSTOMREQUEST => "POST",
	  CURLOPT_POSTFIELDS => "username=".$RUC."&password=".$PAS."&grant_type=password",
	  CURLOPT_HTTPHEADER => array(
		"Authorization: Basic Y2xpZW50OnNlY3JldA==",
		"Content-Type: application/x-www-form-urlencoded",
		"Postman-Token: ed109e19-f70a-40e0-83ad-83efee92acda",
		"cache-control: no-cache"
	  ),
	));	
	$response = curl_exec($curl);
	$err = curl_error($curl);	
	curl_close($curl);
	//
	if($err){
		echo "|MS|" . $err;
	}else{
		$obj = json_decode($response);
		$TOK_NUM=trim($obj->{'access_token'});
		if(strlen($TOK_NUM)==0){
			return "|MS|Error : TOKEN NO GENERADO|";
		}
		$TOK_TIP=trim($obj->{'token_type'});
		if(strlen($TOK_TIP)==0){
			return "|MS|Error : TIPO DE TOKEN NO DEFINIDO|";
		}
		$TIE_DIS=trim($obj->{'expires_in'});
		if(strlen($TIE_DIS)==0){
			return "|MS|Error : TIEMPO NO DEFINIDO|";
		}
		$SCOPE=trim($obj->{'scope'});
		if(strlen($SCOPE)==0){
			return "|MS|Error : SCOPE NO DEFINIDO|";
		}
		$JTI=trim($obj->{'jti'});
		if(strlen($JTI)==0){
			return "|MS|Error : JTI NO DEFINIDO|";
		}	
		return "|OK|".$TOK_NUM."|".$TOK_TIP."|".$TIE_DIS;
	}
	
}
//++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++
function SEND_JSON($TOKEN,$FILE_NAME){
	//return file_get_contents("http://serviciosredcotea.com/lq/lq.php?FUN=SEND_JSON&TOK=".$TOKEN."&FIL=".$FILE_NAME);
	
	$postData = array('file'=>new CURLFile ( $FILE_NAME, 'application/json', $FILE_NAME ));
	//https://ose-qa-rest.efact.pe/api-efact-ose/v1/document
	//https://ose.efact.pe/api-efact-ose/v1/document
	$curl = curl_init();
	curl_setopt_array($curl, array(
	  CURLOPT_URL => "https://ose.efact.pe/api-efact-ose/v1/document",
	  CURLOPT_RETURNTRANSFER => true,
	  CURLOPT_ENCODING => "",
	  CURLOPT_MAXREDIRS => 10,
	  CURLOPT_TIMEOUT => 30,
	  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	  CURLOPT_CUSTOMREQUEST => "POST",
	  CURLOPT_POSTFIELDS => $postData,
	  CURLOPT_HTTPHEADER => array(
		"Authorization: Bearer ".$TOKEN,
		"Postman-Token: a5493058-d5d0-4109-a9b3-ad20dbb1a0d2",
		"cache-control: no-cache"
		
	  ),
	));
	//curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
	//CURLOPT_POSTFIELDS => $postData,
	$response = curl_exec($curl);
	$err = curl_error($curl);
	curl_close($curl);
	if ($err) {
		return "|MS|".$err;
	} else {
		$obj = json_decode($response);
		$CODE=trim($obj->{'code'});
		if(strlen($CODE)==0){
			//echo "TOKEN:".$TOKEN."<br>";
			return "|MS|Error : CODE NO GENERADO $response"."@".realpath($FILE_NAME)." |$response|";
		}
		$TICK=trim($obj->{'description'});
		if(strlen($TICK)==0){
			return "|MS|Error : TICKET NO GENERADO|$response|";
		}
		return "|OK|$TICK|";
	}
	
}
//++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++
function VALIDAR_PDF_NDC($VEN_ID){
	$CX=gn("count(*)","fe_ventaz","id_ven=$VEN_ID");
	if($CX==0){
		return "|MS|ID no registrado|";
	}
	$ANU=gs("fv_anu","fe_ventaz","id_ven=$VEN_ID");
	if($ANU=="S"){
		return "|MS|El comprobante ha sido ANULADO por lo tanto no se puede proseguir|";
	}
	$COM=gs("fv_id_com","fe_ventaz","id_ven=$VEN_ID");
	//5=NC. ELEC. (comprobantes)
	if($COM==5 || $COM==6){
		$DOC_OK=gs("fv_gen","fe_ventaz","id_ven=$VEN_ID");
		if($DOC_OK=="N"){
			return "|MS|El documento electronico AUN NO ha sido GENERADO|";	
		}
		$DOC_TI=trim(gs("fv_gen","fe_ventaz","id_ven=".$VEN_ID));
		if(strlen($DOC_TI)==0){
			return "|MS|Error IDENTIFICADOR no HA SIDO GENERADO|";	
		}
		return "|OK|OK|";
	}else{
		return "|MS|Proceso solo valido para comprobantes electronicos|";
	}
}
//++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++
function VALIDAR_PDF($VEN_ID){
	$CX=gn("count(*)","vventas_fe","ID=$VEN_ID");
	if($CX==0){
		return "|MS|ID no registrado|";
	}
	$ANU=gs("ANU","vventas","ID=$VEN_ID");
	if($ANU=="S"){
		return "|MS|El comprobante ha sido ANULADO por lo tanto no se puede proseguir|";
	}
	$COM=gs("COM","vventas_fe","ID=$VEN_ID");
	if($COM=="BOL. ELEC." || $COM=="FACT. ELEC."){
		$DOC_OK=gs("ven_fe_ge","ventas","id_venta=".$VEN_ID);
		if($DOC_OK=="N"){
			return "|MS|El documento electronico AUN NO ha sido GENERADO|";	
		}
		$DOC_TI=trim(gs("ven_hash","ventas","id_venta=".$VEN_ID));
		if(strlen($DOC_TI)==0){
			return "|MS|Error IDENTIFICADOR no HA SIDO GENERADO|";	
		}
		return "|OK|OK|";
	}else{
		return "|MS|Proceso solo valido para comprobantes electronicos|";
	}
}
//++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++
function GEN_PDF($VEN_ID,$TOKEN){	
	//
	$r=gr("vventas_fe","ID=$VEN_ID");
	$TICKET=$r['FE_HASH'];
	$FILE_PDF=$r['EMPC']." ".substr($r['COM'],0,1)."_".$r['SER']."-".$r['NUM'].".pdf";
	//
	header("Content-type:application/pdf");
	header("Content-Disposition:attachment;filename=".$FILE_PDF);
	//echo file_get_contents("http://serviciosredcotea.com/lq/lq.php?FUN=GEN_PDF&TIK=".$TICKET."&TOK=".$TOKEN);
	//
	//https://ose-qa-rest.efact.pe/api-efact-ose/v1/pdf/
	//https://ose.efact.pe/api-efact-ose/v1/pdf/
	$curl = curl_init();
	curl_setopt_array($curl, array(
	  CURLOPT_URL => "https://ose.efact.pe/api-efact-ose/v1/pdf/".$TICKET,
	  CURLOPT_RETURNTRANSFER => true,
	  CURLOPT_ENCODING => "",
	  CURLOPT_MAXREDIRS => 10,
	  CURLOPT_TIMEOUT => 30,
	  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	  CURLOPT_CUSTOMREQUEST => "GET",
	  CURLOPT_POSTFIELDS => "",
	  CURLOPT_HTTPHEADER => array(
		"Authorization: Bearer ".$TOKEN,
		"Postman-Token: b02d345f-39b2-4781-8379-462287d170c6",
		"cache-control: no-cache"
	  ),
	));	
	$response = curl_exec($curl);
	$err = curl_error($curl);	
	curl_close($curl);	
	if ($err) {
	  echo "cURL Error #:" . $err;
	} else {
	  echo $response;
	}
	//
}
//++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++
function GEN_PDF_NDC($VEN_ID,$TOKEN){	
	//
	$r=gr("vis_fe_ventaz","ID=$VEN_ID");
	$TICKET=$r['FE_HASH'];
	$FILE_PDF=$r['EMP_NC']." NDC_".$r['SER']."-".$r['NUM'].".pdf";
	//
	header("Content-type:application/pdf");
	header("Content-Disposition:attachment;filename=".$FILE_PDF);	
	//echo file_get_contents("http://serviciosredcotea.com/lq/lq.php?FUN=GEN_PDF&TIK=".$TICKET."&TOK=".$TOKEN);
	//
	//https://ose-qa-rest.efact.pe/api-efact-ose/v1/pdf/
	//https://ose.efact.pe/api-efact-ose/v1/pdf/
	$curl = curl_init();
	curl_setopt_array($curl, array(
	  CURLOPT_URL => "https://ose.efact.pe/api-efact-ose/v1/pdf/".$TICKET,
	  CURLOPT_RETURNTRANSFER => true,
	  CURLOPT_ENCODING => "",
	  CURLOPT_MAXREDIRS => 10,
	  CURLOPT_TIMEOUT => 30,
	  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	  CURLOPT_CUSTOMREQUEST => "GET",
	  CURLOPT_POSTFIELDS => "",
	  CURLOPT_HTTPHEADER => array(
		"Authorization: Bearer ".$TOKEN,
		"Postman-Token: b02d345f-39b2-4781-8379-462287d170c6",
		"cache-control: no-cache"
	  ),
	));	
	$response = curl_exec($curl);
	$err = curl_error($curl);	
	curl_close($curl);	
	if ($err) {
	  echo "cURL Error #:" . $err;
	} else {
	  echo $response;
	}
	//
}
//++
function VALIDAR_PDF_NDD($VEN_ID){
	$CX=gn("count(*)","fe_ventaz","id_ven=$VEN_ID");
	if($CX==0){
		return "|MS|ID no registrado|";
	}
	$ANU=gs("fv_anu","fe_ventaz","id_ven=$VEN_ID");
	if($ANU=="S"){
		return "|MS|El comprobante ha sido ANULADO por lo tanto no se puede proseguir|";
	}
	$COM=gs("fv_id_com","fe_ventaz","id_ven=$VEN_ID");
	//5=NC. ELEC. (comprobantes)
	if($COM==5 || $COM==6){
		$DOC_OK=gs("fv_gen","fe_ventaz","id_ven=$VEN_ID");
		if($DOC_OK=="N"){
			return "|MS|El documento electronico AUN NO ha sido GENERADO|";	
		}
		$DOC_TI=trim(gs("fv_gen","fe_ventaz","id_ven=".$VEN_ID));
		if(strlen($DOC_TI)==0){
			return "|MS|Error IDENTIFICADOR no HA SIDO GENERADO|";	
		}
		return "|OK|OK|";
	}else{
		return "|MS|Proceso solo valido para comprobantes electronicos|";
	}
}
//
function GEN_PDF_NDD($VEN_ID,$TOKEN){	
	//
	$r=gr("vis_fe_ventaz","ID=$VEN_ID");
	$TICKET=$r['FE_HASH'];
	$FILE_PDF=$r['EMP_NC']." NDD_".$r['SER']."-".$r['NUM'].".pdf";
	//
	header("Content-type:application/pdf");
	header("Content-Disposition:attachment;filename=".$FILE_PDF);	
	//echo file_get_contents("http://serviciosredcotea.com/lq/lq.php?FUN=GEN_PDF&TIK=".$TICKET."&TOK=".$TOKEN);
	//
	//https://ose-qa-rest.efact.pe/api-efact-ose/v1/pdf/
	//https://ose.efact.pe/api-efact-ose/v1/pdf/
	$curl = curl_init();
	curl_setopt_array($curl, array(
	  CURLOPT_URL => "https://ose.efact.pe/api-efact-ose/v1/pdf/".$TICKET,
	  CURLOPT_RETURNTRANSFER => true,
	  CURLOPT_ENCODING => "",
	  CURLOPT_MAXREDIRS => 10,
	  CURLOPT_TIMEOUT => 30,
	  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	  CURLOPT_CUSTOMREQUEST => "GET",
	  CURLOPT_POSTFIELDS => "",
	  CURLOPT_HTTPHEADER => array(
		"Authorization: Bearer ".$TOKEN,
		"Postman-Token: b02d345f-39b2-4781-8379-462287d170c6",
		"cache-control: no-cache"
	  ),
	));	
	$response = curl_exec($curl);
	$err = curl_error($curl);	
	curl_close($curl);	
	if ($err) {
	  echo "cURL Error #:" . $err;
	} else {
	  echo $response;
	}
	//
}

function GEN_GIA_PDF($GIA_ID,$TOKEN){	
	//
	$r=gr("vguias_fe","ID=$GIA_ID");
	$TICKET=$r['CE_HASH'];
	$FILE_PDF=$r['EMP_NC']."_GUIA_".$r['SER']."-".$r['NUM'].".pdf";
	//
	header("Content-type:application/pdf");
	header("Content-Disposition:attachment;filename=".$FILE_PDF);	
	//echo file_get_contents("http://serviciosredcotea.com/lq/lq.php?FUN=GEN_PDF&TIK=".$TICKET."&TOK=".$TOKEN);
	//
	$MODO="D";
	$URL_PDF="https://ose.efact.pe/api-efact-ose/v1/pdf/";
	if($MODO=="D"){
		//$URL_PDF="https://ose-gw1.efact.pe/api-efact-ose/v1/pdf/";
	}
	$curl = curl_init();
	curl_setopt_array($curl, array(
	  CURLOPT_URL => $URL_PDF.$TICKET,
	  CURLOPT_RETURNTRANSFER => true,
	  CURLOPT_ENCODING => "",
	  CURLOPT_MAXREDIRS => 10,
	  CURLOPT_TIMEOUT => 30,
	  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	  CURLOPT_CUSTOMREQUEST => "GET",
	  CURLOPT_POSTFIELDS => "",
	  CURLOPT_HTTPHEADER => array(
		"Authorization: Bearer ".$TOKEN,
		"Postman-Token: b02d345f-39b2-4781-8379-462287d170c6",
		"cache-control: no-cache"
	  ),
	));	
	//**
	curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, false);
	//**
	$response = curl_exec($curl);
	$err = curl_error($curl);	
	curl_close($curl);	
	if ($err) {
	  echo "cURL Error #:" . $err;
	} else {
	  echo $response;
	}
	//
}


function GIA_VALIDAR_PDF($GIA_ID){
	$CX=gn("count(*)","guias","id_guia=$GIA_ID");
	if($CX==0){
		return "|MS|ID no registrado|";
	}
	$ANU=gs("anu","guias","id_guia=$GIA_ID");
	if($ANU=="S"){
		return "|MS|El comprobante ha sido ANULADO por lo tanto no se puede proseguir|";
	}
	$COM=gn("xx_id_tip_gia","guias","id_guia=$GIA_ID");
	if($COM==8){
		$GEN=gs("xx_ce_gen","guias","id_guia=$GIA_ID");
		if($GEN=="N"){
			return "|MS|El documento electronico AUN NO ha sido GENERADO|";	
		}
		$DOC_TI=gs("xx_ce_hash","guias","id_guia=$GIA_ID");
		if(strlen($DOC_TI)==0){
			return "|MS|Error IDENTIFICADOR no HA SIDO GENERADO|";	
		}
		return "|OK|OK|";
	}else{
		return "|MS|Proceso solo valido para comprobantes electronicos|";
	}
}
//++++++++++++++++++++++++++++++++++++++++++++
function XLM_GET_LINK($XML){
	$INI="<cbc:DocumentDescription>";
	$FIN="</cbc:DocumentDescription>";
	/*
	$XML='<?xml version="1.0" encoding="UTF-8"?><ns4:ApplicationResponse xmlns:ns4="urn:oasis:names:specification:ubl:schema:xsd:ApplicationResponse-2" xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ns2="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:ns3="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"><ns2:UBLExtensions><ns2:UBLExtension><ns2:ExtensionContent><ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#"><ds:SignedInfo><ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/><ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/><ds:Reference URI=""><ds:Transforms><ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/></ds:Transforms><ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/><ds:DigestValue>un3S9BwbzrLFSIt8V5pCtJM/OBY=</ds:DigestValue></ds:Reference></ds:SignedInfo><ds:SignatureValue>VXs4yDtchv1nRFAngfefWq5MUE21n864JHbxjWpjZWuYEaVJ+IJ1Y0emh1xkvDelJIrzwavOCBpj41pgvUAcO7y/asD2aiyT1py9UFoHyu3klB3G/dH/ajo35yMm0bfSZiWnGHfIijXV0Wxc9IU9qAHIGNgibQ1jCr1SDjRa9SA=</ds:SignatureValue><ds:KeyInfo><ds:X509Data><ds:X509Certificate>MIICqTCCAhICCQDbc1B7f535azANBgkqhkiG9w0BAQsFADCBmDELMAkGA1UEBhMCUEUxDTALBgNVBAgMBExpbWExDTALBgNVBAcMBExpbWExFDASBgNVBAoMCzIwNTUxMDkzMDM1MRIwEAYDVQQLDAlFZmFjdCBTQUMxIDAeBgNVBAMMF0NlcnRpZmljYWRvLUNEUi1QcnVlYmFzMR8wHQYJKoZIhvcNAQkBFhBqcXVpc3BlQGVmYWN0LnBlMB4XDTE5MDgwNjIyMjQxMVoXDTIwMDgwNTIyMjQxMVowgZgxCzAJBgNVBAYTAlBFMQ0wCwYDVQQIDARMaW1hMQ0wCwYDVQQHDARMaW1hMRQwEgYDVQQKDAsyMDU1MTA5MzAzNTESMBAGA1UECwwJRWZhY3QgU0FDMSAwHgYDVQQDDBdDZXJ0aWZpY2Fkby1DRFItUHJ1ZWJhczEfMB0GCSqGSIb3DQEJARYQanF1aXNwZUBlZmFjdC5wZTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEApQ/gUGG4E/O2VuBiSs/tNYigIqYGbLMyHidEwFwahCaYtrA9BClFlOhIZbuuQ7DVWDrSSPoC91LPIvy7+lcYzNz9HT8zTHqaRlRkLI9IqtdDSgPRlfM+19XbOaIO7jZm1n4No+boTSauIm3GHZbprXb44y8kqIQh+2WvAMMm3PUCAwEAATANBgkqhkiG9w0BAQsFAAOBgQAjq9WDwPrxJLnjxnPFZZ+ejK8Ylp5qVM8NnHRNQFTifgXUJ+YxXs/old6OMzdwNMPzDBaWo5r+8H2ox9DxKqolPkhx639FM/t8Ctv009jv54bj+LOt/HogEZ6+u1h/d5ZpKlkb9h+2fFufiO86aERgZ1k6LHyehqiSsMhlQG3IFQ==</ds:X509Certificate><ds:X509IssuerSerial><ds:X509IssuerName>1.2.840.113549.1.9.1=#16106a7175697370654065666163742e7065,CN=Certificado-CDR-Pruebas,OU=Efact SAC,O=20551093035,L=Lima,ST=Lima,C=PE</ds:X509IssuerName><ds:X509SerialNumber>15813071207980202347</ds:X509SerialNumber></ds:X509IssuerSerial></ds:X509Data></ds:KeyInfo></ds:Signature></ns2:ExtensionContent></ns2:UBLExtension></ns2:UBLExtensions><UBLVersionID>2.1</UBLVersionID><CustomizationID>1.0</CustomizationID><ID>70cc4a1f-9f78-4ef2-8875-2ad91e29e1f3</ID><IssueDate>2023-03-09</IssueDate><IssueTime>08:09:41.00384</IssueTime><ResponseDate>2023-03-09</ResponseDate><ResponseTime>08:09:41.00395</ResponseTime><Note>4186 -  Warning (4186):cbc:Note - El campo observaciones supera la cantidad maxima especificada (250 carácteres). Error : 4186 (nodo: "/" valor: "")</Note><Note>4369 -  Warning (4369):El dato ingresado como numero de autorizacion del Remitente no cumple con el formato establecido Error : 4369 (nodo: "cac:PartyLegalEntity/cbc:CompanyID" valor: "")</Note><Note>4391 -  Warning (4391):El Numero de Registro MTC del transportista no existe o no contiene informacion Error Expr Regular: 4391 (nodo: "cac:PartyLegalEntity/cbc:CompanyID" valor: "")</Note><ns3:SenderParty><ns3:PartyIdentification><ID>20551093035</ID></ns3:PartyIdentification></ns3:SenderParty><ns3:ReceiverParty><ns3:PartyIdentification><ID>20551093035</ID></ns3:PartyIdentification></ns3:ReceiverParty><ns3:DocumentResponse><ns3:Response><ReferenceID>T001-00001</ReferenceID><ResponseCode listAgencyName="PE:SUNAT">0</ResponseCode><Description>La Guia de remision remitente T001-00001 ha sido aceptada.</Description></ns3:Response><ns3:DocumentReference><ID>T001-00001</ID><IssueDate>2023-03-09</IssueDate><IssueTime>12:00:00</IssueTime><DocumentTypeCode>09</DocumentTypeCode><DocumentDescription>https://e-factura.sunat.gob.pe/v1/contribuyente/gre/comprobantes/descargaqr?hashqr=PB9GHszJi9h3WYsVW00fVZdT5v1GYqWLfoQmeOsBx0ZPg+SVfiYcBnoeuNBAdX3nMaNfBntt+X4zjivr026pMLSFNibw1AKiTUMEcaFwb5cTTE5i0FEg/H/XmkvUyCbG</DocumentDescription><ns3:Attachment><ns3:ExternalReference><DocumentHash>uUhApJHBnodl+2jSAFWdXT4Mye4=</DocumentHash></ns3:ExternalReference></ns3:Attachment></ns3:DocumentReference><ns3:IssuerParty><ns3:PartyIdentification><ID>15604466927</ID></ns3:PartyIdentification></ns3:IssuerParty><ns3:RecipientParty><ns3:PartyIdentification><ID>16422264</ID></ns3:PartyIdentification></ns3:RecipientParty></ns3:DocumentResponse></ns4:ApplicationResponse>';
	*/
	$DAT=explode($INI,$XML);
	$XML=$DAT[1];
	$DAT=explode($FIN,$XML);
	$URL=trim($DAT[0]);
	$POS=strpos($URL,"https:");
	if($POS===false){
		return "";
	}else{
		return $URL;
	}
}
//++++++++++++++++++++++++++++++++++++++++++++
function GEN_CDR($GIA_ID,$TOKEN,&$LINK){	
	$LINK="";
	//
	if($GIA_ID==80772){
		return true;
	}
	//
	$r=gr("vguias","ID=$GIA_ID");
	$TICKET=gs("xx_ce_hash","guias","id_guia=".$GIA_ID);
	/*
	$DAT= file_get_contents("http://serviciosredcotea.com/lq/lq.php?FUN=GEN_CDR&TIK=".$TICKET."&TOK=".$TOKEN);
	$BAT=explode("|",$DAT);
	if($BAT[1]=="OK"){
		$LINK=$BAT[2];
		return true;
	}else{
		return false;
	}
	*/
	
	//$EMPRESA=str_replace(" ","_",$r['EMPC']);
	$FILE_CDR=$EMPRESA."_GRE_".$r['SER']."-".$r['NUM'].".cdr";
	//
	//header("Content-type:application/pdf");
	//header("Content-Disposition:attachment;filename=".$FILE_PDF);	
	$MODO="D";
	$URL_PDF="https://ose.efact.pe/api-efact-ose/v1/cdr/";
	if($MODO=="D"){
		//$URL_PDF="https://ose-gw1.efact.pe/api-efact-ose/v1/cdr/";
	}
	$curl = curl_init();
	curl_setopt_array($curl, array(
	  CURLOPT_URL => $URL_PDF.$TICKET,
	  CURLOPT_RETURNTRANSFER => true,
	  CURLOPT_ENCODING => "",
	  CURLOPT_MAXREDIRS => 10,
	  CURLOPT_TIMEOUT => 30,
	  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	  CURLOPT_CUSTOMREQUEST => "GET",
	  CURLOPT_POSTFIELDS => "",
	  CURLOPT_HTTPHEADER => array(
		"Authorization: Bearer ".$TOKEN,
		"Postman-Token: b02d345f-39b2-4781-8379-462287d170c6",
		"cache-control: no-cache"
	  ),
	));	
	//**
	//curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, false);
	//**
	$response = curl_exec($curl);
	$err = curl_error($curl);	
	curl_close($curl);	
	if ($err) {
	  $BODY="ERROR AL GENERAR #:" . $err;
	} else {
	  $BODY= $response;
	}
	$FULL_FILE_NAME="cdr/".$FILE_CDR;
	file_put_contents($FULL_FILE_NAME, $BODY);
	$TODO_OK=strpos($BODY,"ha sido aceptad");
	if($TODO_OK===false){
		return false;
	}else{
		$LINK=XLM_GET_LINK($BODY);
		if(strlen($LINK)>0){
			return true;
		}else{
			return false;
		}
	}
	
}

function GET_TICKET($FUL_DOC,$TOKEN){
	$LINK="";
	$FUL_URL="https://ose.efact.pe/api-efact-ose/v1/ticket/".$FUL_DOC;
	//echo $FUL_URL."<br>";
	$curl = curl_init();
	curl_setopt_array($curl, array(
	  CURLOPT_URL => $FUL_URL,
	  CURLOPT_RETURNTRANSFER => true,
	  CURLOPT_ENCODING => "",
	  CURLOPT_MAXREDIRS => 10,
	  CURLOPT_TIMEOUT => 30,
	  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	  CURLOPT_CUSTOMREQUEST => "GET",
	  CURLOPT_POSTFIELDS => "",
	  CURLOPT_HTTPHEADER => array(
		"Authorization: Bearer ".$TOKEN,
		"Postman-Token: b02d345f-39b2-4781-8379-462287d170c6",
		"cache-control: no-cache"
	  ),
	));	
	//**
	//curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, false);
	//**
	
	/*
	$response = curl_exec($curl);
	$err = curl_error($curl);	
	curl_close($curl);	
	if ($err) {
	  $BODY="ERROR AL GENERAR #:" . $err;
	} else {
	  $BODY= $response;
	}
	*/
	//return "BODY=".$BODY;
	
	//$FULL_FILE_NAME="cdr/".$FILE_CDR;
	//file_put_contents($FULL_FILE_NAME, $BODY);
	/*
	$TODO_OK=strpos($BODY,"ha sido aceptad");
	if($TODO_OK===false){
		return false;
	}else{
		$LINK=XLM_GET_LINK($BODY);
		if(strlen($LINK)>0){
			return "|OK|".$LINK."|";
		}else{
			return "|MS|No se pudo obtener la validacion|";
		}
	}
	*/
	$response = curl_exec($curl);
	$err = curl_error($curl);
	curl_close($curl);
	if ($err) {
		return "|MS|".$err;
	} else {
	    //return "|OK|".$response."|";
		//$obj = json_decode($response);
		return "|OK|".$response."|";
		/*
		$CODE=trim($obj->{'tickets'});
		
		if(strlen($CODE)==0){
			//echo "TOKEN:".$TOKEN."<br>";
			//return "|MS|Error : CODE NO GENERADO $response"."@".realpath($FILE_NAME)."|$response|";
		}
		*/
		//return "|OK|".$CODE."|";
		
		
	}
	
}

?>