/***********************************************************************************************/
/*APIS generada para mantener la tabla historial_haber */
/*Fecha de creacion en general 23 de Noviembre de 2015 */
/***********************************************************************************************/



CREATE function historial_haber_agregar (integer,integer,integer,integer,numeric,numeric) RETURNS boolean AS '
-- ***********************************************************************************
-- * Funcion plpgsql: permite agregar un registro en la tabla historial_haber
-- * Creada por: Autogenerador de funciones
-- * Fecha: 23 de Noviembre de 2015
-- * Datos de salida:
-- * 		 true si la operacion es correcta
-- * 		 false si ocurre algun error
-- ***********************************************************************************
DECLARE
sql_id_emp ALIAS FOR $1;
sql_id_per ALIAS FOR $2;
sql_ano_hha ALIAS FOR $3;
sql_mes_hha ALIAS FOR $4;
sql_tributable_hha ALIAS FOR $5;
sql_haber_hha ALIAS FOR $6;
sql_registro	record;
sql_aprobacion	boolean;
BEGIN
	INSERT INTO historial_haber (
		id_emp,
		id_per,
		ano_hha,
		mes_hha,
		tributable_hha,
		haber_hha
	) VALUES (
		sql_id_emp,
		sql_id_per,
		sql_ano_hha,
		sql_mes_hha,
		sql_tributable_hha,
		sql_haber_hha
	);
	sql_aprobacion=true;
	return sql_aprobacion;
END;'LANGUAGE plpgsql;




CREATE function historial_haber_modificar (integer,integer,integer,integer,numeric,numeric) RETURNS boolean AS '
-- ***********************************************************************************
-- * Funcion plpgsql: permite modificar un registro en la tabla historial_haber
-- * Creada por: Autogenerador de funciones
-- * Fecha: 23 de Noviembre de 2015
-- * Datos de salida:
-- * 		 true si la operacion es correcta
-- * 		 false si ocurre algun error
-- ***********************************************************************************
DECLARE
sql_id_emp ALIAS FOR $1;
sql_id_per ALIAS FOR $2;
sql_ano_hha ALIAS FOR $3;
sql_mes_hha ALIAS FOR $4;
sql_tributable_hha ALIAS FOR $5;
sql_haber_hha ALIAS FOR $6;
sql_registro	record;
sql_aprobacion	boolean;
BEGIN
	UPDATE sql_registro historial_haber SET 
		id_emp=sql_id_emp,
		id_per=sql_id_per,
		ano_hha=sql_ano_hha,
		mes_hha=sql_mes_hha,
		tributable_hha=sql_tributable_hha,
		haber_hha=sql_haber_hha
	WHERE
		id_emp=sql_id_emp AND 
		id_per=sql_id_per AND 
		ano_hha=sql_ano_hha AND 
		mes_hha=sql_mes_hha
	;
	sql_aprobacion=true;
	return sql_aprobacion;
END;'LANGUAGE plpgsql;




CREATE function historial_haber_existe (integer,integer,integer,integer) RETURNS boolean AS '
-- ***********************************************************************************
-- * Funcion plpgsql: permite verificar si existe el registro señalado en la tabla historial_haber
-- * Creada por: Autogenerador de funciones
-- * Fecha: 23 de Noviembre de 2015
-- * Datos de salida:
-- * 		 true si existe
-- * 		 false si no existe
-- ***********************************************************************************
DECLARE
sql_id_emp ALIAS FOR $1;
sql_id_per ALIAS FOR $2;
sql_ano_hha ALIAS FOR $3;
sql_mes_hha ALIAS FOR $4;
sql_registro	record;
sql_aprobacion	boolean;
BEGIN
	SELECT sql_registro * FROM historial_haber WHERE 
		id_emp=sql_id_emp AND 
		id_per=sql_id_per AND 
		ano_hha=sql_ano_hha AND 
		mes_hha=sql_mes_hha
	;
	IF FOUND THEN
		sql_aprobacion=true;
		return sql_aprobacion;
	ELSE
		sql_aprobacion=false;
		return sql_aprobacion;
	END IF;
END;'LANGUAGE plpgsql;




CREATE function historial_haber_eliminar (integer,integer,integer,integer) RETURNS boolean AS '
-- ***********************************************************************************
-- * Funcion plpgsql: permite eliminar un registro en la tabla historial_haber
-- * Creada por: Autogenerador de funciones
-- * Fecha: 23 de Noviembre de 2015
-- * Datos de salida:
-- * 		 true si la operacion es correcta
-- * 		 false si ocurre algun error
-- ***********************************************************************************
DECLARE
sql_id_emp ALIAS FOR $1;
sql_id_per ALIAS FOR $2;
sql_ano_hha ALIAS FOR $3;
sql_mes_hha ALIAS FOR $4;
sql_registro	record;
sql_aprobacion	boolean;
BEGIN
	DELETE sql_registro FROM historial_haber
	WHERE
		id_emp=sql_id_emp AND 
		id_per=sql_id_per AND 
		ano_hha=sql_ano_hha AND 
		mes_hha=sql_mes_hha
	;
	sql_aprobacion=true;
	return sql_aprobacion;
END;'LANGUAGE plpgsql;




CREATE TYPE typo_historial_haber AS (
	id_emp integer,
	id_per integer,
	ano_hha integer,
	mes_hha integer,
	tributable_hha numeric,
	haber_hha numeric
);

CREATE function historial_haber_datos (integer,integer,integer,integer) RETURNS SETOF typo_historial_haber AS '
-- ***********************************************************************************
-- * Funcion plpgsql: permite obtener los datos de un registro de la tabla historial_haber
-- * Creada por: Autogenerador de funciones
-- * Fecha: 23 de Noviembre de 2015
-- * Datos de salida:
-- * 		 
-- * 		 
-- ***********************************************************************************
DECLARE
sql_id_emp ALIAS FOR $1;
sql_id_per ALIAS FOR $2;
sql_ano_hha ALIAS FOR $3;
sql_mes_hha ALIAS FOR $4;
sql_registro	typo_historial_haber%ROWTYPE;
sql_aprobacion	boolean;
BEGIN
	FOR sql_registro IN SELECT id_emp,id_per,ano_hha,mes_hha,tributable_hha,haber_hha FROM 
		historial_haber
	WHERE
		id_emp=sql_id_emp AND 
		id_per=sql_id_per AND 
		ano_hha=sql_ano_hha AND 
		mes_hha=sql_mes_hha
	LOOP

		RETURN NEXT sql_registro;

	END LOOP;

	RETURN;
END;'LANGUAGE plpgsql;

