1 DROP SCHEMA acq CASCADE;
11 CREATE TABLE acq.currency_type (
12 code TEXT PRIMARY KEY,
16 -- Use the ISO 4217 abbreviations for currency codes
17 INSERT INTO acq.currency_type (code, label) VALUES ('USD','US Dollars');
18 INSERT INTO acq.currency_type (code, label) VALUES ('CAN','Canadian Dollars');
19 INSERT INTO acq.currency_type (code, label) VALUES ('EUR','Euros');
21 CREATE TABLE acq.exchange_rate (
22 id SERIAL PRIMARY KEY,
23 from_currency TEXT NOT NULL REFERENCES acq.currency_type (code) DEFERRABLE INITIALLY DEFERRED,
24 to_currency TEXT NOT NULL REFERENCES acq.currency_type (code) DEFERRABLE INITIALLY DEFERRED,
25 ratio NUMERIC NOT NULL,
26 CONSTRAINT exchange_rate_from_to_once UNIQUE (from_currency,to_currency)
29 INSERT INTO acq.exchange_rate (from_currency,to_currency,ratio) VALUES ('USD','CAN',1.2);
30 INSERT INTO acq.exchange_rate (from_currency,to_currency,ratio) VALUES ('USD','EUR',0.5);
32 CREATE TABLE acq.provider (
33 id SERIAL PRIMARY KEY,
35 owner INT NOT NULL REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED,
36 currency_type TEXT NOT NULL REFERENCES acq.currency_type (code) DEFERRABLE INITIALLY DEFERRED,
39 CONSTRAINT provider_name_once_per_owner UNIQUE (name,owner),
40 CONSTRAINT code_once_per_owner UNIQUE (code, owner)
43 CREATE TABLE acq.provider_holding_subfield_map (
44 id SERIAL PRIMARY KEY,
45 provider INT NOT NULL REFERENCES acq.provider (id) DEFERRABLE INITIALLY DEFERRED,
46 name TEXT NOT NULL, -- barcode, price, etc
47 subfield TEXT NOT NULL,
48 CONSTRAINT name_once_per_provider UNIQUE (provider,name)
51 CREATE TABLE acq.provider_address (
52 id SERIAL PRIMARY KEY,
53 valid BOOL NOT NULL DEFAULT TRUE,
55 provider INT NOT NULL REFERENCES acq.provider (id) DEFERRABLE INITIALLY DEFERRED,
56 street1 TEXT NOT NULL,
61 country TEXT NOT NULL,
62 post_code TEXT NOT NULL
65 CREATE TABLE acq.provider_contact (
66 id SERIAL PRIMARY KEY,
67 provider INT NOT NULL REFERENCES acq.provider (id) DEFERRABLE INITIALLY DEFERRED,
69 role TEXT, -- free-form.. e.g. "our sales guy"
74 CREATE TABLE acq.provider_contact_address (
75 id SERIAL PRIMARY KEY,
76 valid BOOL NOT NULL DEFAULT TRUE,
78 contact INT NOT NULL REFERENCES acq.provider_contact (id) DEFERRABLE INITIALLY DEFERRED,
79 street1 TEXT NOT NULL,
84 country TEXT NOT NULL,
85 post_code TEXT NOT NULL
89 CREATE TABLE acq.funding_source (
90 id SERIAL PRIMARY KEY,
92 owner INT NOT NULL REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED,
93 currency_type TEXT NOT NULL REFERENCES acq.currency_type (code) DEFERRABLE INITIALLY DEFERRED,
95 CONSTRAINT funding_source_name_once_per_owner UNIQUE (name,owner)
98 CREATE TABLE acq.funding_source_credit (
99 id SERIAL PRIMARY KEY,
100 funding_source INT NOT NULL REFERENCES acq.funding_source (id) DEFERRABLE INITIALLY DEFERRED,
101 amount NUMERIC NOT NULL,
105 CREATE TABLE acq.fund (
106 id SERIAL PRIMARY KEY,
107 org INT NOT NULL REFERENCES actor.org_unit (id) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
109 year INT NOT NULL DEFAULT EXTRACT( YEAR FROM NOW() ),
110 currency_type TEXT NOT NULL REFERENCES acq.currency_type (code) DEFERRABLE INITIALLY DEFERRED,
112 CONSTRAINT name_once_per_org_year UNIQUE (org,name,year),
113 CONSTRAINT code_once_per_org_year UNIQUE (org, code, year)
116 CREATE TABLE acq.fund_debit (
117 id SERIAL PRIMARY KEY,
118 fund INT NOT NULL REFERENCES acq.fund (id) DEFERRABLE INITIALLY DEFERRED,
119 origin_amount NUMERIC NOT NULL, -- pre-exchange-rate amount
120 origin_currency_type TEXT NOT NULL REFERENCES acq.currency_type (code) DEFERRABLE INITIALLY DEFERRED,
121 amount NUMERIC NOT NULL,
122 encumbrance BOOL NOT NULL DEFAULT TRUE,
123 debit_type TEXT NOT NULL,
124 xfer_destination INT REFERENCES acq.fund (id) DEFERRABLE INITIALLY DEFERRED,
125 create_time TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
128 CREATE TABLE acq.fund_allocation (
129 id SERIAL PRIMARY KEY,
130 funding_source INT NOT NULL REFERENCES acq.funding_source (id) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
131 fund INT NOT NULL REFERENCES acq.fund (id) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
133 percent NUMERIC CHECK (percent IS NULL OR percent BETWEEN 0.0 AND 100.0),
134 allocator INT NOT NULL REFERENCES actor.usr (id) DEFERRABLE INITIALLY DEFERRED,
136 create_time TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
137 CONSTRAINT allocation_amount_or_percent CHECK ((percent IS NULL AND amount IS NOT NULL) OR (percent IS NOT NULL AND amount IS NULL))
139 CREATE INDEX fund_alloc_allocator_idx ON acq.fund_allocation ( allocator );
141 CREATE TABLE acq.picklist (
142 id SERIAL PRIMARY KEY,
143 owner INT NOT NULL REFERENCES actor.usr (id) DEFERRABLE INITIALLY DEFERRED,
144 creator INT NOT NULL REFERENCES actor.usr (id) DEFERRABLE INITIALLY DEFERRED,
145 editor INT NOT NULL REFERENCES actor.usr (id) DEFERRABLE INITIALLY DEFERRED,
146 org_unit INT NOT NULL REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED,
148 create_time TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
149 edit_time TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
150 CONSTRAINT name_once_per_owner UNIQUE (name,owner)
152 CREATE INDEX acq_picklist_owner_idx ON acq.picklist ( owner );
153 CREATE INDEX acq_picklist_creator_idx ON acq.picklist ( creator );
154 CREATE INDEX acq_picklist_editor_idx ON acq.picklist ( editor );
156 CREATE TABLE acq.purchase_order (
157 id SERIAL PRIMARY KEY,
158 owner INT NOT NULL REFERENCES actor.usr (id) DEFERRABLE INITIALLY DEFERRED,
159 creator INT NOT NULL REFERENCES actor.usr (id) DEFERRABLE INITIALLY DEFERRED,
160 editor INT NOT NULL REFERENCES actor.usr (id) DEFERRABLE INITIALLY DEFERRED,
161 ordering_agency INT NOT NULL REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED,
162 create_time TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
163 edit_time TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
164 provider INT NOT NULL REFERENCES acq.provider (id) DEFERRABLE INITIALLY DEFERRED,
165 state TEXT NOT NULL DEFAULT 'new',
166 order_date TIMESTAMP WITH TIME ZONE,
169 CREATE INDEX po_owner_idx ON acq.purchase_order (owner);
170 CREATE INDEX po_provider_idx ON acq.purchase_order (provider);
171 CREATE INDEX po_state_idx ON acq.purchase_order (state);
172 CREATE INDEX po_creator_idx ON acq.purchase_order ( creator );
173 CREATE INDEX po_editor_idx ON acq.purchase_order ( editor );
174 CREATE INDEX acq_po_org_name_order_date_idx ON acq.purchase_order( ordering_agency, name, order_date );
176 -- The name should default to the id, as text. We can't reference a column
177 -- in a DEFAULT clause, so we use a trigger:
179 CREATE OR REPLACE FUNCTION acq.purchase_order_name_default () RETURNS TRIGGER
182 IF NEW.name IS NULL THEN
183 NEW.name := NEW.id::TEXT;
190 CREATE TRIGGER po_name_default_trg
191 BEFORE INSERT OR UPDATE ON acq.purchase_order
192 FOR EACH ROW EXECUTE PROCEDURE acq.purchase_order_name_default ();
194 -- The order name should be unique for a given ordering agency on a given order date
195 -- (truncated to midnight), but only where the order_date is not NULL. Conceptually
196 -- this rule requires a check constraint with a subquery. However you can't have a
197 -- subquery in a CHECK constraint, so we fake it with a trigger.
199 CREATE OR REPLACE FUNCTION acq.po_org_name_date_unique () RETURNS TRIGGER
205 -- If order_date is not null, then make sure we don't have a collision
206 -- on order_date (truncated to day), org, and name
208 IF NEW.order_date IS NULL THEN
212 -- In the WHERE clause, we compare the order_dates without regard to time of day.
213 -- We use a pair of inequalities instead of comparing truncated dates so that the
214 -- query can do an indexed range scan.
216 SELECT 1 INTO collision
217 FROM acq.purchase_order
219 ordering_agency = NEW.ordering_agency
221 AND order_date >= date_trunc( 'day', NEW.order_date )
222 AND order_date < date_trunc( 'day', NEW.order_date ) + '1 day'::INTERVAL
225 IF collision IS NULL THEN
226 -- okay, no collision
229 -- collision; nip it in the bud
230 RAISE EXCEPTION 'Colliding purchase orders: ordering_agency %, date %, name ''%''',
231 NEW.ordering_agency, NEW.order_date, NEW.name;
236 CREATE TRIGGER po_org_name_date_unique_trg
237 BEFORE INSERT OR UPDATE ON acq.purchase_order
238 FOR EACH ROW EXECUTE PROCEDURE acq.po_org_name_date_unique ();
240 CREATE TABLE acq.po_note (
241 id SERIAL PRIMARY KEY,
242 purchase_order INT NOT NULL REFERENCES acq.purchase_order (id) DEFERRABLE INITIALLY DEFERRED,
243 creator INT NOT NULL REFERENCES actor.usr (id) DEFERRABLE INITIALLY DEFERRED,
244 editor INT NOT NULL REFERENCES actor.usr (id) DEFERRABLE INITIALLY DEFERRED,
245 create_time TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
246 edit_time TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
249 CREATE INDEX po_note_po_idx ON acq.po_note (purchase_order);
250 CREATE INDEX acq_po_note_creator_idx ON acq.po_note ( creator );
251 CREATE INDEX acq_po_note_editor_idx ON acq.po_note ( editor );
253 CREATE TABLE acq.lineitem (
254 id BIGSERIAL PRIMARY KEY,
255 creator INT NOT NULL REFERENCES actor.usr (id) DEFERRABLE INITIALLY DEFERRED,
256 editor INT NOT NULL REFERENCES actor.usr (id) DEFERRABLE INITIALLY DEFERRED,
257 selector INT NOT NULL REFERENCES actor.usr (id) DEFERRABLE INITIALLY DEFERRED,
258 provider INT REFERENCES acq.provider (id) DEFERRABLE INITIALLY DEFERRED,
259 purchase_order INT REFERENCES acq.purchase_order (id) DEFERRABLE INITIALLY DEFERRED,
260 picklist INT REFERENCES acq.picklist (id) DEFERRABLE INITIALLY DEFERRED,
261 expected_recv_time TIMESTAMP WITH TIME ZONE,
262 create_time TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
263 edit_time TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
265 eg_bib_id INT REFERENCES biblio.record_entry (id) DEFERRABLE INITIALLY DEFERRED,
267 item_count INT NOT NULL DEFAULT 0,
268 state TEXT NOT NULL DEFAULT 'new',
269 CONSTRAINT picklist_or_po CHECK (picklist IS NOT NULL OR purchase_order IS NOT NULL)
271 CREATE INDEX li_po_idx ON acq.lineitem (purchase_order);
272 CREATE INDEX li_pl_idx ON acq.lineitem (picklist);
273 CREATE INDEX li_creator_idx ON acq.lineitem ( creator );
274 CREATE INDEX li_editor_idx ON acq.lineitem ( editor );
275 CREATE INDEX li_selector_idx ON acq.lineitem ( selector );
277 CREATE TABLE acq.lineitem_note (
278 id SERIAL PRIMARY KEY,
279 lineitem INT NOT NULL REFERENCES acq.lineitem (id) DEFERRABLE INITIALLY DEFERRED,
280 creator INT NOT NULL REFERENCES actor.usr (id) DEFERRABLE INITIALLY DEFERRED,
281 editor INT NOT NULL REFERENCES actor.usr (id) DEFERRABLE INITIALLY DEFERRED,
282 create_time TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
283 edit_time TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
286 CREATE INDEX li_note_li_idx ON acq.lineitem_note (lineitem);
287 CREATE INDEX li_note_creator_idx ON acq.lineitem_note ( creator );
288 CREATE INDEX li_note_editor_idx ON acq.lineitem_note ( editor );
290 CREATE TABLE acq.lineitem_detail (
291 id BIGSERIAL PRIMARY KEY,
292 lineitem INT NOT NULL REFERENCES acq.lineitem (id) DEFERRABLE INITIALLY DEFERRED,
293 fund INT REFERENCES acq.fund (id) DEFERRABLE INITIALLY DEFERRED,
294 fund_debit INT REFERENCES acq.fund_debit (id) DEFERRABLE INITIALLY DEFERRED,
295 eg_copy_id BIGINT REFERENCES asset.copy (id) ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED,
299 collection_code TEXT,
300 circ_modifier TEXT REFERENCES config.circ_modifier (code) ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED,
301 owning_lib INT REFERENCES actor.org_unit (id) ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED,
302 location INT REFERENCES asset.copy_location (id) ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED,
303 recv_time TIMESTAMP WITH TIME ZONE
306 CREATE INDEX li_detail_li_idx ON acq.lineitem_detail (lineitem);
308 CREATE TABLE acq.lineitem_attr_definition (
309 id BIGSERIAL PRIMARY KEY,
311 description TEXT NOT NULL,
312 remove TEXT NOT NULL DEFAULT '',
313 ident BOOL NOT NULL DEFAULT FALSE
316 CREATE TABLE acq.lineitem_marc_attr_definition (
317 id BIGINT PRIMARY KEY DEFAULT NEXTVAL('acq.lineitem_attr_definition_id_seq'),
319 ) INHERITS (acq.lineitem_attr_definition);
321 CREATE TABLE acq.lineitem_provider_attr_definition (
322 id BIGINT PRIMARY KEY DEFAULT NEXTVAL('acq.lineitem_attr_definition_id_seq'),
324 provider INT NOT NULL REFERENCES acq.provider (id) DEFERRABLE INITIALLY DEFERRED
325 ) INHERITS (acq.lineitem_attr_definition);
327 CREATE TABLE acq.lineitem_generated_attr_definition (
328 id BIGINT PRIMARY KEY DEFAULT NEXTVAL('acq.lineitem_attr_definition_id_seq'),
330 ) INHERITS (acq.lineitem_attr_definition);
332 CREATE TABLE acq.lineitem_usr_attr_definition (
333 id BIGINT PRIMARY KEY DEFAULT NEXTVAL('acq.lineitem_attr_definition_id_seq'),
334 usr INT NOT NULL REFERENCES actor.usr (id) DEFERRABLE INITIALLY DEFERRED
335 ) INHERITS (acq.lineitem_attr_definition);
336 CREATE INDEX li_usr_attr_def_usr_idx ON acq.lineitem_usr_attr_definition ( usr );
338 CREATE TABLE acq.lineitem_local_attr_definition (
339 id BIGINT PRIMARY KEY DEFAULT NEXTVAL('acq.lineitem_attr_definition_id_seq')
340 ) INHERITS (acq.lineitem_attr_definition);
342 CREATE TABLE acq.lineitem_attr (
343 id BIGSERIAL PRIMARY KEY,
344 definition BIGINT NOT NULL,
345 lineitem BIGINT NOT NULL REFERENCES acq.lineitem (id) DEFERRABLE INITIALLY DEFERRED,
346 attr_type TEXT NOT NULL,
347 attr_name TEXT NOT NULL,
348 attr_value TEXT NOT NULL
351 CREATE INDEX li_attr_li_idx ON acq.lineitem_attr (lineitem);
352 CREATE INDEX li_attr_value_idx ON acq.lineitem_attr (attr_value);
353 CREATE INDEX li_attr_definition_idx ON acq.lineitem_attr (definition);
359 INSERT INTO acq.lineitem_marc_attr_definition ( code, description, xpath ) VALUES ('title','Title of work','//*[@tag="245"]/*[contains("abcmnopr",@code)]');
360 INSERT INTO acq.lineitem_marc_attr_definition ( code, description, xpath ) VALUES ('author','Author of work','//*[@tag="100" or @tag="110" or @tag="113"]/*[contains("ad",@code)]');
361 INSERT INTO acq.lineitem_marc_attr_definition ( code, description, xpath ) VALUES ('language','Language of work','//*[@tag="240"]/*[@code="l"][1]');
362 INSERT INTO acq.lineitem_marc_attr_definition ( code, description, xpath ) VALUES ('pagination','Pagination','//*[@tag="300"]/*[@code="a"][1]');
363 INSERT INTO acq.lineitem_marc_attr_definition ( code, description, xpath, remove ) VALUES ('isbn','ISBN','//*[@tag="020"]/*[@code="a"]', $r$(?:-|\s.+$)$r$);
364 INSERT INTO acq.lineitem_marc_attr_definition ( code, description, xpath, remove ) VALUES ('issn','ISSN','//*[@tag="022"]/*[@code="a"]', $r$(?:-|\s.+$)$r$);
365 INSERT INTO acq.lineitem_marc_attr_definition ( code, description, xpath ) VALUES ('price','Price','//*[@tag="020" or @tag="022"]/*[@code="c"][1]');
366 INSERT INTO acq.lineitem_marc_attr_definition ( code, description, xpath ) VALUES ('identifier','Identifier','//*[@tag="001"]');
367 INSERT INTO acq.lineitem_marc_attr_definition ( code, description, xpath ) VALUES ('publisher','Publisher','//*[@tag="260"]/*[@code="b"][1]');
368 INSERT INTO acq.lineitem_marc_attr_definition ( code, description, xpath ) VALUES ('pubdate','Publication Date','//*[@tag="260"]/*[@code="c"][1]');
369 INSERT INTO acq.lineitem_marc_attr_definition ( code, description, xpath ) VALUES ('edition','Edition','//*[@tag="250"]/*[@code="a"][1]');
371 INSERT INTO acq.lineitem_local_attr_definition ( code, description ) VALUES ('estimated_price', 'Estimated Price');
374 CREATE TABLE acq.distribution_formula (
375 id SERIAL PRIMARY KEY,
377 REFERENCES actor.org_unit(id) DEFERRABLE INITIALLY DEFERRED,
379 skip_count INT NOT NULL DEFAULT 0,
380 CONSTRAINT acqdf_name_once_per_owner UNIQUE (name, owner)
383 CREATE TABLE acq.distribution_formula_entry (
384 id SERIAL PRIMARY KEY,
385 formula INTEGER NOT NULL REFERENCES acq.distribution_formula(id)
387 DEFERRABLE INITIALLY DEFERRED,
388 position INTEGER NOT NULL,
389 item_count INTEGER NOT NULL,
390 owning_lib INTEGER REFERENCES actor.org_unit(id)
391 DEFERRABLE INITIALLY DEFERRED,
392 location INTEGER REFERENCES asset.copy_location(id),
393 CONSTRAINT acqdfe_lib_once_per_formula UNIQUE( formula, position ),
394 CONSTRAINT acqdfe_must_be_somewhere
395 CHECK( owning_lib IS NOT NULL OR location IS NOT NULL )
398 CREATE TABLE acq.fund_tag (
399 id SERIAL PRIMARY KEY,
401 REFERENCES actor.org_unit(id) DEFERRABLE INITIALLY DEFERRED,
403 CONSTRAINT acqft_tag_once_per_owner UNIQUE (name, owner)
406 CREATE TABLE acq.fund_tag_map (
407 id SERIAL PRIMARY KEY,
408 fund INTEGER NOT NULL REFERENCES acq.fund(id)
409 DEFERRABLE INITIALLY DEFERRED,
410 tag INTEGER REFERENCES acq.fund_tag(id)
412 DEFERRABLE INITIALLY DEFERRED,
413 CONSTRAINT acqftm_fund_once_per_tag UNIQUE( fund, tag )
418 CREATE TYPE acq.flat_lineitem_holding_subfield AS (lineitem int, holding int, subfield text, data text);
419 CREATE OR REPLACE FUNCTION acq.extract_holding_attr_table (lineitem int, tag text) RETURNS SETOF acq.flat_lineitem_holding_subfield AS $$
422 lida acq.flat_lineitem_holding_subfield%ROWTYPE;
425 SELECT COUNT(*) INTO counter
430 '//*[@tag="' || tag || '"]',
432 ) as t(i int,c text);
434 FOR i IN 1 .. counter LOOP
437 FROM ( SELECT id,i,t,v
442 '//*[@tag="' || tag || '"][position()=' || i || ']/*/@code|' ||
443 '//*[@tag="' || tag || '"][position()=' || i || ']/*[@code]',
445 ) as t(id int,t text,v text)
456 CREATE TYPE acq.flat_lineitem_detail AS (lineitem int, holding int, attr text, data text);
457 CREATE OR REPLACE FUNCTION acq.extract_provider_holding_data ( lineitem_i int ) RETURNS SETOF acq.flat_lineitem_detail AS $$
461 lida acq.flat_lineitem_detail%ROWTYPE;
463 SELECT provider INTO prov_i FROM acq.lineitem WHERE id = lineitem_i;
464 IF NOT FOUND THEN RETURN; END IF;
466 SELECT holding_tag INTO tag_t FROM acq.provider WHERE id = prov_i;
467 IF NOT FOUND OR tag_t IS NULL THEN RETURN; END IF;
474 FROM acq.extract_holding_attr_table( lineitem_i, tag_t ) h
475 JOIN acq.provider_holding_subfield_map a USING (subfield)
476 WHERE a.provider = prov_i
485 -- select * from acq.extract_provider_holding_data(699);
487 CREATE OR REPLACE FUNCTION public.extract_acq_marc_field ( BIGINT, TEXT, TEXT) RETURNS TEXT AS $$
488 SELECT public.extract_marc_field('acq.lineitem', $1, $2, $3);
492 CREATE OR REPLACE FUNCTION public.extract_bib_marc_field ( BIGINT, TEXT ) RETURNS TEXT AS $$
493 SELECT public.extract_marc_field('biblio.record_entry', $1, $2);
496 CREATE OR REPLACE FUNCTION public.extract_authority_marc_field ( BIGINT, TEXT ) RETURNS TEXT AS $$
497 SELECT public.extract_marc_field('authority.record_entry', $1, $2);
501 -- INSERT INTO acq.lineitem_provider_attr_definition ( provider, code, description, xpath ) VALUES (1,'price','Price','//*[@tag="020" or @tag="022"]/*[@code="a"][1]');
504 Suggested vendor fields:
512 CREATE OR REPLACE FUNCTION public.ingest_acq_marc ( ) RETURNS TRIGGER AS $$
520 FOR adef IN SELECT *,tableoid FROM acq.lineitem_attr_definition LOOP
522 SELECT relname::TEXT INTO atype FROM pg_class WHERE oid = adef.tableoid;
524 IF (atype NOT IN ('lineitem_usr_attr_definition','lineitem_local_attr_definition')) THEN
525 IF (atype = 'lineitem_provider_attr_definition') THEN
526 SELECT provider INTO prov FROM acq.lineitem_provider_attr_definition WHERE id = adef.id;
527 CONTINUE WHEN NEW.provider IS NULL OR prov <> NEW.provider;
530 IF (atype = 'lineitem_provider_attr_definition') THEN
531 SELECT xpath INTO xpath_string FROM acq.lineitem_provider_attr_definition WHERE id = adef.id;
532 ELSIF (atype = 'lineitem_marc_attr_definition') THEN
533 SELECT xpath INTO xpath_string FROM acq.lineitem_marc_attr_definition WHERE id = adef.id;
534 ELSIF (atype = 'lineitem_generated_attr_definition') THEN
535 SELECT xpath INTO xpath_string FROM acq.lineitem_generated_attr_definition WHERE id = adef.id;
538 SELECT extract_acq_marc_field(id, xpath_string, adef.remove) INTO value FROM acq.lineitem WHERE id = NEW.id;
540 IF (value IS NOT NULL AND value <> '') THEN
541 INSERT INTO acq.lineitem_attr (lineitem, definition, attr_type, attr_name, attr_value)
542 VALUES (NEW.id, adef.id, atype, adef.code, value);
553 CREATE OR REPLACE FUNCTION public.cleanup_acq_marc ( ) RETURNS TRIGGER AS $$
555 IF TG_OP = 'UPDATE' THEN
556 DELETE FROM acq.lineitem_attr
557 WHERE lineitem = OLD.id AND attr_type IN ('lineitem_provider_attr_definition', 'lineitem_marc_attr_definition','lineitem_generated_attr_definition');
560 DELETE FROM acq.lineitem_attr WHERE lineitem = OLD.id;
566 CREATE TRIGGER cleanup_lineitem_trigger
567 BEFORE UPDATE OR DELETE ON acq.lineitem
568 FOR EACH ROW EXECUTE PROCEDURE public.cleanup_acq_marc();
570 CREATE TRIGGER ingest_lineitem_trigger
571 AFTER INSERT OR UPDATE ON acq.lineitem
572 FOR EACH ROW EXECUTE PROCEDURE public.ingest_acq_marc();
574 CREATE OR REPLACE FUNCTION acq.exchange_ratio ( from_ex TEXT, to_ex TEXT ) RETURNS NUMERIC AS $$
578 IF from_ex = to_ex THEN
582 SELECT ratio INTO rat FROM acq.exchange_rate WHERE from_currency = from_ex AND to_currency = to_ex;
587 SELECT ratio INTO rat FROM acq.exchange_rate WHERE from_currency = to_ex AND to_currency = from_ex;
598 CREATE OR REPLACE FUNCTION acq.exchange_ratio ( TEXT, TEXT, NUMERIC ) RETURNS NUMERIC AS $$
599 SELECT $3 * acq.exchange_ratio($1, $2);
602 CREATE OR REPLACE VIEW acq.funding_source_credit_total AS
603 SELECT funding_source,
604 SUM(amount) AS amount
605 FROM acq.funding_source_credit
608 CREATE OR REPLACE VIEW acq.funding_source_allocation_total AS
609 SELECT funding_source,
610 SUM(amount)::NUMERIC(100,2) AS amount
612 SELECT funding_source,
613 SUM(a.amount)::NUMERIC(100,2) AS amount
614 FROM acq.fund_allocation a
615 WHERE a.percent IS NULL
618 SELECT funding_source,
619 SUM( (SELECT SUM(amount) FROM acq.funding_source_credit c WHERE c.funding_source = a.funding_source) * (a.percent/100.0) )::NUMERIC(100,2) AS amount
620 FROM acq.fund_allocation a
621 WHERE a.amount IS NULL
626 CREATE OR REPLACE VIEW acq.funding_source_balance AS
627 SELECT COALESCE(c.funding_source, a.funding_source) AS funding_source,
628 SUM(COALESCE(c.amount,0.0) - COALESCE(a.amount,0.0))::NUMERIC(100,2) AS amount
629 FROM acq.funding_source_credit_total c
630 FULL JOIN acq.funding_source_allocation_total a USING (funding_source)
633 CREATE OR REPLACE VIEW acq.fund_allocation_total AS
635 SUM(amount)::NUMERIC(100,2) AS amount
638 SUM(a.amount * acq.exchange_ratio(s.currency_type, f.currency_type))::NUMERIC(100,2) AS amount
639 FROM acq.fund_allocation a
640 JOIN acq.fund f ON (a.fund = f.id)
641 JOIN acq.funding_source s ON (a.funding_source = s.id)
642 WHERE a.percent IS NULL
646 SUM( (SELECT SUM(amount) FROM acq.funding_source_credit c WHERE c.funding_source = a.funding_source) * acq.exchange_ratio(s.currency_type, f.currency_type) * (a.percent/100.0) )::NUMERIC(100,2) AS amount
647 FROM acq.fund_allocation a
648 JOIN acq.fund f ON (a.fund = f.id)
649 JOIN acq.funding_source s ON (a.funding_source = s.id)
650 WHERE a.amount IS NULL
655 CREATE OR REPLACE VIEW acq.fund_debit_total AS
658 SUM(amount) AS amount
662 CREATE OR REPLACE VIEW acq.fund_encumbrance_total AS
664 SUM(amount) AS amount
665 FROM acq.fund_debit_total
666 WHERE encumbrance IS TRUE
669 CREATE OR REPLACE VIEW acq.fund_spent_total AS
671 SUM(amount) AS amount
672 FROM acq.fund_debit_total
673 WHERE encumbrance IS FALSE
676 CREATE OR REPLACE VIEW acq.fund_combined_balance AS
678 c.amount - COALESCE(d.amount,0.0) AS amount
679 FROM acq.fund_allocation_total c
680 LEFT JOIN acq.fund_debit_total d USING (fund);
682 CREATE OR REPLACE VIEW acq.fund_spent_balance AS
684 c.amount - COALESCE(d.amount,0.0) AS amount
685 FROM acq.fund_allocation_total c
686 LEFT JOIN acq.fund_spent_total d USING (fund);