From 22ee06d6d9062de8e65ccb57606431798e1e821a Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Fri, 14 Sep 2018 00:16:19 -0400 Subject: [PATCH] initial gl extraction --- schema/triggers/log_insert.sql | 66 ++++++++++++++++++++++++++-------- 1 file changed, 51 insertions(+), 15 deletions(-) diff --git a/schema/triggers/log_insert.sql b/schema/triggers/log_insert.sql index 797f804..f58288a 100644 --- a/schema/triggers/log_insert.sql +++ b/schema/triggers/log_insert.sql @@ -1,16 +1,52 @@ ---------------------------balances---------------------------------------------------- -CREATE TABLE evt.bal ( - acct TEXT REFERENCES evt.acct(acct) - ,perd daterange - ,obal numeric(12,2) - ,debits numeric(12,2) - ,credits numeric(12,2) - ,cbal numeric(12,2) -); -COMMENT ON COLUMN evt.bal.acct IS 'account'; -COMMENT ON COLUMN evt.bal.perd IS 'period'; -COMMENT ON COLUMN evt.bal.obal IS 'opening balance'; -COMMENT ON COLUMN evt.bal.debits IS 'total debits'; -COMMENT ON COLUMN evt.bal.credits IS 'total credits'; -COMMENT ON COLUMN evt.bal.cbal IS 'closing balance'; \ No newline at end of file +WITH + NEW as ( + SELECT $${ + "gl": { + "lines": [ + { + "amount": 2.19, + "account": "h.food" + }, + { + "amount": -2.19, + "account": "h.dcard" + } + ], + "jpath": [ + [ + "{item,0}", + "{header}" + ], + [ + "{item,0}", + "{header}" + ] + ] + }, + "item": [ + { + "item": "green olives", + "amount": 2.19, + "reason": "food", + "account": "h.food" + } + ], + "header": { + "entity": "home", + "module": "MHI", + "offset": "h.dcard", + "transaction": "purchase" + } +}$$::jsonb bpr + ) +SELECT + * +FROM + NEW + --gl array hold each gl line + LEFT JOIN LATERAL JSONB_ARRAY_ELEMENTS(NEW.bpr->'gl') WITH ORDINALITY gl(i, rn) ON TRUE + --eaxpand the array of gl lines + LEFT JOIN LATERAL JSONB_ARRAY_ELEMENTS(gl.i->'lines') WITH ORDINALITY a(i, rn) ON TRUE + --for each + LEFT JOIN LATERAL JSONB_ARRAY_ELEMENTS(gl.i->'jpath') WITH ORDINALITY p(i, rn) ON TRUE \ No newline at end of file