<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
	//IMPORTACIONES LQ	
	include("cls_conexion.php");$CNN=new ClsMySQL();include("conexion23.php");
	include("fe_kernel.php");

	//++++++++++++++++++++++++++
	function yy($s){
		if(strlen(trim($s))==0){
			return "<";
		}else{
			$s=strtoupper($s);
			$s=str_replace("Á","A",$s);
			$s=str_replace("É","E",$s);
			$s=str_replace("Í","I",$s);
			$s=str_replace("Ó","O",$s);
			$s=str_replace("Ú","U",$s);
			//$s=str_replace("Ñ","<N>",$s);
			return $s;
		}
	}
	//
	$Z="|";
	$CAB="";
	$DET="";
	//
	$CM=V($_GET['C']);
	$P1=V($_GET['P1']);
	$P2=V($_GET['P2']);
	$P3=V($_GET['P3']);
	$P4=V($_GET['P4']);
	$P5=V($_GET['P5']);
	$P6=V($_GET['P6']);
	$P7=V($_GET['P7']);
	//
	$EMP=$P1;
	$SER=vbval($P2);
	$NUM=vbval($P3);
	//
	//
	if($CM=="GRE_EMP_SER_NUM"){
		$EMP=V($_GET['P1']);
		$SER=vbval(V($_GET['P2']));
		$NUM=vbval(V($_GET['P3']));
		$CX=gn("count(*)","vguias","EMPC='$EMP' AND ISER=$SER AND INUM=$NUM AND TIP_GIA='GUIA DE REMISION ELECTRONICA'");
		if($CX==0){
			//+++++++++++++++++++
			echo "|MS|NO_REGISTRADO|";
			//+++++++++++++++++++
			return;
		}
		$SER=gn("ID","vguias","EMPC='$EMP' AND ISER=$SER AND INUM=$NUM AND TIP_GIA='GUIA DE REMISION ELECTRONICA'");
		$CM="GRE_ID_EX";		
	}
	//
	if($CM=="GRE_ID_EX"){				
		$ID=$SER;		
		if($ID==0){
			//+++++++++++++++++++
			echo "|MS|NO_REGISTRADO|";
			//+++++++++++++++++++
			return;
		}
		//
		$CX=gn("count(*)","vguias","ID=$ID AND TIP_GIA='GUIA DE REMISION ELECTRONICA'");
		if($CX==0){
			//+++++++++++++++++++
			echo "|MS|NO_REGISTRADO|";
			//+++++++++++++++++++
			return;
		}
		//
		$GE_GEN=gs("CE_GEN","vguias","ID=$ID");			
		if($GE_GEN!="S"){
			//
			echo "|MS|NO_GENERADO|";
			//
			return;
		}
		//INI_VERIFICAR_VALIDACION
		$XX_LINK=trim(gs("xx_link","guias","id_guia=$ID"));
		if(strlen($XX_LINK)==0){
			$MODO="P";
			$EMP_RUC=gs("EMP_RUC","vguias_fe","ID=$ID");
			//
			$LINK_GENERADO="";
			$XX_TOK=GET_TOKEN($EMP_RUC,$MODO);
			$XX_DAT=explode("|",$XX_TOK);
			$RET=GEN_CDR($ID,$XX_DAT[2],$LINK_GENERADO);
			if($RET==false){		
				echo "|MS|AUN_NO_ACEPTADO|";return;
			}else{
				$qqq=ejt("UPDATE guias SET xx_link='$LINK_GENERADO' WHERE id_guia=$ID");
			}	
		}
		//FIN_VERIFICAR_VALIDACION
		$GE_EMP=yy(gs("EMPC","vguias","ID=$ID"));		
		$GE_EMP_RUC=yy(gs("EMP_RUC","vguias","ID=$ID"));	
		$GE_EMP_NOM=gs("emp_noml","empresas","emp_fe_ruc='$GE_EMP_RUC'");
		$GE_EMP_TEL="934193690";
		$GE_SER="T".yy(vbr("00000".gs("SER","vguias","ID=$ID"),3));
		$GE_NUM=vbr("000000000".yy(gs("NUM","vguias","ID=$ID")),6);
		$GE_CLI_NOM=yy(gs("DES_RS","vguias","ID=$ID"));
		$GE_CLI_RUC=yy(gs("DES_RUC","vguias","ID=$ID"));
		$GE_CLI_DNI=yy(gs("DES_DNI","vguias","ID=$ID"));
		$GE_CLI_DEP=yy(gs("UBI_DEP","vguias","ID=$ID"));
		$GE_CLI_PRO=yy(gs("UBI_PRO","vguias","ID=$ID"));
		$GE_CLI_DIS=yy(gs("UBI_DIS","vguias","ID=$ID"));
		$GE_CLI_DIR=yy(gs("DES_DIR","vguias","ID=$ID"));
		$GE_TOT_CAJ=0;
		//
		$CAB=$CAB.$GE_EMP."|".$GE_SER."|".$GE_NUM."|".$GE_CLI_NOM."|".$GE_CLI_DIR."|".$GE_CLI_RUC."|".$GE_CLI_DNI."|";
		$CAB=$CAB.$GE_FEC_EMI."|".$GE_CLI_CIU."|".$GE_REF_DOC."|".$GE_REF_SER."|".$GE_REF_NUM."|".$GE_REF_SER_NUM."|";
		$CAB=$CAB.$GE_TIP_TRA."|".$GE_FEC_ENT."|".$GE_MTC."|".$GE_UND_MED."|".$GE_PES_BRU."|".$GE_MOT."|";
		$CAB=$CAB.$GE_PUN_PAR."|".$GE_PUN_LLE."|".$GE_EDT_NOM."|".$GE_EDT_RUC."|".$GE_CHO_TDI."|";
		$CAB=$CAB.$GE_CHO_DNI."|".$GE_CHO_NOM."|".$GE_CHO_APE."|".$GE_CHO_LIC."|".$GE_CHO_PLA."|".$GE_LINK."|".$GE_EMP_RUC."|";
		//
		$s="SELECT COD,DES,PRE,IMP,PIE,CAN FROM pri_gia_det WHERE ID=$ID ORDER BY CX";
		$rs=grs($s);			
		while($r=$rs->fetch_assoc()){
			$CAN=yy($r['CAN']);
			$GE_TOT_CAJ=$GE_TOT_CAJ + $CAN;
		}
		//++
		$GE_CLI_RUC="-";
		$GE_CLI_DNI="-";
		//
		$CAB="";
		$CAB=$CAB."|OK|".$GE_EMP_NOM."|".$GE_EMP_TEL."|".$GE_SER."|".$GE_NUM."|".$GE_CLI_NOM."|".$GE_CLI_RUC."|".$GE_CLI_DNI."|";
		$CAB=$CAB.$GE_CLI_DEP."|".$GE_CLI_PRO."|".$GE_CLI_DIS."|".$GE_CLI_DIR."|".$GE_TOT_CAJ."|";
		//
		$DAT=$CAB."'"."PROCESO_COMPLETADO";
		echo $DAT;return;
	}
?>