<?php
//++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++
function GET_TOKEN($RUC,$MODO){
	$PAS="";
    $MODO="P";
	if($MODO=="D"){
		//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{
		//BAOMA EIRL
		if($RUC=="20603651155"){
			$PAS="f313ab62ea67f767f1de9fe247d1df6ac43b5df6151f4c34dc58a1d02e972c7d";
		}
		//ZI HAO IMPORT EXPORT SAC
		if($RUC=="20601600286"){
			$PAS="17908d44fd71c86942557723f5389bc25f49a4f878d9c4b5a12903e8bf2b2246";
		}
		//LOU & LOU IMPORT EXPORT SAC
		if($RUC=="20607998079"){
			$PAS="18017760bc0c632b80d1eafe28afdfc87efb4d30b960af623782f45103aa3bf9";
		}
		//GRUPO L&Q
		if($RUC=="20608115065"){
			$PAS="405d3571810c0b0a3f1cc0caba9578355178655caafdea17106ad1de0fd8099c";
		}
		//GRUPO LSQ-XX
		if($RUC=="20609849666"){
			$PAS="a7fc1d9ff80fda8522a52d68e6e8551765e4a288545d8c2a67bcc67b24f5e813";
		}	
	}
	//+++++++
	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){
	$postData = array('file'=>new CURLFile ( $FILE_NAME, 'application/json', $FILE_NAME ));
	/*
	$postData = array(
		'file' => "@".$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",
		"content-type: multipart/form-data; "
	  ),
	));
	//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_GEN","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);	
	//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);	
	//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);	
	//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;
	}
}
?>