Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yogonza524/cf2f3a33f0791c30a6db to your computer and use it in GitHub Desktop.
Save yogonza524/cf2f3a33f0791c30a6db to your computer and use it in GitHub Desktop.
Función para obtener la fecha de vencimiento siguiente
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
@yogonza524
Copy link
Author

Esta función se encarga de generar la siguiente fecha a partir del dia de vencimiento cargado en la tabla "config".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment