Created
December 2, 2015 21:02
-
-
Save yogonza524/cf2f3a33f0791c30a6db to your computer and use it in GitHub Desktop.
Función para obtener la fecha de vencimiento siguiente
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE FUNCTION get_fecha_vencimiento() | |
RETURNS DATE AS $$ | |
DECLARE | |
dia_ven integer; | |
today integer; | |
result date; | |
BEGIN | |
SELECT c.dia_vencimiento INTO dia_ven FROM config c LIMIT 1; | |
IF (dia_ven IS NULL) THEN | |
RAISE EXCEPTION 'Error grave: no existe dia de vencimiento en la tabla de configuración'; | |
END IF; | |
today = date_part('day',now()::date); | |
--Todavia no llegó el dia de emision | |
result = dia_ven || '-' || date_part('month',now()) || '-' || date_part('year',now()); | |
IF (today > dia_ven) THEN | |
result = result + '1 month'::interval; | |
END IF; | |
RETURN result; | |
END; | |
$$ | |
LANGUAGE PLPGSQL |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Esta función se encarga de generar la siguiente fecha a partir del dia de vencimiento cargado en la tabla "config".