2 * Copyright (C) 2004-2008 Georgia Public Library Service
3 * Copyright (C) 2007-2008 Equinox Software, Inc.
4 * Mike Rylander <miker@esilibrary.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
18 DROP SCHEMA IF EXISTS asset CASCADE;
24 CREATE TABLE asset.copy_location (
25 id SERIAL PRIMARY KEY,
27 owning_lib INT NOT NULL REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED,
28 holdable BOOL NOT NULL DEFAULT TRUE,
29 hold_verify BOOL NOT NULL DEFAULT FALSE,
30 opac_visible BOOL NOT NULL DEFAULT TRUE,
31 circulate BOOL NOT NULL DEFAULT TRUE,
34 checkin_alert BOOL NOT NULL DEFAULT FALSE,
35 CONSTRAINT acl_name_once_per_lib UNIQUE (name, owning_lib)
38 CREATE TABLE asset.copy_location_order
40 id SERIAL PRIMARY KEY,
42 REFERENCES asset.copy_location
44 DEFERRABLE INITIALLY DEFERRED,
46 REFERENCES actor.org_unit
48 DEFERRABLE INITIALLY DEFERRED,
49 position INT NOT NULL DEFAULT 0,
50 CONSTRAINT acplo_once_per_org UNIQUE ( location, org )
53 CREATE TABLE asset.copy_location_group (
54 id SERIAL PRIMARY KEY,
55 name TEXT NOT NULL, -- i18n
56 owner INT NOT NULL REFERENCES actor.org_unit (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
57 pos INT NOT NULL DEFAULT 0,
58 top BOOL NOT NULL DEFAULT FALSE,
59 opac_visible BOOL NOT NULL DEFAULT TRUE,
60 CONSTRAINT lgroup_once_per_owner UNIQUE (owner,name)
63 CREATE TABLE asset.copy_location_group_map (
64 id SERIAL PRIMARY KEY,
65 location INT NOT NULL REFERENCES asset.copy_location (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
66 lgroup INT NOT NULL REFERENCES asset.copy_location_group (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
67 CONSTRAINT lgroup_once_per_group UNIQUE (lgroup,location)
71 CREATE TABLE asset.copy (
72 id BIGSERIAL PRIMARY KEY,
73 circ_lib INT NOT NULL REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED,
74 creator BIGINT NOT NULL,
75 call_number BIGINT NOT NULL,
76 editor BIGINT NOT NULL,
77 create_date TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
78 edit_date TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
80 status INT NOT NULL DEFAULT 0 REFERENCES config.copy_status (id) DEFERRABLE INITIALLY DEFERRED,
81 location INT NOT NULL DEFAULT 1 REFERENCES asset.copy_location (id) DEFERRABLE INITIALLY DEFERRED,
82 loan_duration INT NOT NULL CHECK ( loan_duration IN (1,2,3) ),
83 fine_level INT NOT NULL CHECK ( fine_level IN (1,2,3) ),
85 circulate BOOL NOT NULL DEFAULT TRUE,
86 deposit BOOL NOT NULL DEFAULT FALSE,
87 ref BOOL NOT NULL DEFAULT FALSE,
88 holdable BOOL NOT NULL DEFAULT TRUE,
89 deposit_amount NUMERIC(6,2) NOT NULL DEFAULT 0.00,
91 barcode TEXT NOT NULL,
97 opac_visible BOOL NOT NULL DEFAULT TRUE,
98 deleted BOOL NOT NULL DEFAULT FALSE,
99 floating BOOL NOT NULL DEFAULT FALSE,
101 status_changed_time TIMESTAMP WITH TIME ZONE,
102 active_date TIMESTAMP WITH TIME ZONE,
103 mint_condition BOOL NOT NULL DEFAULT TRUE,
106 CREATE UNIQUE INDEX copy_barcode_key ON asset.copy (barcode) WHERE deleted = FALSE OR deleted IS FALSE;
107 CREATE INDEX cp_cn_idx ON asset.copy (call_number);
108 CREATE INDEX cp_avail_cn_idx ON asset.copy (call_number);
109 CREATE INDEX cp_creator_idx ON asset.copy ( creator );
110 CREATE INDEX cp_editor_idx ON asset.copy ( editor );
111 CREATE INDEX cp_create_date ON asset.copy (create_date);
112 CREATE RULE protect_copy_delete AS ON DELETE TO asset.copy DO INSTEAD UPDATE asset.copy SET deleted = TRUE WHERE OLD.id = asset.copy.id;
114 CREATE TABLE asset.copy_part_map (
115 id SERIAL PRIMARY KEY,
116 target_copy BIGINT NOT NULL, -- points o asset.copy
117 part INT NOT NULL REFERENCES biblio.monograph_part (id) ON DELETE CASCADE
119 CREATE UNIQUE INDEX copy_part_map_cp_part_idx ON asset.copy_part_map (target_copy, part);
121 CREATE TABLE asset.opac_visible_copies (
122 id BIGSERIAL primary key,
123 copy_id BIGINT, -- copy id
127 COMMENT ON TABLE asset.opac_visible_copies IS $$
128 Materialized view of copies that are visible in the OPAC, used by
129 search.query_parser_fts() to speed up OPAC visibility checks on large
130 databases. Contents are maintained by a set of triggers.
132 CREATE INDEX opac_visible_copies_idx1 on asset.opac_visible_copies (record, circ_lib);
133 CREATE INDEX opac_visible_copies_copy_id_idx on asset.opac_visible_copies (copy_id);
134 CREATE UNIQUE INDEX opac_visible_copies_once_per_record_idx on asset.opac_visible_copies (copy_id, record);
136 CREATE OR REPLACE FUNCTION asset.acp_status_changed()
137 RETURNS TRIGGER AS $$
139 IF NEW.status <> OLD.status AND NOT (NEW.status = 0 AND OLD.status = 7) THEN
140 NEW.status_changed_time := now();
141 IF NEW.active_date IS NULL AND NEW.status IN (SELECT id FROM config.copy_status WHERE copy_active = true) THEN
142 NEW.active_date := now();
149 -- Need to check on initial create. Fast adds, manual edit of status at create, etc.
150 CREATE OR REPLACE FUNCTION asset.acp_created()
151 RETURNS TRIGGER AS $$
153 IF NEW.active_date IS NULL AND NEW.status IN (SELECT id FROM config.copy_status WHERE copy_active = true) THEN
154 NEW.active_date := now();
156 IF NEW.status_changed_time IS NULL THEN
157 NEW.status_changed_time := now();
163 CREATE TRIGGER acp_status_changed_trig
164 BEFORE UPDATE ON asset.copy
165 FOR EACH ROW EXECUTE PROCEDURE asset.acp_status_changed();
167 CREATE TRIGGER acp_created_trig
168 BEFORE INSERT ON asset.copy
169 FOR EACH ROW EXECUTE PROCEDURE asset.acp_created();
171 CREATE TABLE asset.stat_cat_sip_fields (
172 field CHAR(2) PRIMARY KEY,
174 one_only BOOL NOT NULL DEFAULT FALSE
176 COMMENT ON TABLE asset.stat_cat_sip_fields IS $$
177 Asset Statistical Category SIP Fields
179 Contains the list of valid SIP Field identifiers for
180 Statistical Categories.
183 CREATE TABLE asset.stat_cat_entry_transparency_map (
184 id BIGSERIAL PRIMARY KEY,
185 stat_cat INT NOT NULL, -- needs ON DELETE CASCADE
186 stat_cat_entry INT NOT NULL, -- needs ON DELETE CASCADE
187 owning_transparency INT NOT NULL, -- needs ON DELETE CASCADE
188 CONSTRAINT scte_once_per_trans UNIQUE (owning_transparency,stat_cat)
191 CREATE TABLE asset.stat_cat (
192 id SERIAL PRIMARY KEY,
194 opac_visible BOOL NOT NULL DEFAULT FALSE,
196 required BOOL NOT NULL DEFAULT FALSE,
197 sip_field CHAR(2) REFERENCES asset.stat_cat_sip_fields(field) ON UPDATE CASCADE ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED,
199 checkout_archive BOOL NOT NULL DEFAULT FALSE,
200 CONSTRAINT sc_once_per_owner UNIQUE (owner,name)
203 CREATE TABLE asset.stat_cat_entry (
204 id SERIAL PRIMARY KEY,
205 stat_cat INT NOT NULL,
208 CONSTRAINT sce_once_per_owner UNIQUE (stat_cat,owner,value)
211 CREATE TABLE asset.stat_cat_entry_copy_map (
212 id BIGSERIAL PRIMARY KEY,
213 stat_cat INT NOT NULL,
214 stat_cat_entry INT NOT NULL,
215 owning_copy BIGINT NOT NULL,
216 CONSTRAINT sce_once_per_copy UNIQUE (owning_copy,stat_cat)
218 CREATE INDEX scecm_owning_copy_idx ON asset.stat_cat_entry_copy_map(owning_copy);
220 CREATE FUNCTION asset.stat_cat_check() RETURNS trigger AS $func$
222 sipfield asset.stat_cat_sip_fields%ROWTYPE;
225 IF NEW.sip_field IS NOT NULL THEN
226 SELECT INTO sipfield * FROM asset.stat_cat_sip_fields WHERE field = NEW.sip_field;
227 IF sipfield.one_only THEN
228 SELECT INTO use_count count(id) FROM asset.stat_cat WHERE sip_field = NEW.sip_field AND id != NEW.id;
229 IF use_count > 0 THEN
230 RAISE EXCEPTION 'Sip field cannot be used twice';
236 $func$ LANGUAGE PLPGSQL;
238 CREATE TRIGGER asset_stat_cat_sip_update_trigger
239 BEFORE INSERT OR UPDATE ON asset.stat_cat FOR EACH ROW
240 EXECUTE PROCEDURE asset.stat_cat_check();
242 CREATE TABLE asset.copy_note (
243 id BIGSERIAL PRIMARY KEY,
244 owning_copy BIGINT NOT NULL,
245 creator BIGINT NOT NULL,
246 create_date TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
247 pub BOOL NOT NULL DEFAULT FALSE,
251 CREATE INDEX asset_copy_note_creator_idx ON asset.copy_note ( creator );
252 CREATE INDEX asset_copy_note_owning_copy_idx ON asset.copy_note ( owning_copy );
254 CREATE TABLE asset.uri (
255 id SERIAL PRIMARY KEY,
258 use_restriction TEXT,
259 active BOOL NOT NULL DEFAULT TRUE
262 CREATE TABLE asset.call_number_class (
263 id bigserial PRIMARY KEY,
265 normalizer TEXT NOT NULL DEFAULT 'asset.normalize_generic',
266 field TEXT NOT NULL DEFAULT '050ab,055ab,060ab,070ab,080ab,082ab,086ab,088ab,090,092,096,098,099'
268 COMMENT ON TABLE asset.call_number_class IS $$
269 Defines the call number normalization database functions in the "normalizer"
270 column and the tag/subfield combinations to use to lookup the call number in
271 the "field" column for a given classification scheme. Tag/subfield combinations
272 are delimited by commas.
275 CREATE OR REPLACE FUNCTION asset.label_normalizer() RETURNS TRIGGER AS $func$
279 sortkey := NEW.label_sortkey;
281 IF NEW.label_class IS NULL THEN
282 NEW.label_class := COALESCE(
284 SELECT substring(value from E'\\d+')::integer
285 FROM actor.org_unit_ancestor_setting('cat.default_classification_scheme', NEW.owning_lib)
290 EXECUTE 'SELECT ' || acnc.normalizer || '(' ||
291 quote_literal( NEW.label ) || ')'
292 FROM asset.call_number_class acnc
293 WHERE acnc.id = NEW.label_class
295 NEW.label_sortkey = sortkey;
298 $func$ LANGUAGE PLPGSQL;
300 CREATE OR REPLACE FUNCTION asset.label_normalizer_generic(TEXT) RETURNS TEXT AS $func$
301 # Created after looking at the Koha C4::ClassSortRoutine::Generic module,
302 # thus could probably be considered a derived work, although nothing was
303 # directly copied - but to err on the safe side of providing attribution:
304 # Copyright (C) 2007 LibLime
305 # Copyright (C) 2011 Equinox Software, Inc (Steve Callendar)
306 # Licensed under the GPL v2 or later
311 # Converts the callnumber to uppercase
312 # Strips spaces from start and end of the call number
313 # Converts anything other than letters, digits, and periods into spaces
314 # Collapses multiple spaces into a single underscore
315 my $callnum = uc(shift);
316 $callnum =~ s/^\s//g;
317 $callnum =~ s/\s$//g;
318 # NOTE: this previously used underscores, but this caused sorting issues
319 # for the "before" half of page 0 on CN browse, sorting CNs containing a
320 # decimal before "whole number" CNs
321 $callnum =~ s/[^A-Z0-9_.]/ /g;
322 $callnum =~ s/ {2,}/ /g;
325 $func$ LANGUAGE PLPERLU;
327 CREATE OR REPLACE FUNCTION asset.label_normalizer_dewey(TEXT) RETURNS TEXT AS $func$
328 # Derived from the Koha C4::ClassSortRoutine::Dewey module
329 # Copyright (C) 2007 LibLime
330 # Licensed under the GPL v2 or later
335 my $init = uc(shift);
339 $init =~ s/^([\p{IsAlpha}]+)/$1 /;
340 my @tokens = split /\.|\s+/, $init;
341 my $digit_group_count = 0;
342 my $first_digit_group_idx;
343 for (my $i = 0; $i <= $#tokens; $i++) {
344 if ($tokens[$i] =~ /^\d+$/) {
345 $digit_group_count++;
346 if ($digit_group_count == 1) {
347 $first_digit_group_idx = $i;
349 if (2 == $digit_group_count) {
350 $tokens[$i] = sprintf("%-15.15s", $tokens[$i]);
351 $tokens[$i] =~ tr/ /0/;
355 # Pad the first digit_group if there was only one
356 if (1 == $digit_group_count) {
357 $tokens[$first_digit_group_idx] .= '_000000000000000'
359 my $key = join("_", @tokens);
360 $key =~ s/[^\p{IsAlnum}_]//g;
364 $func$ LANGUAGE PLPERLU;
367 CREATE OR REPLACE FUNCTION asset.label_normalizer_lc(TEXT) RETURNS TEXT AS $func$
371 # Library::CallNumber::LC is currently hosted at http://code.google.com/p/library-callnumber-lc/
372 # The author hopes to upload it to CPAN some day, which would make our lives easier
373 use Library::CallNumber::LC;
375 my $callnum = Library::CallNumber::LC->new(shift);
376 return $callnum->normalize();
378 $func$ LANGUAGE PLPERLU;
380 INSERT INTO asset.call_number_class (name, normalizer, field) VALUES
381 ('Generic', 'asset.label_normalizer_generic', '050ab,055ab,060ab,070ab,080ab,082ab,086ab,088ab,090,092,096,098,099'),
382 ('Dewey (DDC)', 'asset.label_normalizer_dewey', '080ab,082ab,092abef'),
383 ('Library of Congress (LC)', 'asset.label_normalizer_lc', '050ab,055ab,090abef')
386 CREATE OR REPLACE FUNCTION asset.normalize_affix_sortkey () RETURNS TRIGGER AS $$
388 NEW.label_sortkey := REGEXP_REPLACE(
389 evergreen.lpad_number_substrings(
390 naco_normalize(NEW.label),
402 CREATE TABLE asset.call_number_prefix (
403 id SERIAL PRIMARY KEY,
404 owning_lib INT NOT NULL REFERENCES actor.org_unit (id),
405 label TEXT NOT NULL, -- i18n
408 CREATE TRIGGER prefix_normalize_tgr BEFORE INSERT OR UPDATE ON asset.call_number_prefix FOR EACH ROW EXECUTE PROCEDURE asset.normalize_affix_sortkey();
409 CREATE UNIQUE INDEX asset_call_number_prefix_once_per_lib ON asset.call_number_prefix (label, owning_lib);
410 CREATE INDEX asset_call_number_prefix_sortkey_idx ON asset.call_number_prefix (label_sortkey);
412 CREATE TABLE asset.call_number_suffix (
413 id SERIAL PRIMARY KEY,
414 owning_lib INT NOT NULL REFERENCES actor.org_unit (id),
415 label TEXT NOT NULL, -- i18n
418 CREATE TRIGGER suffix_normalize_tgr BEFORE INSERT OR UPDATE ON asset.call_number_suffix FOR EACH ROW EXECUTE PROCEDURE asset.normalize_affix_sortkey();
419 CREATE UNIQUE INDEX asset_call_number_suffix_once_per_lib ON asset.call_number_suffix (label, owning_lib);
420 CREATE INDEX asset_call_number_suffix_sortkey_idx ON asset.call_number_suffix (label_sortkey);
422 CREATE TABLE asset.call_number (
423 id bigserial PRIMARY KEY,
424 creator BIGINT NOT NULL,
425 create_date TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
426 editor BIGINT NOT NULL,
427 edit_date TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
428 record bigint NOT NULL,
429 owning_lib INT NOT NULL,
431 deleted BOOL NOT NULL DEFAULT FALSE,
432 prefix INT NOT NULL DEFAULT -1 REFERENCES asset.call_number_prefix(id) DEFERRABLE INITIALLY DEFERRED,
433 suffix INT NOT NULL DEFAULT -1 REFERENCES asset.call_number_suffix(id) DEFERRABLE INITIALLY DEFERRED,
434 label_class BIGINT NOT NULL
435 REFERENCES asset.call_number_class(id)
436 DEFERRABLE INITIALLY DEFERRED,
439 CREATE INDEX asset_call_number_record_idx ON asset.call_number (record);
440 CREATE INDEX asset_call_number_creator_idx ON asset.call_number (creator);
441 CREATE INDEX asset_call_number_editor_idx ON asset.call_number (editor);
442 CREATE INDEX asset_call_number_dewey_idx ON asset.call_number (public.call_number_dewey(label));
443 CREATE INDEX asset_call_number_upper_label_id_owning_lib_idx ON asset.call_number (oils_text_as_bytea(label),id,owning_lib);
444 CREATE INDEX asset_call_number_label_sortkey ON asset.call_number(oils_text_as_bytea(label_sortkey));
445 CREATE UNIQUE INDEX asset_call_number_label_once_per_lib ON asset.call_number (record, owning_lib, label, prefix, suffix) WHERE deleted = FALSE OR deleted IS FALSE;
446 CREATE INDEX asset_call_number_label_sortkey_browse ON asset.call_number(oils_text_as_bytea(label_sortkey), oils_text_as_bytea(label), id, owning_lib) WHERE deleted IS FALSE OR deleted = FALSE;
447 CREATE RULE protect_cn_delete AS ON DELETE TO asset.call_number DO INSTEAD UPDATE asset.call_number SET deleted = TRUE WHERE OLD.id = asset.call_number.id;
448 CREATE TRIGGER asset_label_sortkey_trigger
449 BEFORE UPDATE OR INSERT ON asset.call_number
450 FOR EACH ROW EXECUTE PROCEDURE asset.label_normalizer();
452 CREATE TABLE asset.uri_call_number_map (
453 id BIGSERIAL PRIMARY KEY,
454 uri INT NOT NULL REFERENCES asset.uri (id),
455 call_number INT NOT NULL REFERENCES asset.call_number (id),
456 CONSTRAINT uri_cn_once UNIQUE (uri,call_number)
458 CREATE INDEX asset_uri_call_number_map_cn_idx ON asset.uri_call_number_map (call_number);
460 CREATE TABLE asset.call_number_note (
461 id BIGSERIAL PRIMARY KEY,
462 call_number BIGINT NOT NULL,
463 creator BIGINT NOT NULL,
464 create_date TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
465 pub BOOL NOT NULL DEFAULT FALSE,
469 CREATE INDEX asset_call_number_note_creator_idx ON asset.call_number_note ( creator );
471 CREATE TABLE asset.copy_template (
472 id SERIAL PRIMARY KEY,
473 owning_lib INT NOT NULL
474 REFERENCES actor.org_unit (id)
475 DEFERRABLE INITIALLY DEFERRED,
476 creator BIGINT NOT NULL
477 REFERENCES actor.usr (id)
478 DEFERRABLE INITIALLY DEFERRED,
479 editor BIGINT NOT NULL
480 REFERENCES actor.usr (id)
481 DEFERRABLE INITIALLY DEFERRED,
482 create_date TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
483 edit_date TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
485 -- columns above this point are attributes of the template itself
486 -- columns after this point are attributes of the copy this template modifies/creates
487 circ_lib INT REFERENCES actor.org_unit (id)
488 DEFERRABLE INITIALLY DEFERRED,
489 status INT REFERENCES config.copy_status (id)
490 DEFERRABLE INITIALLY DEFERRED,
491 location INT REFERENCES asset.copy_location (id)
492 DEFERRABLE INITIALLY DEFERRED,
493 loan_duration INT CONSTRAINT valid_loan_duration CHECK (
494 loan_duration IS NULL OR loan_duration IN (1,2,3)),
495 fine_level INT CONSTRAINT valid_fine_level CHECK (
496 fine_level IS NULL OR loan_duration IN (1,2,3)),
502 deposit_amount NUMERIC(6,2),
512 CREATE OR REPLACE FUNCTION asset.opac_ou_record_copy_count (org INT, rid BIGINT) RETURNS TABLE (depth INT, org_unit INT, visible BIGINT, available BIGINT, unshadow BIGINT, transcendant INT) AS $f$
517 SELECT 1 INTO trans FROM biblio.record_entry b JOIN config.bib_source src ON (b.source = src.id) WHERE src.transcendant AND b.id = rid;
519 FOR ans IN SELECT u.id, t.depth FROM actor.org_unit_ancestors(org) AS u JOIN actor.org_unit_type t ON (u.ou_type = t.id) LOOP
524 SUM( CASE WHEN cp.status IN (0,7,12) THEN 1 ELSE 0 END ),
528 actor.org_unit_descendants(ans.id) d
529 JOIN asset.opac_visible_copies av ON (av.record = rid AND av.circ_lib = d.id)
530 JOIN asset.copy cp ON (cp.id = av.copy_id)
534 RETURN QUERY SELECT ans.depth, ans.id, 0::BIGINT, 0::BIGINT, 0::BIGINT, trans;
541 $f$ LANGUAGE PLPGSQL;
543 CREATE OR REPLACE FUNCTION asset.opac_lasso_record_copy_count (i_lasso INT, rid BIGINT) RETURNS TABLE (depth INT, org_unit INT, visible BIGINT, available BIGINT, unshadow BIGINT, transcendant INT) AS $f$
548 SELECT 1 INTO trans FROM biblio.record_entry b JOIN config.bib_source src ON (b.source = src.id) WHERE src.transcendant AND b.id = rid;
550 FOR ans IN SELECT u.org_unit AS id FROM actor.org_lasso_map AS u WHERE lasso = i_lasso LOOP
555 SUM( CASE WHEN cp.status IN (0,7,12) THEN 1 ELSE 0 END ),
559 actor.org_unit_descendants(ans.id) d
560 JOIN asset.opac_visible_copies av ON (av.record = rid AND av.circ_lib = d.id)
561 JOIN asset.copy cp ON (cp.id = av.copy_id)
565 RETURN QUERY SELECT -1, ans.id, 0::BIGINT, 0::BIGINT, 0::BIGINT, trans;
572 $f$ LANGUAGE PLPGSQL;
574 CREATE OR REPLACE FUNCTION asset.staff_ou_record_copy_count (org INT, rid BIGINT) RETURNS TABLE (depth INT, org_unit INT, visible BIGINT, available BIGINT, unshadow BIGINT, transcendant INT) AS $f$
579 SELECT 1 INTO trans FROM biblio.record_entry b JOIN config.bib_source src ON (b.source = src.id) WHERE src.transcendant AND b.id = rid;
581 FOR ans IN SELECT u.id, t.depth FROM actor.org_unit_ancestors(org) AS u JOIN actor.org_unit_type t ON (u.ou_type = t.id) LOOP
586 SUM( CASE WHEN cp.status IN (0,7,12) THEN 1 ELSE 0 END ),
587 SUM( CASE WHEN cl.opac_visible AND cp.opac_visible THEN 1 ELSE 0 END),
590 actor.org_unit_descendants(ans.id) d
591 JOIN asset.copy cp ON (cp.circ_lib = d.id AND NOT cp.deleted)
592 JOIN asset.copy_location cl ON (cp.location = cl.id)
593 JOIN asset.call_number cn ON (cn.record = rid AND cn.id = cp.call_number AND NOT cn.deleted)
597 RETURN QUERY SELECT ans.depth, ans.id, 0::BIGINT, 0::BIGINT, 0::BIGINT, trans;
604 $f$ LANGUAGE PLPGSQL;
606 CREATE OR REPLACE FUNCTION asset.staff_lasso_record_copy_count (i_lasso INT, rid BIGINT) RETURNS TABLE (depth INT, org_unit INT, visible BIGINT, available BIGINT, unshadow BIGINT, transcendant INT) AS $f$
611 SELECT 1 INTO trans FROM biblio.record_entry b JOIN config.bib_source src ON (b.source = src.id) WHERE src.transcendant AND b.id = rid;
613 FOR ans IN SELECT u.org_unit AS id FROM actor.org_lasso_map AS u WHERE lasso = i_lasso LOOP
618 SUM( CASE WHEN cp.status IN (0,7,12) THEN 1 ELSE 0 END ),
619 SUM( CASE WHEN cl.opac_visible AND cp.opac_visible THEN 1 ELSE 0 END),
622 actor.org_unit_descendants(ans.id) d
623 JOIN asset.copy cp ON (cp.circ_lib = d.id AND NOT cp.deleted)
624 JOIN asset.copy_location cl ON (cp.location = cl.id)
625 JOIN asset.call_number cn ON (cn.record = rid AND cn.id = cp.call_number AND NOT cn.deleted)
629 RETURN QUERY SELECT -1, ans.id, 0::BIGINT, 0::BIGINT, 0::BIGINT, trans;
636 $f$ LANGUAGE PLPGSQL;
638 CREATE OR REPLACE FUNCTION asset.record_copy_count ( place INT, rid BIGINT, staff BOOL) RETURNS TABLE (depth INT, org_unit INT, visible BIGINT, available BIGINT, unshadow BIGINT, transcendant INT) AS $f$
640 IF staff IS TRUE THEN
642 RETURN QUERY SELECT * FROM asset.staff_ou_record_copy_count( place, rid );
644 RETURN QUERY SELECT * FROM asset.staff_lasso_record_copy_count( -place, rid );
648 RETURN QUERY SELECT * FROM asset.opac_ou_record_copy_count( place, rid );
650 RETURN QUERY SELECT * FROM asset.opac_lasso_record_copy_count( -place, rid );
656 $f$ LANGUAGE PLPGSQL;
658 CREATE OR REPLACE FUNCTION asset.record_has_holdable_copy ( rid BIGINT ) RETURNS BOOL AS $f$
663 JOIN asset.call_number acn ON acp.call_number = acn.id
664 JOIN asset.copy_location acpl ON acp.location = acpl.id
665 JOIN config.copy_status ccs ON acp.status = ccs.id
668 AND acp.holdable = true
669 AND acpl.holdable = true
670 AND ccs.holdable = true
671 AND acp.deleted = false
678 $f$ LANGUAGE PLPGSQL;
680 CREATE OR REPLACE FUNCTION asset.opac_ou_metarecord_copy_count (org INT, rid BIGINT) RETURNS TABLE (depth INT, org_unit INT, visible BIGINT, available BIGINT, unshadow BIGINT, transcendant INT) AS $f$
685 SELECT 1 INTO trans FROM biblio.record_entry b JOIN config.bib_source src ON (b.source = src.id) WHERE src.transcendant AND b.id = rid;
687 FOR ans IN SELECT u.id, t.depth FROM actor.org_unit_ancestors(org) AS u JOIN actor.org_unit_type t ON (u.ou_type = t.id) LOOP
692 SUM( CASE WHEN cp.status IN (0,7,12) THEN 1 ELSE 0 END ),
696 actor.org_unit_descendants(ans.id) d
697 JOIN asset.opac_visible_copies av ON (av.record = rid AND av.circ_lib = d.id)
698 JOIN asset.copy cp ON (cp.id = av.copy_id)
699 JOIN metabib.metarecord_source_map m ON (m.source = av.record)
703 RETURN QUERY SELECT ans.depth, ans.id, 0::BIGINT, 0::BIGINT, 0::BIGINT, trans;
710 $f$ LANGUAGE PLPGSQL;
712 CREATE OR REPLACE FUNCTION asset.opac_lasso_metarecord_copy_count (i_lasso INT, rid BIGINT) RETURNS TABLE (depth INT, org_unit INT, visible BIGINT, available BIGINT, unshadow BIGINT, transcendant INT) AS $f$
717 SELECT 1 INTO trans FROM biblio.record_entry b JOIN config.bib_source src ON (b.source = src.id) WHERE src.transcendant AND b.id = rid;
719 FOR ans IN SELECT u.org_unit AS id FROM actor.org_lasso_map AS u WHERE lasso = i_lasso LOOP
724 SUM( CASE WHEN cp.status IN (0,7,12) THEN 1 ELSE 0 END ),
728 actor.org_unit_descendants(ans.id) d
729 JOIN asset.opac_visible_copies av ON (av.record = rid AND av.circ_lib = d.id)
730 JOIN asset.copy cp ON (cp.id = av.copy_id)
731 JOIN metabib.metarecord_source_map m ON (m.source = av.record)
735 RETURN QUERY SELECT ans.depth, ans.id, 0::BIGINT, 0::BIGINT, 0::BIGINT, trans;
742 $f$ LANGUAGE PLPGSQL;
744 CREATE OR REPLACE FUNCTION asset.staff_ou_metarecord_copy_count (org INT, rid BIGINT) RETURNS TABLE (depth INT, org_unit INT, visible BIGINT, available BIGINT, unshadow BIGINT, transcendant INT) AS $f$
749 SELECT 1 INTO trans FROM biblio.record_entry b JOIN config.bib_source src ON (b.source = src.id) WHERE src.transcendant AND b.id = rid;
751 FOR ans IN SELECT u.id, t.depth FROM actor.org_unit_ancestors(org) AS u JOIN actor.org_unit_type t ON (u.ou_type = t.id) LOOP
756 SUM( CASE WHEN cp.status IN (0,7,12) THEN 1 ELSE 0 END ),
760 actor.org_unit_descendants(ans.id) d
761 JOIN asset.copy cp ON (cp.circ_lib = d.id AND NOT cp.deleted)
762 JOIN asset.call_number cn ON (cn.record = rid AND cn.id = cp.call_number AND NOT cn.deleted)
763 JOIN metabib.metarecord_source_map m ON (m.source = cn.record)
767 RETURN QUERY SELECT ans.depth, ans.id, 0::BIGINT, 0::BIGINT, 0::BIGINT, trans;
774 $f$ LANGUAGE PLPGSQL;
776 CREATE OR REPLACE FUNCTION asset.staff_lasso_metarecord_copy_count (i_lasso INT, rid BIGINT) RETURNS TABLE (depth INT, org_unit INT, visible BIGINT, available BIGINT, unshadow BIGINT, transcendant INT) AS $f$
781 SELECT 1 INTO trans FROM biblio.record_entry b JOIN config.bib_source src ON (b.source = src.id) WHERE src.transcendant AND b.id = rid;
783 FOR ans IN SELECT u.org_unit AS id FROM actor.org_lasso_map AS u WHERE lasso = i_lasso LOOP
788 SUM( CASE WHEN cp.status IN (0,7,12) THEN 1 ELSE 0 END ),
792 actor.org_unit_descendants(ans.id) d
793 JOIN asset.copy cp ON (cp.circ_lib = d.id AND NOT cp.deleted)
794 JOIN asset.call_number cn ON (cn.record = rid AND cn.id = cp.call_number AND NOT cn.deleted)
795 JOIN metabib.metarecord_source_map m ON (m.source = cn.record)
799 RETURN QUERY SELECT ans.depth, ans.id, 0::BIGINT, 0::BIGINT, 0::BIGINT, trans;
806 $f$ LANGUAGE PLPGSQL;
808 CREATE OR REPLACE FUNCTION asset.metarecord_copy_count ( place INT, rid BIGINT, staff BOOL) RETURNS TABLE (depth INT, org_unit INT, visible BIGINT, available BIGINT, unshadow BIGINT, transcendant INT) AS $f$
810 IF staff IS TRUE THEN
812 RETURN QUERY SELECT * FROM asset.staff_ou_metarecord_copy_count( place, rid );
814 RETURN QUERY SELECT * FROM asset.staff_lasso_metarecord_copy_count( -place, rid );
818 RETURN QUERY SELECT * FROM asset.opac_ou_metarecord_copy_count( place, rid );
820 RETURN QUERY SELECT * FROM asset.opac_lasso_metarecord_copy_count( -place, rid );
826 $f$ LANGUAGE PLPGSQL;
828 CREATE OR REPLACE FUNCTION asset.metarecord_has_holdable_copy ( rid BIGINT ) RETURNS BOOL AS $f$
833 JOIN asset.call_number acn ON acp.call_number = acn.id
834 JOIN asset.copy_location acpl ON acp.location = acpl.id
835 JOIN config.copy_status ccs ON acp.status = ccs.id
836 JOIN metabib.metarecord_source_map mmsm ON acn.record = mmsm.source
838 mmsm.metarecord = rid
839 AND acp.holdable = true
840 AND acpl.holdable = true
841 AND ccs.holdable = true
842 AND acp.deleted = false
849 $f$ LANGUAGE PLPGSQL;
851 CREATE OR REPLACE FUNCTION asset.autogenerate_placeholder_barcode ( ) RETURNS TRIGGER AS $f$
853 IF NEW.barcode LIKE '@@%' THEN
854 NEW.barcode := '@@' || NEW.id;
858 $f$ LANGUAGE PLPGSQL;
860 CREATE TRIGGER autogenerate_placeholder_barcode
861 BEFORE INSERT OR UPDATE ON asset.copy
862 FOR EACH ROW EXECUTE PROCEDURE asset.autogenerate_placeholder_barcode();
864 CREATE OR REPLACE FUNCTION evergreen.fake_fkey_tgr () RETURNS TRIGGER AS $F$
868 EXECUTE 'SELECT ($1).' || quote_ident(TG_ARGV[0]) INTO copy_id USING NEW;
869 PERFORM * FROM asset.copy WHERE id = copy_id;
871 RAISE EXCEPTION 'Key (%.%=%) does not exist in asset.copy', TG_TABLE_SCHEMA, TG_TABLE_NAME, copy_id;
875 $F$ LANGUAGE PLPGSQL;