SET client_min_messages = notice;
DO $$
DECLARE
_msj TEXT;
__id_factura INTEGER;
__fecha_emision DATE;
__fecha_venci DATE;
__identificador DATE;
__count INTEGER;
__id_factura_salvar INTEGER;
BEGIN
SELECT id_factura, fec_emision FROM configuracion.facturacion WHERE fec_venci IS NULL INTO __id_factura, __fecha_emision;
__fecha_venci := __fecha_emision + CAST('5 day' AS INTERVAL);
RAISE NOTICE ' __fecha_venci % ', __fecha_venci;
IF __id_factura IS NOT NULL THEN
UPDATE configuracion.facturacion SET fec_venci = __fecha_venci, estado = 'GRATIS' WHERE id_factura = __id_factura;
SELECT fec_emision , COUNT(*) AS c
INTO __identificador, __count
FROM configuracion.facturacion
GROUP BY fec_emision
HAVING COUNT(*) > 1;
raise notice '__identificador %', __identificador;
raise notice '__count %', __count;
select id_factura INTO __id_factura_salvar from configuracion.facturacion WHERE fec_emision = __identificador ORDER by id_factura;
RAISE NOTICE '__id_factura_salvar % ', __id_factura_salvar;
DELETE FROM configuracion.facturacion WHERE id_factura > __id_factura_salvar;
END IF;
EXCEPTION
WHEN OTHERS THEN
GET STACKED DIAGNOSTICS _msj = PG_EXCEPTION_CONTEXT;
RAISE NOTICE 'SE JODIO: % %', SQLERRM, SQLSTATE;
END
$$
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.