<?php
	session_start();
	include("cls_conexion.php");$CNN=new ClsMySQL();include("conexion23.php");
	include("lq_fe.php");
	include("ip.php");	
	$IDU=vbval(V($_SESSION['SYS_USER_ID']));
	$ALM=gn("id_alm","usuarios","id_usuario=".$IDU);
	//
	$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']);
	$P8=V($_GET['P8']);
	$P9=V($_GET['P9']);
	//
	if($CM=="FE_GEN"){
		//
		$TXT="";
		$SYS_FEC=date("Y-m-d");
		$SYS_HOR=GetHoraAP();
		$SYS_TIM=time();
		$SYS_IP=Saber_IP();
		//
		$VEN_ID=vbval($P1);
		$USU_ID=vbval($P2);
		//
		$CX=gn("count(*)","ventas","id_venta=$VEN_ID");
		if($CX==0){
			echo "|MS|*** Error : Venta no registrada ***|";
			return;
		}
		$r=gr("ventas","id_venta=".$VEN_ID);
		if($r['id_com']==3 || $r['id_com']==4){
			if($r['anu']=="S"){
				echo "|MS|*** Error : La venta ha sido anulada ***|";
				return;
			}
		}else{
			echo "|MS|*** Error : La venta no corresponde a un documento electronico ***|";
			return;
		}
		//
ejt("DELETE FROM facturas_electronicas WHERE fe_id_venta=$VEN_ID");
		$ID_FE=gn("id_fe","facturas_electronicas","fe_id_venta=$VEN_ID");
		if($ID_FE==0){	
$TOT_SUM=0;	
$TOT_ZUM=round(gn("TOT","vventas_fe","ID=".$VEN_ID),2);		
			$STR_TXT=GEN_DOC_ELE($VEN_ID,$TXT,$TOT_SUM);
$TOT_SUM=round($TOT_SUM,2);
			if($TOT_ZUM!=$TOT_SUM){
				//echo "|MS|SUMA INCORRECTA TOT1=$TOT_ZUM, TOT2=$TOT_SUM|";
				//return;
			}
			if(file_exists($TXT)){
				unlink($TXT);
			}	
			CREATE_FILE("facturas_electronicas/".$TXT,$STR_TXT);
			//
			$ID_FE=GenId("id_fe","facturas_electronicas");
			$s="INSERT INTO facturas_electronicas 
				VALUES ($ID_FE,'$SYS_FEC','$SYS_HOR',$SYS_TIM,'$SYS_IP',$USU_ID,$VEN_ID,'$TXT','$STR_TXT',0)";
			ejt($s);
			//
			ejt("UPDATE ventas SET ven_fe_ge='S' WHERE id_venta=$VEN_ID");
			//	
			echo "|OK|$ID_FE|";		
		}else{
			$DAT_NDW=gn("fe_nd","facturas_electronicas","id_fe=$ID_FE");
			$DAT_TXT=gs("fe_txt_dat","facturas_electronicas","id_fe=$ID_FE");
			$STR_TXT=GEN_DOC_ELE($VEN_ID,$TXT);
			if($DAT_TXT!=$STR_TXT){
				echo "|MS|*** Error : Incongruencia de datos, uno o mas datos han sido alterados ***|";
			}else{
				echo "|OK|$ID_FE|$DAT_NDW|";
			}			
		}
	}
	//
	if($CM=="07"){
		$MSG="";
		$_EMPR=$P1;
		$_SERR=vbval($P2);
		$_NUMR=vbval($P3);
		$_HWND=$P4;
		//
		$_EMPR=gn("id_emp","empresas","emp_nomc='$_EMPR'");
		$COT_=gn("ID_COT","vix_cotizaciones","SER_=$_SERR AND NUM_=$_NUMR AND ID_EMP=$_EMPR");
		//
		$s="SELECT temc_id_producto as IDP,temc_cant as CAN,dscto as UNI  
			FROM temp_compras WHERE cod_temc='$_HWND' ORDER BY temc_index";
		$result = grs($s);			
		while ($row = $result->fetch_assoc()){
			$_IDP=$row['IDP'];
			$_UNI=$row['UNI'];
			$_CAN=$row['CAN'];			
			$_KAN=gn("ca_control - dej","cotizaciones_det",
					 "id_cotizacion=$COT_ AND id_producto=$_IDP AND can_uni=$_UNI");
			if($_CAN>$_KAN){				
				$MSG="CANTIDAD SUPERIOR : $_COD x $_UNI , cantidad maxima a emitir : $_KAN";
				break;
			}
		}
		if(strlen($MSG)==0){
			echo "|OK|";
		}else{
			echo "|$MSG|";
		}
	}
	//PHP-BUSCAR
	if($CM=="06"){
		$ID=COT_GET_ID_COMPRA($P1,$P2,$P3);		
		if($ID==0){
			echo "Documento de referencia no registrado";
		}else{
			$AN=gs("anulado","compras","id_compra=$ID");
			if($AN=="N"){
				echo "OK";
			}else{
				echo "El documento de referencia ha sido anulado";
			}
		}
	}
	//
	if($CM=="05"){
		$IE=gn("id_emp","empresas","emp_nomc='$P3'");
		$CO=$P1;
		$SN=explode("-",$P2);
		$SE=$SN[0];
		$NU=$SN[1];
		$IR=VEN_GET_ID_REF($CO,$SE,$NU,$IE);
		if($IR==0){
			echo "Documento de referencia no registrado";
		}else{
			$AN="N";
			if($CO=="PROFORMA"){
				$AN=gs("anu","cotizaciones","id_cotizacion=$IR");
			}else{
				$AN=gs("anulado","compras","id_compra=$IR");
			}
			if($AN=="N"){
				echo "OK";
			}else{
				echo "El documento de referencia ha sido anulado, por lo tanto no se puede proceder";
			}
		}
	}
	//
	if($CM=="04"){
		$IA=gn("id_almacen","almacenes","alm_obs='$P4'");
		echo TRANS_GET_ID_REF($P1,$P2,$P3,$IA);
	}
	//
	if($CM=="03"){
		$G="";		
		if($P1=="TODOS")$P1="%";
		if($ALM==0){
			$G=$G."TODOS|";
			$s="SELECT ANC FROM vis_almacenes 
				WHERE IDA>0 AND ENC LIKE '$P1'  
				GROUP BY ANC 
				ORDER BY ANC ";		
		}else{
			$s="SELECT ANC FROM vis_almacenes 
				WHERE IDA=$ALM";		
		}
		$rs = grs($s);
		while ($r = $rs->fetch_assoc()){
			$G=$G.$r['ANC']."|";
		}
		if(strlen($G)>0){
			$G=vbmid($G,1,strlen($G)-1);
		}else{
			$G="X";
		}
		echo $G;
	}
	//PHP
	if($CM=="02"){
		$G="";		
		if($ALM==0){
			$s="SELECT ANC FROM vis_almacenes 
				WHERE ENC='$P1' 
				GROUP BY ANC 
				ORDER BY ANC ";		
		}else{
			$s="SELECT ANC FROM vis_almacenes 
				WHERE IDA=$ALM";		
		}
		$rs = grs($s);
		while ($r = $rs->fetch_assoc()){
			$G=$G.$r['ANC']."|";
		}
		if(strlen($G)>0){
			$G=vbmid($G,1,strlen($G)-1);
		}else{
			$G="X";
		}
		echo $G;
	}
	//
	if($CM=="01"){
		if($P1=="TODOS")$P1="%";
		$G="";
		$G=$G."TODOS|";
		$s="SELECT ANC FROM vis_almacenes WHERE ENC LIKE '$P1'  ORDER BY ANC";		
		$rs = grs($s);
		while ($r = $rs->fetch_assoc()){
			$G=$G.$r['ANC']."|";
		}
		if(strlen($G)>0){
			$G=vbmid($G,1,strlen($G)-1);
		}else{
			$G="X";
		}
		echo $G;
	}
	//
	if($CM=="00"){
		$G="";		
		$s="SELECT ANC FROM vis_almacenes WHERE ENC='$P1' ORDER BY ANC";		
		$rs = grs($s);
		while ($r = $rs->fetch_assoc()){
			$G=$G.$r['ANC']."|";
		}
		if(strlen($G)>0){
			$G=vbmid($G,1,strlen($G)-1);
		}else{
			$G="X";
		}
		echo $G;
	}
?>