Create tables acq.fiscal_calendar and acq.fiscal_year.
authorscottmk <scottmk@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Thu, 27 Aug 2009 12:51:09 +0000 (12:51 +0000)
committerscottmk <scottmk@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Thu, 27 Aug 2009 12:51:09 +0000 (12:51 +0000)
commit359dfd3e790dce2ab8e7aa57110c9c8d3e8fee7a
treec79566c0b8c5ac8cf282538753f9b18664777d1e
parent84cbfcfeff418724d9439e9bbf1a618dd1957207
Create tables acq.fiscal_calendar and acq.fiscal_year.

Create function acq.find_bad_fy to perform sanity checks
on acq.fiscal_year.

To add to an existing database:

CREATE TABLE acq.fiscal_calendar (
id              SERIAL         PRIMARY KEY,
name            TEXT           NOT NULL
);

CREATE TABLE acq.fiscal_year (
id              SERIAL         PRIMARY KEY,
calendar        INT            NOT NULL
                               REFERENCES acq.fiscal_calendar
                               ON DELETE CASCADE
                               DEFERRABLE INITIALLY DEFERRED,
year            INT            NOT NULL,
year_begin      TIMESTAMPTZ    NOT NULL,
year_end        TIMESTAMPTZ    NOT NULL,
CONSTRAINT acq_fy_logical_key  UNIQUE ( calendar, year ),
    CONSTRAINT acq_fy_physical_key UNIQUE ( calendar, year_begin )
);

CREATE OR REPLACE FUNCTION acq.find_bad_fy()
/*
Examine the acq.fiscal_year table, comparing successive years.
Report any inconsistencies, i.e. years that overlap or have
gaps between them.
*/
RETURNS SETOF RECORD AS $$
DECLARE
first_row  BOOLEAN;
curr_year  RECORD;
prev_year  RECORD;
return_rec RECORD;
BEGIN
first_row := true;
FOR curr_year in
SELECT
id,
calendar,
year,
year_begin,
year_end
FROM
acq.fiscal_year
ORDER BY
calendar,
year_begin
LOOP
--
IF first_row THEN
first_row := FALSE;
ELSIF curr_year.calendar    = prev_year.calendar THEN
IF curr_year.year_begin > prev_year.year_end THEN
-- This ugly kludge works around the fact that older
-- versions of PostgreSQL don't support RETURN QUERY SELECT
FOR return_rec IN SELECT
prev_year.id,
prev_year.year,
'Gap between fiscal years'::TEXT
LOOP
RETURN NEXT return_rec;
END LOOP;
ELSIF curr_year.year_begin < prev_year.year_end THEN
FOR return_rec IN SELECT
prev_year.id,
prev_year.year,
'Overlapping fiscal years'::TEXT
LOOP
RETURN NEXT return_rec;
END LOOP;
ELSIF curr_year.year < prev_year.year THEN
FOR return_rec IN SELECT
prev_year.id,
prev_year.year,
'Fiscal years out of order'::TEXT
LOOP
RETURN NEXT return_rec;
END LOOP;
END IF;
END IF;
--
prev_year := curr_year;
END LOOP;
--
RETURN;
END;
$$ LANGUAGE plpgsql;

git-svn-id: svn://svn.open-ils.org/ILS/trunk@13931 dcc99617-32d9-48b4-a31d-7c20da2025e4
Open-ILS/examples/fm_IDL.xml
Open-ILS/src/sql/Pg/200.schema.acq.sql