<?php
    $PRO_NO=0;
	$LIMPIAR=0;
	$StrHWND_T=$_SESSION['SYS_HW']."_T";
	$STR_ANU="N";
	//++++++++++++++++++++++++
	$StrDATA=V($_POST['TxtDAT']);
	$StrId=V($_POST['TxtId']);
	$StrCmd=V($_POST['TxtAc']);
	//-------------------------
	$StrFec=V($_POST['TxtFec']);
	$StrEmp=V($_POST['CboEmp']);
	$StrAlm=V($_POST['CboAlm']);
	//-------------------------
	if(strlen($StrEmp)==0)$StrEmp=GE("N");
	if(strlen($StrAlm)==0)$StrAlm=GA($StrEmp,"N");	
	//-------------------------
	$StrNum=V($_POST['TxtNum']);
	$StrEmpD=V($_POST['CboEmpD']);
	$StrAlmD=V($_POST['CboAlmD']);
	$StrTSer=V($_POST['TraSer']);
	$StrTNum=V($_POST['TraNum']);
	$StrCar=V($_POST['TxtCar']);
	//-------------------------
	if(strlen($StrCmd)==0){
		$StrCmd=V($_GET['TxtAc']);
		if($StrCmd=="X"){
			$StrCmd="C";			
		}
		$StrFec=date("d-m-y");
	}
	//------------------------
	$StrPro_ID=vbval(V($_POST['Pro_TxtId']));
	$StrPro_PR=V($_POST['TxtPre']);
	$StrPro_CA=vbval(V($_POST['TxtCant']));	
	//------------------------
	if(strlen($StrPro_PR)==0 || !is_numeric($StrPro_PR)) $StrPro_PR="0";
	//------------------------
	$StrCod=V($_POST['TxtCod']);
	$StrDes=V($_POST['TxtDes']);
	$StrMARCA=V($_POST['TxtMarca']);
	$StrCant=V($_POST['TxtCant']);
	$StrCUni=vbval(V($_POST['TxtCUni']));
	$StrCom=V($_POST['CboCom']);
	$StrPSER=V($_POST['TxtPSER']);
	$StrPNUM=V($_POST['TxtPNUM']);

	//ACTIVAR DESDE OTRA PAGINA
	if(strlen($StrId)==0){
		$StrId=V($_GET['Id']);
		if(is_numeric($StrId)==false){
			$StrId="";
		}else{
			$StrCmd="AR";
			$LStrPag=V($_GET['Pag']);
			if(!is_numeric($LStrPag)){
				$LStrPag="";
			}
		}
	}
	//FUNCIONES DE DESZPLAZAMIENTO
	if(strlen($LStrPag)==0){
		$LStrPag="0";
	}
	if($StrCmd=="MSig"){		
		$LStrPag=($LStrPag+1);
	}
	if($StrCmd=="MAnt"){
		$LStrPag=($LStrPag-1);
	}
	if($StrCmd=="MPri"){
		$LStrPag=0;
	}
	if($StrCmd=="MUlt"){
		$LStrPag=(floor($LStrTR / $VP_RegXPag) );
	}
	//FUNCION PARA ACTIVAR UN REGISTRO
	if(strlen($StrId)>0 && $StrCmd=="AR"){
		$Id=$StrId;
		$StrEmp=gs("O_EMPC","vtransferencias","ID=$Id");
		$StrEmpD=gs("D_EMPC","vtransferencias","ID=$Id");
		$StrAlm=gs("ORI","vtransferencias","ID=$Id");
		$StrAlmD=gs("DES","vtransferencias","ID=$Id");
		$StrFec=gs("DATE_FORMAT(FEC,'%d-%m-%y')","vtransferencias","ID=$Id");
		$StrNum=gs("NUM","vtransferencias","ID=$Id");
		$StrTSer=gs("TSER","vtransferencias","ID=$Id");
		$StrTNum=gs("TNUM","vtransferencias","ID=$Id");
		$STR_ANU=gs("ANU","vtransferencias","ID=$Id");
		$StrCom=gs("R_COM","vtransferencias","ID=$Id");
		$StrPSER=gs("R_SER","vtransferencias","ID=$Id");
		$StrPNUM=gs("R_NUM","vtransferencias","ID=$Id");
		$StrCar=gs("CARR","vtransferencias","ID=$Id");
		ejt("DELETE FROM temp_compras WHERE cod_temc='$StrHWND_T'");
		$s="SELECT * FROM transferencias_det WHERE id_trans=$Id ORDER BY cx";
		$cx=0;
		$result = grs($s);			
		while ($row = $result->fetch_row()){
			$cx++;
			$AR_ID=$row[2];
			$AR_CA=$row[3];
			$AR_CU=$row[4];
			$AR_PR=0;
			$AR_IM=0;
			$s="INSERT INTO temp_compras VALUES ('$StrHWND_T',$AR_ID,$AR_CA,
			$AR_PR,$AR_IM,$cx,'','',$AR_CU)";
			ejt($s);			
		}
	}
	if($StrCmd=="KCH"){
		$DAT = explode(',',$StrDATA);
		$i=0;
		for($i=0;$i<count($DAT);$i++){
			$STR_INDEX=vbval($DAT[$i]);
			ejt("DELETE FROM temp_compras WHERE 
				temc_index=$STR_INDEX AND cod_temc='$StrHWND_T'");
		}		
		$LStrFoc="document.getElementById('TxtCod').focus();";						
		$StrGT="S";		
	}
	//
	if($StrCmd=="C"){
		ejt("DELETE FROM temp_compras WHERE cod_temc='$StrHWND_T'");
		$StrAlm="";
		$StrAlmD="";
		$StrFec=date("d-m-y");
		$StrNum="";	
		$StrCod="";
		$StrDes="";	
		$StrMARCA="";
		$StrCant="";
		$StrCUni="";
		$StrId="";
		$StrPSER="";
		$StrPNUM="";
		$StrTSer="";
		$StrTNum="";
		$StrCar="";
	}
	//
	if($StrCmd=="KILLER"){
		ejt("DELETE FROM temp_compras WHERE temc_index=$StrPro_ID AND cod_temc='$StrHWND_T'");
	}
//----------------------------------------------------------------------------
if($StrCmd=="COT"){		
	if(strlen($StrGT)==0){	
		ejt("DELETE FROM temp_compras WHERE cod_temc='$StrHWND_T'");	
		$IA=gn("id_almacen","almacenes","alm_obs='$StrAlm'");
		$ID_PRO=TRANS_GET_ID_REF($StrCom,$StrPSER,$StrPNUM,$IA);	
		if($StrCom=="PROFORMA"){
			$s="SELECT id_producto,ca,can_uni,pr,im 
				FROM cotizaciones_det WHERE id_cotizacion=$ID_PRO";
		}else{
			$s="SELECT id_producto,ca,can_uni,pr,im 
				FROM compras_det WHERE id_compra=$ID_PRO";
		}
		$cx=0;
		$result = grs($s);			
		while ($row = $result->fetch_row()){
			$cx++;
			$AR_ID=$row[0];
			$AR_CA=$row[1];
			$AR_CU=$row[2];
			$AR_PR=$row[3];
			$AR_IM=$row[4];
			$s="INSERT INTO temp_compras VALUES ('$StrHWND_T',$AR_ID,$AR_CA,
			$AR_PR,$AR_IM,$cx,'','',$AR_CU)";
			ejt($s);			
		}
	}
}
//----------------------------------------------------------------------------
if($StrCmd=="ADD"){	
	if($StrPro_ID==0){
		$LStrMsg="No se puede determinar el ID del producto";
		$LStrFoc="document.getElementById('TxtCod').focus();";						
		$StrGT="S";			
	}
	if(strlen($StrGT)==0){
		if($StrPro_CA==0){
			$LStrMsg="La cantidad debe ser mayor a cero";
			$LStrFoc="document.getElementById('TxtCant').focus();";						
			$StrGT="S";							
		}
	}
	if(strlen($StrGT)==0){
		if(gn("count(*)","temp_compras",
			  "temc_id_producto=$StrPro_ID AND ".
			  "cod_temc='$StrHWND_T' AND dscto=$StrCUni")>0){
			$LStrMsg="El producto que desea agregar ya se encuentra en la lista";
			$LStrFoc="document.getElementById('TxtCod').focus();";						
			$StrGT="S";	
		}
	}
	if(strlen($StrGT)==0){			
		$StrImporte="0";
		$StrPro_PR="0";
		$StrIndex=GenId("temc_index","temp_compras");
		$s="INSERT INTO temp_compras VALUES ('$StrHWND_T',
			$StrPro_ID,$StrPro_CA,$StrPro_PR,
			$StrImporte,$StrIndex,'','',$StrCUni)";
		ejt($s);
		$StrCant="";
		$StrPre="";
		$StrDes="";
		$StrMARCA="";
		$StrCod="";
		$StrImp="";
		$StrCUni="";
		$LStrFoc="document.getElementById('TxtCod').focus();";						
		$StrGT="S";	
	}
}
//----------------------------------------------------------------------------
if(($StrCmd=="G" || $StrCmd=="M") && strlen($StrGT)==0){
	$StrEmp_=gn("id_emp","empresas","emp_nomc='$StrEmp'");		
	$StrEmpD_=gn("id_emp","empresas","emp_nomc='$StrEmpD'");		
	$StrAlm_=gn("id_almacen","almacenes","alm_obs='$StrAlm' AND alm_id_empresa=$StrEmp_");
	$StrAlmD_=gn("id_almacen","almacenes","alm_obs='$StrAlmD' AND alm_id_empresa=$StrEmpD_");
	if(gn("count(*)","temp_compras","cod_temc='$StrHWND_T'")==0){
		$LStrMsg="La lista de productos esta vacia";
		$LStrFoc="document.getElementById('TxtCod').focus();";						
		$StrGT="S";
	}
}
//FUNCIONES DE ACTUALIZACION
if($StrCmd=="M" && strlen($StrGT)==0){
	$LStrMsg="";
	$_VAL=gs("val","transferencias","id_trans=$StrId");
	if($_VAL=="S"){
		$LStrMsg="La presente transferencia ya ha sido ".
				 "validada por lo tanto no se puede modificar";
		$LStrFoc="document.getElementById('TxtCod').focus();";						
		$StrGT="S";
	}
	if(strlen($StrGT)==0){
		//BIT
		$_BIT_MOD_ID=$StrId;
		$_BIT_SQL_CAB="";//$s;
		$_BIT_SQL_DET="";
		$_BIT_PHP="transferencias.php";
		$_BIT_ID=BIT_ADD("ALMACEN","TRANSFERENCIAS","MODIFICAR",$_BIT_MOD_ID,$_BIT_SQL_CAB,$_BIT_SQL_DET,$_BIT_PHP);
		BIT_DET_ADD($_BIT_ID,"");
		//BIT	
		$qqq=ejt("DELETE FROM transferencias WHERE id_trans=$StrId");
		BIT_DET_ADD($_BIT_ID,$qqq);
		//
		$s="SELECT id_producto,cant FROM transferencias_det WHERE id_trans=$StrId";
		$result = grs($s);			
		while ($row = $result->fetch_row()){
			$E_ID=$row[0];
			$E_CA=$row[1];
			//STOCK_SUB($StrAlmD_,$E_ID,$E_CA);			
			//STOCK_ADD($StrAlm_,$E_ID,$E_CA);			
		}
		$qqq=ejt("DELETE FROM transferencias_det WHERE id_trans=$StrId");
		BIT_DET_ADD($_BIT_ID,$qqq);
		//
		$LStrMsg="Los datos han sido modificados";
		$StrGT="";
		$StrCmd="G";
	}
}
//---------------------------------
if($StrCmd=="G"){	
	$s="SELECT temc_id_producto,temc_cant,dscto FROM temp_compras WHERE cod_temc='$StrHWND_T'";
	$result = grs($s);	
	while ($row = $result->fetch_row()){
		$ID_PRO=$row[0];
		$CAN_PRO=$row[1];
		$UNI_PRO=$row[2];
		$Stock=gn("sp_cantidad","stocks_presentaciones","sp_id_almacen=$StrAlm_ AND sp_id_producto=$ID_PRO AND sp_presentacion=$UNI_PRO");
		$S_PDes=gs("pro_des","productos","id_producto=$ID_PRO");

		if($Stock<=0){				
			$LStrMsg=$S_PDes." Stock no disponible (0) ";
			$LStrFoc="document.getElementById('TxtCod').focus();";						
			$StrGT="S";	
			break;
		}
		//
		if($Stock < $CAN_PRO){
			$LStrMsg=$S_PDes." Cantidad superior al Stock Actual";
			$LStrFoc="document.getElementById('TxtCod').focus();";						
			$StrGT="S";	
			break;
		}				
	}	
}
//---------------------------------
//FUNCIONES DE ACTUALIZACION
	if($StrCmd=="G" && strlen($StrGT)==0){
		if(strlen($StrTSer)>0 && strlen($StrTNum)>0){
			$S=vbval($StrTSer);
			$N=vbval($StrTNum);
			if(gn("count(*)","z_vis_transferencias",
				  "TSER_=$S AND TNUM_=$N AND ID_EMP_O=$StrEmp_")>0){
				$LStrMsg="Serie y numero de transferencia ya registrado";
				$LStrFoc="document.getElementById('TxtCod').focus();";						
				$StrGT="S";	
			}
		}
	}
	//+++
	if($StrCmd=="G" && strlen($StrGT)==0){		
		$StrFec_=ToDate($StrFec);
		$GII=GenGI();
		$StrId=GenId("id_trans","transferencias");
		$StrNum=vbr("0000000".$StrId ,5);
		if($_SESSION['SYS_USER_ID']==0){
			$VALIDADO="S";
		}else{
			$VALIDADO="N";
		}
		$s="INSERT INTO transferencias 
			VALUES ($StrId,'$StrFec_',$StrAlm_,
			'$StrNum','$StrAlmD_','N',0,$GII,'$VALIDADO',
			'$StrTSer','$StrTNum',".
			vbval($StrTSer).",".vbval($StrTNum).",".
			"'$StrCom','$StrPSER','$StrPNUM','$StrCar')";
			ejt($s);
		//BIT
		$_BIT_MOD_ID=$StrId;
		$_BIT_SQL_CAB=$s;
		$_BIT_SQL_DET="";
		$_BIT_PHP="transferencias.php";
		$_BIT_ID=BIT_ADD("ALMACEN","TRANSFERENCIAS","GUARDAR",$_BIT_MOD_ID,$_BIT_SQL_CAB,$_BIT_SQL_DET,$_BIT_PHP);
		BIT_DET_ADD($_BIT_ID,$s);
		//BIT	
		$qqq=ejt("DELETE FROM transferencias_det WHERE id_trans=$StrId");
		BIT_DET_ADD($_BIT_ID,$qqq);
		//
		$s="SELECT * FROM temp_compras WHERE cod_temc='$StrHWND_T' 
			ORDER BY temc_index";
		$result = grs($s);			
		$cx=0;			
		while ($row = $result->fetch_row()){
			$cx++;
			$G_ID=$row[1];
			$G_CA=$row[2];
			$G_CU=$row[8];
			$_CONTROL=$G_CA;
			$ss="INSERT INTO transferencias_det ".
			    "VALUES ($StrId,$cx,$G_ID,$G_CA,$G_CU,$_CONTROL)";
			ejt($ss);
			//
			BIT_DET_ADD($_BIT_ID,$ss);
			//
			if($_SESSION['SYS_USER_ID']==0){
				STOCK_SUB($StrAlm_,$G_ID,$G_CA*$G_CU);
				STOCK_ADD($StrAlmD_,$G_ID,$G_CA*$G_CU);
				//----------------------------------------
				if(gn("count(*)","stocks_presentaciones",
					  "sp_id_almacen=$StrAlm_ AND sp_id_producto=$G_ID AND sp_presentacion=$G_CU")==0){
					$qqq=ejt("INSERT INTO stocks_presentaciones 
						 VALUES ($StrAlm_,$G_ID,$G_CU,$G_CA* -1,$PRO_NO)");
					//
					BIT_DET_ADD($_BIT_ID,$qqq);
					//
				}else{
					$qqq=ejt("UPDATE stocks_presentaciones 
						 SET sp_cantidad=sp_cantidad-$G_CA 
						 WHERE sp_id_almacen=$StrAlm_ AND 
						 sp_id_producto=$G_ID AND sp_presentacion=$G_CU");
					//
					BIT_DET_ADD($_BIT_ID,$qqq);
					//
				}
				//----------------------------------------
				if(gn("count(*)","stocks_presentaciones",
					  "sp_id_almacen=$StrAlmD_ AND 
					   sp_id_producto=$G_ID AND sp_presentacion=$G_CU")==0){
					$qqq=ejt("INSERT INTO stocks_presentaciones 
						 VALUES ($StrAlmD_,$G_ID,$G_CU,$G_CA,$PRO_NO)");
					//
					BIT_DET_ADD($_BIT_ID,$qqq);
					//
				}else{
					$qqq=ejt("UPDATE stocks_presentaciones 
						SET sp_cantidad=sp_cantidad+$G_CA 
						WHERE sp_id_almacen=$StrAlmD_ AND 
						sp_id_producto=$G_ID AND sp_presentacion=$G_CU");
					//
					BIT_DET_ADD($_BIT_ID,$qqq);
					//
				}
				//----------------------------------------
				T_P_ACT($G_ID);
			}
		}
		ejt("DELETE FROM temp_compras WHERE cod_temc='$StrHWND_T'");
		//
		if(strlen($LStrMsg)==0){
			$LStrMsg="Los datos han sido registrado. El numero generado es : $StrNum";
		}
		$LStrFoc="document.getElementById('TxtFec').focus();";						
		$StrGT="S";
		//
		$LIMPIAR=1;
	}
	if($StrCmd=="VAL"){		
		$_VAL=gs("val","transferencias","id_trans=$StrId");
		if($_VAL=="S"){
			$LStrMsg="La presente transferencia ya ha sido validada por lo tanto no se puede volver a validar";
			$LStrFoc="document.getElementById('TxtFec').focus();";						
			$StrGT="S";
		}
		if(strlen($StrGT)==0){
			//BIT
			$_BIT_MOD_ID=$StrId;
			$_BIT_SQL_CAB="SQL";//$s;
			$_BIT_SQL_DET="";
			$_BIT_PHP="transferencias.php";
			$_BIT_ID=BIT_ADD("ALMACEN","TRANSFERENCIA","VALIDAR",$_BIT_MOD_ID,$_BIT_SQL_CAB,$_BIT_SQL_DET,$_BIT_PHP);
			BIT_DET_ADD($_BIT_ID,"");
			//BIT	
			$StrAlm_=gn("id_alm_o","transferencias","id_trans=$StrId");
			$StrAlmD_=gn("id_alm_d","transferencias","id_trans=$StrId");
			$s="SELECT * FROM temp_compras WHERE cod_temc='$StrHWND_T' 
				ORDER BY temc_index";
			$result = grs($s);			
			$cx=0;			
			while ($row = $result->fetch_row()){
				$cx++;
				$G_ID=$row[1];
				$G_CA=$row[2];
				$G_CU=$row[8];	
				STOCK_SUB($StrAlm_,$G_ID,$G_CA*$G_CU);
				STOCK_ADD($StrAlmD_,$G_ID,$G_CA*$G_CU);
				//----------------------------------------
				if(gn("count(*)",
					 "stocks_presentaciones",
					 "sp_id_almacen=$StrAlm_ AND 
					 sp_id_producto=$G_ID AND sp_presentacion=$G_CU")==0){
					$qqq=ejt("INSERT INTO stocks_presentaciones 
						VALUES ($StrAlm_,$G_ID,$G_CU,$G_CA* -1,$PRO_NO)");
					//
					BIT_DET_ADD($_BIT_ID,$qqq);
					//
				}else{
					$qqq=ejt("UPDATE stocks_presentaciones 
						SET sp_cantidad=sp_cantidad-$G_CA 
					    WHERE sp_id_almacen=$StrAlm_ AND 
						sp_id_producto=$G_ID AND sp_presentacion=$G_CU");
					//
					BIT_DET_ADD($_BIT_ID,$qqq);
					//
				}
				//----------------------------------------
				if(gn("count(*)","stocks_presentaciones",
					  "sp_id_almacen=$StrAlmD_ AND sp_id_producto=$G_ID AND sp_presentacion=$G_CU")==0){
					$qqq=ejt("INSERT INTO stocks_presentaciones 
						 VALUES ($StrAlmD_,$G_ID,$G_CU,$G_CA,$PRO_NO)");
					//
					BIT_DET_ADD($_BIT_ID,$qqq);
					//
				}else{
					$qqq=ejt("UPDATE stocks_presentaciones 
						 SET sp_cantidad=sp_cantidad + $G_CA 
						 WHERE sp_id_almacen=$StrAlmD_ AND 
						 sp_id_producto=$G_ID AND sp_presentacion=$G_CU");
					//
					BIT_DET_ADD($_BIT_ID,$qqq);
					//
				}
				//----------------------------------------
				T_P_ACT($G_ID);
			}
			$LStrMsg="La presente transferencia ha sido validada correctamente";
			$LStrFoc="document.getElementById('TxtFec').focus();";						
			$StrGT="S";
			$qqq=ejt("UPDATE transferencias SET val='S' WHERE id_trans=$StrId");
			//
			BIT_DET_ADD($_BIT_ID,$qqq);
			//
		}
	}
	//
	if($StrCmd=="E"){
		$_VAL=gs("val","transferencias","id_trans=$StrId");
		if($_VAL=="S"){
			$StrAlm_=gn("id_alm_o","transferencias","id_trans=$StrId");
			$StrAlmD_=gn("id_alm_d","transferencias","id_trans=$StrId");			
			$s="SELECT id_producto,cant,can_uni FROM transferencias_det WHERE id_trans=$StrId";
			$result = grs($s);			
			while ($row = $result->fetch_row()){
				$E_ID=$row[0];
				$E_CA=$row[1];
				$E_PR=$row[2];
				STOCK_SUB($StrAlmD_,$E_ID,$E_CA*$E_PR);
				ZTOCK_SUB($StrAlmD_,$E_ID,$E_CA,$E_PR);		
				
				STOCK_ADD($StrAlm_,$E_ID,$E_CA*$E_PR);
				ZTOCK_ADD($StrAlm_,$E_ID,$E_CA,$E_PR);	
				
				T_P_ACT($G_ID);	
			}
			$qqq=ejt("DELETE FROM transferencias WHERE id_trans=$StrId");
			//BIT
			$_BIT_MOD_ID=$StrId;
			$_BIT_SQL_CAB=$qqq;
			$_BIT_SQL_DET="";
			$_BIT_PHP="transferencias.php";
			$_BIT_ID=BIT_ADD("ALMACEN","TRANSFERENCIAS","ELIMINAR",$_BIT_MOD_ID,$_BIT_SQL_CAB,$_BIT_SQL_DET,$_BIT_PHP);
			BIT_DET_ADD($_BIT_ID,$qqq);
			//BIT	
			$qqq=ejt("DELETE FROM transferencias_det WHERE id_trans=$StrId");
			//BIT
			BIT_DET_ADD($_BIT_ID,$qqq);
			//BIT	
			ejt("DELETE FROM temp_compras WHERE cod_temc='$StrHWND_T'");
			//
			$LStrMsg="Los datos han sido eliminados";
			$LStrFoc="document.getElementById('TxtFec').focus();";						
			$StrGT="S";
			//
			$LIMPIAR=1;
			//------------------------------------------------------------------------------
		}
		if(strlen($StrGT)==0 && $_VAL=="N"){
			$qqq=ejt("DELETE FROM transferencias WHERE id_trans=$StrId");
			//BIT
			$_BIT_MOD_ID=$StrId;
			$_BIT_SQL_CAB=$qqq;
			$_BIT_SQL_DET="";
			$_BIT_PHP="transferencias.php";
			$_BIT_ID=BIT_ADD("ALMACEN","TRANSFERENCIAS","ELIMINAR",$_BIT_MOD_ID,$_BIT_SQL_CAB,$_BIT_SQL_DET,$_BIT_PHP);
			BIT_DET_ADD($_BIT_ID,$qqq);
			//BIT	
			$qqq=ejt("DELETE FROM transferencias_det WHERE id_trans=$StrId");
			BIT_DET_ADD($_BIT_ID,$qqq);
			//
			ejt("DELETE FROM temp_compras WHERE cod_temc='$StrHWND_T'");
			//
			$LStrMsg="Los datos han sido eliminados";
			$LStrFoc="document.getElementById('TxtFec').focus();";						
			$StrGT="S";
			//
			$LIMPIAR=1;
		}
	}	
	
	if($StrCmd=="A"){
		$_VAL=gs("val","transferencias","id_trans=$StrId");
		if($_VAL=="S"){
			$StrAlm_=gn("id_alm_o","transferencias","id_trans=$StrId");
			$StrAlmD_=gn("id_alm_d","transferencias","id_trans=$StrId");			
			$s="SELECT id_producto,cant,can_uni FROM transferencias_det WHERE id_trans=$StrId";
			$result = grs($s);			
			while ($row = $result->fetch_row()){
				$E_ID=$row[0];
				$E_CA=$row[1];
				$E_PR=$row[2];
				STOCK_SUB($StrAlmD_,$E_ID,$E_CA*$E_PR);
				ZTOCK_SUB($StrAlmD_,$E_ID,$E_CA,$E_PR);		
				
				STOCK_ADD($StrAlm_,$E_ID,$E_CA*$E_PR);
				ZTOCK_ADD($StrAlm_,$E_ID,$E_CA,$E_PR);	
				
				T_P_ACT($G_ID);	
			}
			$qqq=ejt("UPDATE transferencias SET anu='S' WHERE id_trans=$StrId");
			//BIT
			$_BIT_MOD_ID=$StrId;
			$_BIT_SQL_CAB=$qqq;
			$_BIT_SQL_DET="";
			$_BIT_PHP="transferencias.php";
			$_BIT_ID=BIT_ADD("ALMACEN","TRANSFERENCIAS","ANULAR",$_BIT_MOD_ID,$_BIT_SQL_CAB,$_BIT_SQL_DET,$_BIT_PHP);
			BIT_DET_ADD($_BIT_ID,$qqq);
			//BIT			
			ejt("DELETE FROM temp_compras WHERE cod_temc='$StrHWND_T'");
			//
			$LStrMsg="La transferencia ha sido anulada";
			$LStrFoc="document.getElementById('TxtFec').focus();";						
			$StrGT="S";
			//
			$LIMPIAR=1;
			//------------------------------------------------------------------------------
		}
		if(strlen($StrGT)==0 && $_VAL=="N"){
			$qqq=ejt("UPDATE transferencias SET anu='S' WHERE id_trans=$StrId");
			//BIT
			$_BIT_MOD_ID=$StrId;
			$_BIT_SQL_CAB=$qqq;
			$_BIT_SQL_DET="";
			$_BIT_PHP="transferencias.php";
			$_BIT_ID=BIT_ADD("ALMACEN","TRANSFERENCIAS","ANULAR*",$_BIT_MOD_ID,$_BIT_SQL_CAB,$_BIT_SQL_DET,$_BIT_PHP);
			BIT_DET_ADD($_BIT_ID,$qqq);
			//BIT		
			ejt("DELETE FROM temp_compras WHERE cod_temc='$StrHWND_T'");
			$LStrMsg="La transferencia ha sido anulada";
			$LStrFoc="document.getElementById('TxtFec').focus();";						
			$StrGT="S";
			$LIMPIAR=1;
		}
	}
	//
	if($LIMPIAR==1){
		$StrId="";
		$StrFec=date("d-m-y");
		$StrEmpD="";
		$StrAlmD="";
		$StrNum="";
		$StrCod="";
		$StrDes="";
		$StrMARCA="";
		$StrCant="";
		$StrCUni="";
		$StrCom="";
		$StrPSER="";
		$StrPNUM="";
		$StrTSer="";
		$StrTNum="";
		$StrCar="";
	}
	//
	if($StrGT=="S"){
		if(strlen($LStrMsg)>0){
			if(strlen($LStrFoc)==0){
				$LStrMsg="onload=\"alert('$LStrMsg');\"";
			}else{
				$LStrMsg="onload=\"alert('$LStrMsg');$LStrFoc;\"";
			}
		}else{
			if(strlen($LStrFoc)>0){
				$LStrMsg="onload=\"$LStrFoc;\"";
			}
		}
	}else{
		$LStrFoc="document.getElementById('TxtFec').focus();";
		$LStrMsg="onload=\"$LStrFoc;\"";
	}
	
?>