<?php
	//IMPORTACIONES LQ	
	include("cls_conexion.php");$CNN=new ClsMySQL();include("conexion23.php");
	include("fe_kernel.php");
	/*
	error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
*/
	//++++++++++++++++++++++++++
	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=$P3;
	$SER=vbval($P1);
	$NUM=vbval($P2);
	//
	//
	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 "NO_REGISTRADO";
			//+++++++++++++++++++
			return;
		}
		$SER=gn("ID","vguias","EMPC='$EMP' AND ISER=$SER AND INUM=$NUM");
		$CM="GRE_ID_EX";		
	}
	//
	if($CM=="GRE_ID_EX"){				
		$ID=$SER;		
		if($ID==0){
			//+++++++++++++++++++
			echo "NO_REGISTRADO";
			//+++++++++++++++++++
			return;
		}
		//
		$CX=gn("count(*)","vguias","ID=$ID AND TIP_GIA='GUIA DE REMISION ELECTRONICA'");
		if($CX==0){
			//+++++++++++++++++++
			echo "NO_REGISTRADO";
			//+++++++++++++++++++
			return;
		}
		//
		$GE_GEN=gs("CE_GEN","vguias","ID=$ID");			
		if($GE_GEN!="S"){
			//
			echo "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 "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_TIP=yy(gs("TIP_GIA","vguias","ID=$ID"));
		if($GE_TIP!="GUIA DE REMISION ELECTRONICA"){
			//
			echo "NO_REGISTRADO";
			//
			return;
		}
		$GE_SER="T".yy(vbr("00000".gs("SER","vguias","ID=$ID"),3));
		$GE_NUM=yy(gs("NUM","vguias","ID=$ID"));
		//
		$GE_CLI_NOM=yy(gs("DES_RS","vguias","ID=$ID"));
		$GE_CLI_DIR=yy(gs("DES_DIR","vguias","ID=$ID"));
		$GE_CLI_RUC=yy(gs("DES_RUC","vguias","ID=$ID"));
		$GE_CLI_DNI=yy(gs("DES_DNI","vguias","ID=$ID"));
		$GE_FEC_EMI=yy(gs("DATE_FORMAT(FEC,'%d-%m-%Y')","vguias","ID=$ID"));	
		$GE_CLI_CIU=yy(gs("CONCAT(UBI_DIS,' - ',UBI_PRO,' - ',UBI_DEP)","vguias","ID=$ID"));
		//
		$GE_REF_DOC=yy(gs("COMR","vguias","ID=$ID"));
		$GE_REF_SER=gs("SERR","vguias","ID=$ID");
		$GE_REF_NUM=gs("NUMR","vguias","ID=$ID");
		if($GE_REF_DOC=="BOL. ELEC."){
			$GE_REF_SER="B".vbr("000".$GE_REF_SER,3);
		}
		if($GE_REF_DOC=="FAC. ELEC."){
			$GE_REF_SER="F".vbr("000".$GE_REF_SER,3);
		}
		$GE_REF_SER_NUM=$GE_REF_SER."-".$GE_REF_NUM;
		$GE_TIP_TRA=yy(gs("TIP_TRA","vguias","ID=$ID"));
		$GE_FEC_ENT=yy(gs("DATE_FORMAT(FIT,'%d-%m-%Y')","vguias","ID=$ID"));
		$GE_MTC=yy("");
		$GE_UND_MED=yy("KGM");
		$GE_PES_BRU=yy(gs("PESO","vguias","ID=$ID"));
		$GE_MOT=yy(gs("MOTIVO","vguias","ID=$ID"));
		$GE_PUN_PAR=yy(gs("DIR_PAR","vguias","ID=$ID"));
		$GE_PUN_LLE=yy(gs("DES_DIR","vguias","ID=$ID"));
		//
		$GE_EDT_NOM=yy(gs("ET_RS","vguias","ID=$ID"));
		$GE_EDT_RUC=yy(gs("ET_RU","vguias","ID=$ID"));	
		//
		$GE_CHO_TDI=yy(gs("CHO_TDI","vguias","ID=$ID"));
		$GE_CHO_DNI=yy(gs("TDI_NRO","vguias","ID=$ID"));	
		$GE_CHO_NOM=yy(gs("CHO_NOM","vguias","ID=$ID"));	
		$GE_CHO_APE=yy(gs("CHO_APE","vguias","ID=$ID"));	
		$GE_CHO_LIC=yy(gs("TLIC","vguias","ID=$ID"));
		$GE_CHO_PLA=yy(gs("TPLA","vguias","ID=$ID"));
		$GE_LINK=y(gs("LINK","vguias","ID=$ID"));
		//
		$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()){
			$COD=yy($r['COD']);			
			$PRE=yy($r['PRE']);
			$IMP=yy($r['IMP']);
			$PIE=yy($r['PIE']);
			$CAN=yy($r['CAN']);
            $DES=yy($r['DES']);
			//$DET=$DET.$COD.$Z.$DES.$Z.$PRE.$Z.$IMP.$Z.$PIE.$Z.$CAN."=";
			$DET=$DET.$COD.$Z.$PRE.$Z.$IMP.$Z.$PIE.$Z.$CAN.$Z.$DES."=";
		}
		$DAT=$CAB."'".$DET."'PROCESO_COMPLETADO";
		echo $DAT;return;
	}
?>