<?php
	//PANDA
	session_start();
	include("cls_conexion.php");$CNN=new ClsMySQL();include("conexion23.php");
	//
	$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']);
	//
	$SER=vbval($P1);$NUM=vbval($P2);
	//
	if($CM=="G"){
		$ID=gn("ID","vguias","ISER=$SER AND INUM=$NUM");
		if($ID==0){
			echo "NR";
			return;
		}
		$FE_D=y(gs("DATE_FORMAT(FEC,'%d')","vguias","ID=$ID"));
		$FE_M=y(gs("DATE_FORMAT(FEC,'%m')","vguias","ID=$ID"));
		$FE_A=y(gs("DATE_FORMAT(FEC,'%y')","vguias","ID=$ID"));
		
		$FT_D=y(gs("DATE_FORMAT(FIT,'%d')","vguias","ID=$ID"));
		$FT_M=y(gs("DATE_FORMAT(FIT,'%m')","vguias","ID=$ID"));
		$FT_A=y(gs("DATE_FORMAT(FIT,'%y')","vguias","ID=$ID"));
		
		$PAR=y(gs("DIR_PAR","vguias","ID=$ID"));
		$LLE=y(gs("DES_DIR","vguias","ID=$ID"));
		
		$DES_NOM=y(gs("DES_RS","vguias","ID=$ID"));
		$DES_RUC=y(gs("DES_RUC","vguias","ID=$ID"));
		$DES_TDI=y(gs("DES_DNI","vguias","ID=$ID"));
		
		$UT_MP=y(gs("CONCAT(TMAR,' ',TPLA)","vguias","ID=$ID"));
		$UT_CE=y(gs("TCON","vguias","ID=$ID"));
		$UT_LI=y(gs("TLIC","vguias","ID=$ID"));
		
		$ET_NOM=y(gs("ET_RS","vguias","ID=$ID"));
		$ET_RUC=y(gs("ET_RU","vguias","ID=$ID"));
		
		$CR_NOM=y(gs("COMR","vguias","ID=$ID"));
		$CR_SER=gs("SERR","vguias","ID=$ID");
		$CR_NUM=gs("NUMR","vguias","ID=$ID");
		if(strlen($CR_SER)>0){
			$CR_NUM=$CR_SER."-".$CR_NUM;
		}
		$CR_NUM=y($CR_NUM);
		$CAB=$FE_D.$Z.$FE_M.$Z.$FE_A.$Z.
			 $FT_D.$Z.$FT_M.$Z.$FT_A.$Z.
			 $PAR.$Z.$LLE.$Z.
			 $DES_NOM.$Z.$DES_RUC.$Z.$DES_TDI.$Z.
			 $UT_MP.$Z.$UT_CE.$Z.$UT_LI.$Z.
			 $ET_NOM.$Z.$ET_RUC.$Z.
			 $CR_NOM.$Z.$CR_NUM;
		//
		$s="SELECT COD,DES,PRE,IMP,PIE,CAN FROM pri_gia_det WHERE ID=$ID ORDER BY CX";
		$result = grs($s);			
		while ($row = $result->fetch_assoc()){
			$COD=y($row['COD']);			
			$PRE=y($row['PRE']);
			$IMP=y($row['IMP']);
			$PIE=y($row['PIE']);
			$CAN=y($row['CAN']);
            $DES=$COD."x".$PIE." ".y($row['DES']);
			$DET=$DET.$COD.$Z.$DES.$Z.$PRE.$Z.$IMP.$Z.$PIE.$Z.$CAN."=";
		}
		$DAT=$CAB."'".$DET;
		echo $DAT;
		
	}
	//
	if($CM=="B"){
		$ID=gn("ID","vventas","ISER=$SER AND INUM=$NUM AND COM='BOLETA'");
		if($ID==0){
			echo "NR";
			return;
		}
		$CLI=y(gs("CLI","vventas","ID=$ID"));
		$DIR=y(gs("DIR","vventas","ID=$ID"));
		$DNI=y(gs("DNI","vventas","ID=$ID"));
		$DIA=y(gs("DATE_FORMAT(FEC,'%d')","vventas","ID=$ID"));
		$MES=y(gs("DATE_FORMAT(FEC,'%m')","vventas","ID=$ID"));
		$ANI=y(gs("DATE_FORMAT(FEC,'%Y')","vventas","ID=$ID"));
		$TOT=y(NF(gn("TOT","vventas","ID=$ID"),3));
		$GIA=y(gs("ven_gia","ventas","id_venta=$ID"));
		$SON=n2l($TOT);
		$CAB=$CLI.$Z.$DIR.$Z.$DNI.$Z.$DIA.$Z.$MES.$Z.$ANI.$Z.$TOT.$Z.$SON.$Z.$GIA;	
		//
		$s="SELECT COD,DES,PRE,IMP,PIE,CAN FROM pri_ven_det WHERE ID=$ID ORDER BY CX";
		$result = grs($s);			
		while ($row = $result->fetch_assoc()){
			$COD=y($row['COD']);			
			$PRE=y(NF($row['PRE'],3));
			$IMP=y(NF($row['IMP'],3));
			$PIE=y($row['PIE']);
			$CAN=y($row['CAN']);
            $DES=$COD."x".$PIE." ".y($row['DES']);
			$DET=$DET.$COD.$Z.$DES.$Z.$PRE.$Z.$IMP.$Z.$PIE.$Z.$CAN."=";
		}
		$DAT=$CAB."'".$DET;
		echo $DAT;
	}
	//
	if($CM=="F"){
		$ID=gn("ID","vventas","ISER=$SER AND INUM=$NUM AND COM='FACTURA'");
		if($ID==0){
			echo "NR";
			return;
		}
		$CLI=y(gs("CLI","vventas","ID=$ID"));
		$DIR=y(gs("DIR","vventas","ID=$ID"));
		$RUC=y(gs("RUC","vventas","ID=$ID"));
		$DIA=y(gs("DATE_FORMAT(FEC,'%d')","vventas","ID=$ID"));
		$MES=y(gs("DATE_FORMAT(FEC,'%m')","vventas","ID=$ID"));
		$ANI=y(gs("DATE_FORMAT(FEC,'%Y')","vventas","ID=$ID"));
		$GIA=y(gs("ven_gia","ventas","id_venta=$ID"));
		$SUB=y(NF(gn("ST","vventas","ID=$ID"),3));
		$IGV=y(NF(gn("IGV","vventas","ID=$ID"),3));
		$TOT=y(NF(gn("TOT","vventas","ID=$ID"),3));
		$SON=n2l(gn("TOT","vventas","ID=$ID"));
		$CAB=$CLI.$Z.$DIR.$Z.$RUC.$Z.$DIA.$Z.$MES.$Z.$ANI.$Z.$GIA.$Z.$SUB.$Z.$IGV.$Z.$TOT.$Z.$SON;	
		//
		$s="SELECT COD,DES,PRE,IMP,PIE,CAN FROM pri_ven_det WHERE ID=$ID ORDER BY CX";
		$result = grs($s);			
		while ($row = $result->fetch_assoc()){
			$COD=y($row['COD']);			
			$PRE=y(NF($row['PRE'],3));
			$IMP=y(NF($row['IMP'],3));
			$PIE=y($row['PIE']);
			$CAN=y($row['CAN']);
            $DES=$COD."x".$PIE." ".y($row['DES']);
			$DET=$DET.$COD.$Z.$DES.$Z.$PRE.$Z.$IMP.$Z.$PIE.$Z.$CAN."=";
		}
		$DAT=$CAB."'".$DET;
		echo $DAT;
	}
	//
	if($CM=="P"){				
		$ID=gn("ID","vcotizaciones","ISER=$SER AND INUM=$NUM");
		if($ID==0){
			echo "NR";
			return;
		}
		$DIA=y(gs("DATE_FORMAT(FEC,'%d')","vcotizaciones","ID=$ID"));
		$MES=y(gs("DATE_FORMAT(FEC,'%m')","vcotizaciones","ID=$ID"));
		$ANI=y(gs("DATE_FORMAT(FEC,'%y')","vcotizaciones","ID=$ID"));
		$CLI=y(gs("CLI","vcotizaciones","ID=$ID"));
		$DIR=y(gs("DIR","vcotizaciones","ID=$ID"));
		$RUC=y(gs("RUC","vcotizaciones","ID=$ID"));
		$TOT=y(NF(gn("TOT","vcotizaciones","ID=$ID"),3));
		$DSC=y(NF(gn("TOT_DES","vcotizaciones","ID=$ID"),3));
		$TOR=y(NF(gn("TOT_REAL","vcotizaciones","ID=$ID"),3));
		$VEN=y(gs("VEN","vcotizaciones","ID=$ID"));
		$CAB=$DIA.$Z.$MES.$Z.$ANI.$Z.$CLI.$Z.$DIR.$Z.$RUC.$Z.$TOT.$Z.$DSC.$Z.$TOR.$Z.$VEN;	
		//			
		$s="SELECT COD,DES,PRE,IMP,PIE,CAN FROM pri_cot_det WHERE ID=$ID ORDER BY CX";
		$result = grs($s);			
		while ($row = $result->fetch_assoc()){
			$COD=y($row['COD']);			
			$PRE=y(NF($row['PRE'],3));
			$IMP=y(NF($row['IMP'],3));
			$PIE=y($row['PIE']);
			$CAN=y($row['CAN']);
            $DES=$COD."x".$PIE." ".y($row['DES']);
			$DET=$DET.$COD.$Z.$DES.$Z.$PRE.$Z.$IMP.$Z.$PIE.$Z.$CAN."=";
		}
		$DAT=$CAB."'".$DET;
		echo $DAT;
	}
?>