jl/schema/triggers/gl_insert.sql

44 lines
1.2 KiB
MySQL
Raw Normal View History

2018-09-20 23:38:26 -04:00
---------------------------handle new gl lines----------------------------------------
CREATE OR REPLACE FUNCTION evt.gl_insert() RETURNS trigger
LANGUAGE plpgsql
AS
$func$
2018-09-21 00:01:11 -04:00
BEGIN
WITH
agg AS (
SELECT
acct
,fspr
,coalesce(sum(amount) FILTER (WHERE amount > 0),0) debits
,coalesce(sum(amount) FILTER (WHERE amount < 0),0) credits
FROM
ins
GROUP BY
acct
,fspr
)
INSERT INTO
evt.bal
2018-09-20 23:38:26 -04:00
SELECT
acct
,fspr
2018-09-21 00:01:11 -04:00
,0 obal
,debits
,credits
,debits + credits
2018-09-20 23:38:26 -04:00
FROM
2018-09-21 00:01:11 -04:00
agg
ON CONFLICT ON CONSTRAINT bal_pk DO UPDATE SET
debits = evt.bal.debits + EXCLUDED.debits
,credits = evt.bal.credits + EXCLUDED.credits
,cbal = evt.bal.cbal + EXCLUDED.debits + EXCLUDED.credits;
RETURN NULL;
END;
$func$;
CREATE TRIGGER gl_insert
AFTER INSERT ON evt.gl
REFERENCING NEW TABLE AS ins
FOR EACH STATEMENT
EXECUTE PROCEDURE evt.gl_insert();