<?php
	//-------------------------
	$LIMPIAR=0;
	$StrId=V($_POST['TxtId']);
	$StrCmd=V($_POST['TxtAc']);
	$StrFec=V($_POST['TxtFec']);
	$StrEmp=V($_POST['CboEmp']);
	$StrAlm=V($_POST['CboAlm']);
	$StrTot=V($_POST['TxtTot']);
	$StrObs=V($_POST['TxtObs']);
	//
	if(strlen($StrEmp)==0)$StrEmp=GE("N");
	if(strlen($StrAlm)==0)$StrAlm=GA($StrEmp,"N");	
	//	
	//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="";
		}}}	
	if(strlen($StrCmd)==0){
		$StrFec=date("d-m-y");
	}
	//FUNCION PARA ACTIVAR UN REGISTRO
	if(strlen($StrId)>0 && $StrCmd=="AR"){
		$Id=$StrId;
		$StrFec=gs("DATE_FORMAT(FEC,'%d-%m-%y')","vcaja","ID=$Id");		
		$StrEmp=gs("EMPC","vcaja","ID=$Id");
		$StrAlm=gs("ALMC","vcaja","ID=$Id");		
		$StrTot=gn("TOT","vcaja","ID=$Id");		
		$StrObs=gs("OBS","vcaja","ID=$Id");		
		$LStrFoc="document.getElementById('TxtFec').focus();";		
		$StrGT="S";
	}	
	if($StrCmd=="G" || $StrCmd=="M"){
		$StrEmp_=gn("id_emp","empresas","emp_nomc='$StrEmp'");
		$StrAlm_=gn("id_almacen","almacenes",
					"alm_obs='$StrAlm' AND alm_id_empresa=$StrEmp_");
		$StrFec_=ToDate($StrFec);
	}
	//FUNCIONES DE ACTUALIZACION+++
	if($StrCmd=="G"){
		$Id=GenId("id_caja","caja_chica");
		$s="INSERT INTO caja_chica VALUES ($Id,'$StrFec_',$StrAlm_,$StrTot,'$StrObs')";
		ejt($s);
		//BIT
		$_BIT_MOD_ID=$Id;
		$_BIT_SQL_CAB=$s;
		$_BIT_SQL_DET="";
		$_BIT_PHP="caja_chica.php";
		$_BIT_ID=BIT_ADD("MANTENIMIENTO","CAJA CHICA","GUARDAR",$_BIT_MOD_ID,$_BIT_SQL_CAB,$_BIT_SQL_DET,$_BIT_PHP);
		BIT_DET_ADD($_BIT_ID,$s);
		//BIT	
		$LStrMsg="Datos registrados";
		$LStrFoc="document.getElementById('TxtFec').focus();";		
		$LIMPIAR=1;		
	}
	if($StrCmd=="M"){
		if(strlen($StrGT)==0){
			$s="UPDATE caja_chica SET fecha='$StrFec_',
				id_almacen=$StrAlm_,total=$StrTot,obs='$StrObs' 
				WHERE id_caja=$StrId";	
			ejt($s);
			//BIT
			$_BIT_MOD_ID=$StrId;
			$_BIT_SQL_CAB=$s;
			$_BIT_SQL_DET="";
			$_BIT_PHP="caja_chica.php";
			$_BIT_ID=BIT_ADD("MANTENIMIENTO","CAJA CHICA","MODIFICAR",$_BIT_MOD_ID,$_BIT_SQL_CAB,$_BIT_SQL_DET,$_BIT_PHP);
			BIT_DET_ADD($_BIT_ID,$s);
			//BIT	
			$LStrMsg="Datos actualizados";
			$LStrFoc="document.getElementById('TxtFec').focus();";
			$LIMPIAR=1;
		}
	}
	if($StrCmd=="E"){
		if(strlen($StrGT)==0){
			$s="DELETE FROM caja_chica WHERE id_caja=$StrId";	
			ejt($s);
			//BIT
			$_BIT_MOD_ID=$StrId;
			$_BIT_SQL_CAB=$s;
			$_BIT_SQL_DET="";
			$_BIT_PHP="caja_chica.php";
			$_BIT_ID=BIT_ADD("MANTENIMIENTO","CAJA CHICA","ELIMINAR",$_BIT_MOD_ID,$_BIT_SQL_CAB,$_BIT_SQL_DET,$_BIT_PHP);
			BIT_DET_ADD($_BIT_ID,$s);
			//BIT	
			$LStrMsg="Registro Eliminado";
			$LStrFoc="document.getElementById('TxtFec').focus();";
			$LIMPIAR=1;
		}		
	}
	if($LIMPIAR==1){
		$StrId="";		
		$StrFec=date("d-m-y");
		$StrTot="";
		$StrObs="";
		$StrGT="S";
	}
	//////////////////////////////////
	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;\"";
	}

?>