<?php
	//IMPORTACIONES LQ	
	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']);
	//
	$EMP=$P3;
	$SER=vbval($P1);$NUM=vbval($P2);
	//
	if($CM=="EM"){
		$L="";
		$s="SELECT emp_nomc as E FROM empresas WHERE id_emp>0 ORDER BY emp_nomc";
		$result = grs($s);
		while ($r = $result->fetch_assoc()){
			$L=$L.$r['E']."'";
		}
		echo $L;return;
	}
	if($CM=="ZT"){
		if($SER>0){
			$ID=gn("ID","vtransferencias","TSER_=$SER AND TNUM_=$NUM");
		}else{
			$ID=gn("ID","vtransferencias","ID=$NUM AND O_EMPC='$EMP'");			
		}		
		if($ID==0){
			echo "NR";
			return;
		}
		$FE_D=y(gs("DATE_FORMAT(fec,'%d')","transferencias","id_trans=$ID"));
		$FE_M=y(gs("DATE_FORMAT(fec,'%m')","transferencias","id_trans=$ID"));
		$FE_A=y(gs("DATE_FORMAT(fec,'%y')","transferencias","id_trans=$ID"));
		$CLI=y(gs("CONCAT(ORI,' -> ',DES)","vtransferencias","ID=$ID"));
                $CARR=y(gs("CARR","vtransferencias","ID=$ID"));
		$GIA=y(gs("CONCAT(tser,'-',tnum)","transferencias","id_trans=$ID"));
		$CAB=$FE_D.$Z.$FE_M.$Z.$FE_A.$Z.$CLI.$Z.$GIA.$Z.$CARR;
		//
		$s="SELECT COD,DES, 0 as PRE,0 as IMP, CAN_UNI as PIE, KA as CAN 
		FROM vtransferencias_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=y($row['DES']);
			$DET=$DET.$COD.$Z.$DES.$Z.$PRE.$Z.$IMP.$Z.$PIE.$Z.$CAN."=";
		}
		$DAT=$CAB."'".$DET;
		echo $DAT;return;
	}
	//
	if($CM=="GI"){
		if($SER>0){
			$ID=gn("ID","vginterna","ISER=$SER AND INUM=$NUM AND EMPC='$EMP'");
		}else{
			$ID=gn("ID","vginterna","INUM=$NUM AND EMPC='$EMP'");
		}
		if($ID==0){
			echo "NR";
			return;
		}
		$FE_D=y(gs("DATE_FORMAT(FEC,'%d')","vginterna","ID=$ID"));
		$FE_M=y(gs("DATE_FORMAT(FEC,'%m')","vginterna","ID=$ID"));
		$FE_A=y(gs("DATE_FORMAT(FEC,'%y')","vginterna","ID=$ID"));
		$CLI=y(gs("CLI","vginterna","ID=$ID"));
		$CAR=y(gs("CAR","vginterna","ID=$ID"));
		$GIA=y(gs("CONCAT(COT_SER,'-',COT_NUM)","vginterna","ID=$ID"));
		$CAB=$FE_D.$Z.$FE_M.$Z.$FE_A.$Z.$CLI.$Z.$GIA.$Z.$CAR;
		//
		$s="SELECT PRO_COD as COD,PRO_DES as DES, 0 as PRE,0 as IMP, PIEZAS as PIE, CA as CAN 
		FROM vginterna_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=y($row['DES']);
			$DET=$DET.$COD.$Z.$DES.$Z.$PRE.$Z.$IMP.$Z.$PIE.$Z.$CAN."=";
		}
		$DAT=$CAB."'".$DET;
		echo $DAT;return;		
	}
	//
	if($CM=="G"){
		$ID=gn("ID","vguias","ISER=$SER AND INUM=$NUM AND EMPC='$EMP'");
		if($ID==0){
			echo "NR";
			return;
		}
		$FE_E=y(gs("DATE_FORMAT(FEC,'%d-%m-%y')","vguias","ID=$ID"));		
		$FE_T=y(gs("DATE_FORMAT(FIT,'%d-%m-%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_DIR=y("");
		$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($CR_NOM=="BOL. ELEC."){
			$CR_SER="B".vbr("000".$CR_SER,3);
		}
		if($CR_NOM=="FAC. ELEC."){
			$CR_SER="F".vbr("000".$CR_SER,3);
		}
		if(strlen($CR_SER)>0){
			$CR_NUM=$CR_SER."-".$CR_NUM;
		}
		$CR_NUM=y($CR_NUM);
		$CAB=$FE_E.$Z.$FE_T.$Z.$PAR.$Z.$LLE.$Z.$DES_NOM.$Z.$DES_DIR.$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;return;
		
	}
	//
	if($CM=="B"){
		$ID=gn("ID","vventas","ISER=$SER AND INUM=$NUM AND COM='BOLETA' AND EMPC='$EMP'");
		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"),2));
		$GIA=y(gs("ven_gia","ventas","id_venta=$ID"));
		$SON=n2l($TOT);
		$TDP=y(gs("TDP","vventas","ID=$ID"));
		$CAB=$CLI.$Z.$DIR.$Z.$DNI.$Z.$DIA.$Z.$MES.$Z.$ANI.$Z.$TOT.$Z.$SON.$Z.$GIA.$Z.$TDP;	
		//
		$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'],2));
			$IMP=y(NF($row['IMP'],2));
			$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;return;
	}
	//
	if($CM=="F"){
		$ID=gn("ID","vventas","ISER=$SER AND INUM=$NUM AND COM='FACTURA' AND EMPC='$EMP'");
		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"),2));
		$IGV=y(NF(gn("IGV","vventas","ID=$ID"),2));
		$TOT=y(NF(gn("TOT","vventas","ID=$ID"),2));
		$SON=n2l(gn("TOT","vventas","ID=$ID"));
		$TDP=y(gs("TDP","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.$Z.$TDP;	
		//
		$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'],2));
			$IMP=y(NF($row['IMP'],2));
			$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;return;
	}
	//
	if($CM=="P"){				
		$ID=gn("ID","vcotizaciones","ISER=$SER AND INUM=$NUM AND EMPC='$EMP'");
		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(NF2(gn("TOT","vcotizaciones","ID=$ID"),2));
		$DSC=y(NF(gn("TOT_DES","vcotizaciones","ID=$ID"),2));
		$TOR=y(NF(gn("TOT_REAL","vcotizaciones","ID=$ID"),2));
		$VEN=y(gs("VEN","vcotizaciones","ID=$ID"));
		$VAL=y(gs("val","cotizaciones_con",""));
		$KON=y(gs("fdp","cotizaciones_con",""));
		$CAB=$DIA.$Z.$MES.$Z.$ANI.$Z.$CLI.$Z.$DIR.$Z.$RUC.$Z.$TOT.$Z.$DSC.$Z.$TOR.$Z.$VEN.$Z.$VAL.$Z.$KON;	
		//			
		$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'],2));
			$IMP=y(NF($row['IMP'],2));
			$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;
		//
		$PA_ID=GenId("id_ci","cotizaciones_imprimir");
		$PA_COT_ID=$ID;
		$PA_TIM=time();
		$PA_FEC=date("Y-m-d");
		$PA_HOR=GetHoraAP();
		$PA_PRN="N";
		$PA_PRN_TIM=0;
		$PA_PRN_FEC="1900-01-01";
		$PA_PRN_HOR="";
		$PA_SQL="INSERT INTO cotizaciones_imprimir VALUES ($PA_ID,$PA_COT_ID,$PA_TIM,'$PA_FEC','$PA_HOR','$PA_PRN',$PA_PRN_TIM,'$PA_PRN_FEC','$PA_PRN_HOR')";
		ejt($PA_SQL);
		//
		echo $DAT;return;
	}
	//
	if($CM=="P_ID"){				
		$ID=$SER;
		$PA_TIM=time();
		$PA_FEC=date("Y-m-d");
		$PA_HOR=GetHoraAP();
		$s="UPDATE cotizaciones_imprimir SET ci_prn='S',ci_prn_tim=".time().",ci_prn_fec='$PA_FEC',ci_prn_hor='$PA_HOR' WHERE id_ci=$NUM";
		ejt($s);
		//echo $s;
		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(NF2(gn("TOT","vcotizaciones","ID=$ID"),2));
		$DSC=y(NF(gn("TOT_DES","vcotizaciones","ID=$ID"),2));
		$TOR=y(NF(gn("TOT_REAL","vcotizaciones","ID=$ID"),2));
		$VEN=y(gs("VEN","vcotizaciones","ID=$ID"));
		$VAL=y(gs("val","cotizaciones_con",""));
		$KON=y(gs("fdp","cotizaciones_con",""));
		$EWP=y(gs("EMPC","vcotizaciones","ID=$ID"));
		$SERIE=y(gs("SER","vcotizaciones","ID=$ID"));
		$NUMERO=y(gs("NUM","vcotizaciones","ID=$ID"));
		$CAB=$DIA.$Z.$MES.$Z.$ANI.$Z.$CLI.$Z.$DIR.$Z.$RUC.$Z.$TOT.$Z.$DSC.$Z.$TOR.$Z.$VEN.$Z.$VAL.$Z.$KON.$Z.$EWP.$Z.$SERIE.$Z.$NUMERO;	
		//			
		$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'],2));
			$IMP=y(NF($row['IMP'],2));
			$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;return;
	}
	//++
	if($CM=="P_EMP_SER_NUM"){
		$EMP=V($_GET['P1']);
		$SER=vbval(V($_GET['P2']));
		$NUM=vbval(V($_GET['P3']));
		$CX=gn("count(*)","vcotizaciones","EMPC='$EMP' AND ISER=$SER AND INUM=$NUM");
		if($CX==0){
			echo "NO_REGISTRADO";
			return;
		}
		$SER=gn("ID","vcotizaciones","EMPC='$EMP' AND ISER=$SER AND INUM=$NUM");
		$CM="P_ID_EX";		
	}
	//++
	if($CM=="P_ID_EX"){				
		$ID=$SER;		
		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"));
		$CLI_TEL=y(gs("CLI_TEL","vcotizaciones","ID=$ID"));
		$CLI_CEL=y(gs("CLI_CEL","vcotizaciones","ID=$ID"));
		$DIR=y(gs("DIR","vcotizaciones","ID=$ID"));
		$RUC=y(gs("RUC","vcotizaciones","ID=$ID"));
		$TOT=y(NF2(gn("TOT","vcotizaciones","ID=$ID"),2));
		$DSC=y(NF(gn("TOT_DES","vcotizaciones","ID=$ID"),2));
		$TOR=y(NF(gn("TOT_REAL","vcotizaciones","ID=$ID"),2));
		$VEN=y(gs("VEN","vcotizaciones","ID=$ID")." - ".gs("PER_CEL","vcotizaciones","ID=$ID"));
		$VAL=y(gs("val","cotizaciones_con",""));
		$KON=y(gs("fdp","cotizaciones_con",""));
		$EWP=y(gs("EMPC","vcotizaciones","ID=$ID"));
		$SERIE=y(gs("SER","vcotizaciones","ID=$ID"));
		$NUMERO=y(gs("NUM","vcotizaciones","ID=$ID"));
		$EMP_KOD=gn("ID_EMP","vcotizaciones","ID=$ID");
		$EMP_KOD_CUE=gn("cue_id_emp","cotizaciones","id_cotizacion=$ID");
		if($EMP_KOD_CUE==0){
			//$EMP_KOD_CUE=$EMP_KOD;
		}
		$CAB=$DIA.$Z.$MES.$Z.$ANI.$Z.$CLI.$Z.$DIR.$Z.$RUC.$Z.$TOT.$Z.$DSC.$Z.$TOR.$Z.$VEN.$Z.$VAL.$Z.$KON.$Z.$EWP.$Z.$SERIE.$Z.$NUMERO.$Z.$CLI_TEL.$Z.$CLI_CEL;	
		//			
		$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'],2));
			$IMP=y(NF($row['IMP'],2));
			$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;
		//
		$CUENTAS="";
		if($EMP_KOD_CUE>0){
			$s="SELECT * FROM vis_cuentas_emp WHERE HAB='SI' AND EMP_ID=$EMP_KOD_CUE ORDER BY BAN";
			//$s="SELECT * FROM vis_cuentas_emp WHERE HAB='SI' ORDER BY BAN";
			$rs = grs($s);			
			while ($r = $rs->fetch_assoc()){
				$CUENTAS=$CUENTAS.y($r['BAN']).$Z.y($r['MND']).$Z.y($r['NRO']).$Z.y($r['CCI'])."=";
			}
		}
		//
		$DAT=$CAB."'".$DET."'".$CUENTAS."'PROCESO_COMPLETADO";
		//
		echo $DAT;return;
	}
?>