4 -- note this does now not drop the timewarp tables..
6 -- The time warp schema is for reporting - to make SG accounts look like HK...
8 -- DROP SCHEMA timewarp;
9 -- SELECT timewarp.yearperiod_fill_all();
10 -- SELECT timewarp.period_fill_all();
12 -- DELETE FROM timewarp.trialbal;
13 -- SELECT timewarp.trialbal_fill_all();
15 CREATE SCHEMA timewarp;
18 DROP VIEW IF EXISTS timewarp.trialbal ;
19 DROP VIEW IF EXISTS timewarp.period;
20 DROP VIEW IF EXISTS timewarp.yearperiod ;
24 DROP VIEW IF EXISTS timewarp.trialbalview ;
25 DROP VIEW IF EXISTS timewarp.periodview;
26 DROP VIEW IF EXISTS timewarp.yearperiodview ;
30 --CREATE OR REPLACE VIEW timewarp.accnt AS SELECT * FROM public.accnt;
32 -- DROP BOTH togetyer!
36 CREATE VIEW timewarp.yearperiodview AS
40 to_char(yearperiod_start , 'YYYY-05-DD')::date as yearperiod_start,
41 to_char(yearperiod_end , 'YYYY-04-DD')::date as yearperiod_end,
43 FROM public.yearperiod;
46 CREATE VIEW timewarp.periodview AS
57 (SELECT yearperiod_id FROM
58 timewarp.yearperiodview
60 period_start >= yearperiod_start AND
61 period_start < yearperiod_end
62 LIMIT 1) as period_yearperiod_id,
64 floor((((period_number + 4) % 12) ) / 3) +1 as period_quarter ,
65 ((period_number + 4) % 12) +1 as period_number
72 -- SELECT * from yearperiod ORDER BY yearperiod_start ASC;
77 -- find the qty available..
78 CREATE OR REPLACE FUNCTION timewarp.trailbal_getvalue(integer, date, text)
79 RETURNS numeric(20, 2)
83 i_accnt_id ALIAS FOR $1;
85 i_b_or_e ALIAS FOR $3;
98 yearperiod_start <= i_date
100 yearperiod_end >= i_date
102 yearperiod_start DESC
106 --IF i_date = i_yearstart AND
114 SUM(trialbal_credits - trialbal_debits)
122 trialbal_period_id = period_id
124 trialbal_accnt_id = i_accnt_id
129 period_start < i_date
137 period_start >= i_yearstart;
142 RETURN COALESCE(v_ret,0.00);
146 LANGUAGE plpgsql VOLATILE
149 ALTER FUNCTION timewarp.trailbal_getvalue(integer, date, text)
156 DROP VIEW timewarp.trialbalview ;
157 CREATE VIEW timewarp.trialbalview AS
165 -- begining and ending -- are the same for ASSETS and LIABILITIES
168 accnt_type IN ('A','L','Q')
173 -- if it's not a asset liability
174 -- then it 0's out at the start of each financial year.
175 timewarp.trailbal_getvalue( accnt_id, period_start , 'S')
177 as trialbal_beginning,
182 accnt_type IN ('A','L','Q')
187 timewarp.trailbal_getvalue( accnt_id, period_end, 'E')
198 -- FIXME -- this might need some more thought...
206 trialbal_accnt_id = accnt_id
210 trialbal_period_id = period_id ;
213 -- select period_start, trialbal_beginning , trialbal_ending, trialbal_credits, trialbal_debits from timewarp.trialbalview left join timewarp.period on trialbal_period_id = period_id where trialbal_accnt_id = 121 order by period_start ASC;
215 -- -- slase id = 135 - type ='R'
217 -- trialbal_beginning,trialbal_ending, period_start
218 -- from timewarp.trialbal LEFT JOIN period ON trialbal_period_id = period_id where trialbal_accnt_id = 135 order by period_start ASC;
223 -- trialbal_beginning,trialbal_ending, period_start, trialbal_yearend
224 -- from trialbal LEFT JOIN period ON trialbal_period_id = period_id where trialbal_accnt_id = 208 order by period_start ASC;
228 -- DROP TABLE IF EXISTS timewarp.period;
229 CREATE TABLE timewarp.period
231 period_id integer NOT NULL,
234 period_closed boolean,
235 period_freeze boolean,
236 period_initial boolean DEFAULT false,
238 period_yearperiod_id integer,
239 period_quarter integer,
240 period_number integer NOT NULL
246 ALTER TABLE timewarp.period
248 GRANT ALL ON TABLE timewarp.period TO admin;
249 GRANT ALL ON TABLE timewarp.period TO xtrole;
250 COMMENT ON TABLE timewarp.period
251 IS 'Store financial reports Accounting Periods information.';
254 -- DROP TABLE IF EXISTS timewarp.yearperiod;
255 CREATE TABLE timewarp.yearperiod
257 yearperiod_id integer NOT NULL,
258 yearperiod_start date NOT NULL,
259 yearperiod_end date NOT NULL,
260 yearperiod_closed boolean NOT NULL DEFAULT false
265 ALTER TABLE timewarp.yearperiod
267 GRANT ALL ON TABLE timewarp.yearperiod TO admin;
268 GRANT ALL ON TABLE timewarp.yearperiod TO xtrole;
269 COMMENT ON TABLE timewarp.yearperiod
270 IS 'Store financial reports Accounting Year Periods information.';
272 -- DROP TABLE IF EXISTS timewarp.trialbal;
273 CREATE TABLE timewarp.trialbal
275 trialbal_id integer NOT NULL,
276 trialbal_period_id integer,
277 trialbal_accnt_id integer,
278 trialbal_beginning numeric(20,2),
279 trialbal_ending numeric(20,2),
280 trialbal_credits numeric(20,2),
281 trialbal_debits numeric(20,2),
282 trialbal_dirty boolean,
283 trialbal_yearend numeric(20,2) NOT NULL DEFAULT 0.00
290 ALTER TABLE timewarp.trialbal
292 GRANT ALL ON TABLE timewarp.trialbal TO admin;
293 GRANT ALL ON TABLE timewarp.trialbal TO xtrole;
294 COMMENT ON TABLE timewarp.trialbal
295 IS 'Store financial reports Trial Balance information.';
298 --SET search_path TO timewarp,public;
299 --SELECT * FROM public.financialReport(14 , ARRAY[ 112] , 'Y' , true, NULL )
302 GRANT ALL ON SCHEMA timewarp TO admin;
303 GRANT ALL ON SCHEMA timewarp TO xtrole;
304 GRANT ALL ON timewarp.trialbalview TO admin;
305 GRANT ALL ON timewarp.trialbalview TO xtrole;
306 GRANT ALL ON timewarp.periodview TO admin;
307 GRANT ALL ON timewarp.periodview TO xtrole;
308 GRANT ALL ON timewarp.yearperiodview TO admin;
309 GRANT ALL ON timewarp.yearperiodview TO xtrole;