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 CONSTRAINT acl_name_once_per_lib UNIQUE (name, owning_lib)
37 CREATE TABLE asset.copy_location_order
39 id SERIAL PRIMARY KEY,
41 REFERENCES asset.copy_location
43 DEFERRABLE INITIALLY DEFERRED,
45 REFERENCES actor.org_unit
47 DEFERRABLE INITIALLY DEFERRED,
48 position INT NOT NULL DEFAULT 0,
49 CONSTRAINT acplo_once_per_org UNIQUE ( location, org )
52 CREATE TABLE asset.copy (
53 id BIGSERIAL PRIMARY KEY,
54 circ_lib INT NOT NULL REFERENCES actor.org_unit (id) DEFERRABLE INITIALLY DEFERRED,
55 creator BIGINT NOT NULL,
56 call_number BIGINT NOT NULL,
57 editor BIGINT NOT NULL,
58 create_date TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
59 edit_date TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
61 status INT NOT NULL DEFAULT 0 REFERENCES config.copy_status (id) DEFERRABLE INITIALLY DEFERRED,
62 location INT NOT NULL DEFAULT 1 REFERENCES asset.copy_location (id) DEFERRABLE INITIALLY DEFERRED,
63 loan_duration INT NOT NULL CHECK ( loan_duration IN (1,2,3) ),
64 fine_level INT NOT NULL CHECK ( fine_level IN (1,2,3) ),
66 circulate BOOL NOT NULL DEFAULT TRUE,
67 deposit BOOL NOT NULL DEFAULT FALSE,
68 ref BOOL NOT NULL DEFAULT FALSE,
69 holdable BOOL NOT NULL DEFAULT TRUE,
70 deposit_amount NUMERIC(6,2) NOT NULL DEFAULT 0.00,
72 barcode TEXT NOT NULL,
78 opac_visible BOOL NOT NULL DEFAULT TRUE,
79 deleted BOOL NOT NULL DEFAULT FALSE,
80 floating BOOL NOT NULL DEFAULT FALSE,
82 status_changed_time TIMESTAMP WITH TIME ZONE,
83 mint_condition BOOL NOT NULL DEFAULT TRUE,
86 CREATE UNIQUE INDEX copy_barcode_key ON asset.copy (barcode) WHERE deleted = FALSE OR deleted IS FALSE;
87 CREATE INDEX cp_cn_idx ON asset.copy (call_number);
88 CREATE INDEX cp_avail_cn_idx ON asset.copy (call_number);
89 CREATE INDEX cp_creator_idx ON asset.copy ( creator );
90 CREATE INDEX cp_editor_idx ON asset.copy ( editor );
91 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;
93 CREATE TABLE asset.opac_visible_copies (
94 id BIGINT primary key, -- copy id
98 COMMENT ON TABLE asset.opac_visible_copies IS $$
99 Materialized view of copies that are visible in the OPAC, used by
100 search.query_parser_fts() to speed up OPAC visibility checks on large
101 databases. Contents are maintained by a set of triggers.
103 CREATE INDEX opac_visible_copies_idx1 on asset.opac_visible_copies (record, circ_lib);
105 CREATE OR REPLACE FUNCTION asset.acp_status_changed()
106 RETURNS TRIGGER AS $$
108 IF NEW.status <> OLD.status THEN
109 NEW.status_changed_time := now();
115 CREATE TRIGGER acp_status_changed_trig
116 BEFORE UPDATE ON asset.copy
117 FOR EACH ROW EXECUTE PROCEDURE asset.acp_status_changed();
119 CREATE TABLE asset.stat_cat_entry_transparency_map (
120 id BIGSERIAL PRIMARY KEY,
121 stat_cat INT NOT NULL, -- needs ON DELETE CASCADE
122 stat_cat_entry INT NOT NULL, -- needs ON DELETE CASCADE
123 owning_transparency INT NOT NULL, -- needs ON DELETE CASCADE
124 CONSTRAINT scte_once_per_trans UNIQUE (owning_transparency,stat_cat)
127 CREATE TABLE asset.stat_cat (
128 id SERIAL PRIMARY KEY,
130 opac_visible BOOL NOT NULL DEFAULT FALSE,
132 required BOOL NOT NULL DEFAULT FALSE,
133 CONSTRAINT sc_once_per_owner UNIQUE (owner,name)
136 CREATE TABLE asset.stat_cat_entry (
137 id SERIAL PRIMARY KEY,
138 stat_cat INT NOT NULL,
141 CONSTRAINT sce_once_per_owner UNIQUE (stat_cat,owner,value)
144 CREATE TABLE asset.stat_cat_entry_copy_map (
145 id BIGSERIAL PRIMARY KEY,
146 stat_cat INT NOT NULL,
147 stat_cat_entry INT NOT NULL,
148 owning_copy BIGINT NOT NULL,
149 CONSTRAINT sce_once_per_copy UNIQUE (owning_copy,stat_cat)
151 CREATE INDEX scecm_owning_copy_idx ON asset.stat_cat_entry_copy_map(owning_copy);
153 CREATE TABLE asset.copy_note (
154 id BIGSERIAL PRIMARY KEY,
155 owning_copy BIGINT NOT NULL,
156 creator BIGINT NOT NULL,
157 create_date TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
158 pub BOOL NOT NULL DEFAULT FALSE,
162 CREATE INDEX asset_copy_note_creator_idx ON asset.copy_note ( creator );
164 CREATE TABLE asset.uri (
165 id SERIAL PRIMARY KEY,
168 use_restriction TEXT,
169 active BOOL NOT NULL DEFAULT TRUE
172 CREATE TABLE asset.call_number_class (
173 id bigserial PRIMARY KEY,
175 normalizer TEXT NOT NULL DEFAULT 'asset.normalize_generic',
176 field TEXT NOT NULL DEFAULT '050ab,055ab,060ab,070ab,080ab,082ab,086ab,088ab,090,092,096,098,099'
178 COMMENT ON TABLE asset.call_number_class IS $$
179 Defines the call number normalization database functions in the "normalizer"
180 column and the tag/subfield combinations to use to lookup the call number in
181 the "field" column for a given classification scheme. Tag/subfield combinations
182 are delimited by commas.
185 CREATE OR REPLACE FUNCTION asset.label_normalizer() RETURNS TRIGGER AS $func$
189 sortkey := NEW.label_sortkey;
191 EXECUTE 'SELECT ' || acnc.normalizer || '(' ||
192 quote_literal( NEW.label ) || ')'
193 FROM asset.call_number_class acnc
194 WHERE acnc.id = NEW.label_class
197 NEW.label_sortkey = sortkey;
201 $func$ LANGUAGE PLPGSQL;
203 CREATE OR REPLACE FUNCTION asset.label_normalizer_generic(TEXT) RETURNS TEXT AS $func$
204 # Created after looking at the Koha C4::ClassSortRoutine::Generic module,
205 # thus could probably be considered a derived work, although nothing was
206 # directly copied - but to err on the safe side of providing attribution:
207 # Copyright (C) 2007 LibLime
208 # Licensed under the GPL v2 or later
213 # Converts the callnumber to uppercase
214 # Strips spaces from start and end of the call number
215 # Converts anything other than letters, digits, and periods into underscores
216 # Collapses multiple underscores into a single underscore
217 my $callnum = uc(shift);
218 $callnum =~ s/^\s//g;
219 $callnum =~ s/\s$//g;
220 $callnum =~ s/[^A-Z0-9_.]/_/g;
221 $callnum =~ s/_{2,}/_/g;
224 $func$ LANGUAGE PLPERLU;
226 CREATE OR REPLACE FUNCTION asset.label_normalizer_dewey(TEXT) RETURNS TEXT AS $func$
227 # Derived from the Koha C4::ClassSortRoutine::Dewey module
228 # Copyright (C) 2007 LibLime
229 # Licensed under the GPL v2 or later
234 my $init = uc(shift);
238 $init =~ s/^([\p{IsAlpha}]+)/$1 /;
239 my @tokens = split /\.|\s+/, $init;
240 my $digit_group_count = 0;
241 for (my $i = 0; $i <= $#tokens; $i++) {
242 if ($tokens[$i] =~ /^\d+$/) {
243 $digit_group_count++;
244 if (2 == $digit_group_count) {
245 $tokens[$i] = sprintf("%-15.15s", $tokens[$i]);
246 $tokens[$i] =~ tr/ /0/;
250 my $key = join("_", @tokens);
251 $key =~ s/[^\p{IsAlnum}_]//g;
255 $func$ LANGUAGE PLPERLU;
257 CREATE OR REPLACE FUNCTION asset.label_normalizer_lc(TEXT) RETURNS TEXT AS $func$
261 # Library::CallNumber::LC is currently hosted at http://code.google.com/p/library-callnumber-lc/
262 # The author hopes to upload it to CPAN some day, which would make our lives easier
263 use Library::CallNumber::LC;
265 my $callnum = Library::CallNumber::LC->new(shift);
266 return $callnum->normalize();
268 $func$ LANGUAGE PLPERLU;
270 INSERT INTO asset.call_number_class (name, normalizer, field) VALUES
271 ('Generic', 'asset.label_normalizer_generic', '050ab,055ab,060ab,070ab,080ab,082ab,086ab,088ab,090,092,096,098,099'),
272 ('Dewey (DDC)', 'asset.label_normalizer_dewey', '080ab,082ab'),
273 ('Library of Congress (LC)', 'asset.label_normalizer_lc', '050ab,055ab')
276 CREATE TABLE asset.call_number (
277 id bigserial PRIMARY KEY,
278 creator BIGINT NOT NULL,
279 create_date TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
280 editor BIGINT NOT NULL,
281 edit_date TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
282 record bigint NOT NULL,
283 owning_lib INT NOT NULL,
285 deleted BOOL NOT NULL DEFAULT FALSE,
286 label_class BIGINT DEFAULT 1 NOT NULL
287 REFERENCES asset.call_number_class(id)
288 DEFERRABLE INITIALLY DEFERRED,
291 CREATE INDEX asset_call_number_record_idx ON asset.call_number (record);
292 CREATE INDEX asset_call_number_creator_idx ON asset.call_number (creator);
293 CREATE INDEX asset_call_number_editor_idx ON asset.call_number (editor);
294 CREATE INDEX asset_call_number_dewey_idx ON asset.call_number (public.call_number_dewey(label));
295 CREATE INDEX asset_call_number_upper_label_id_owning_lib_idx ON asset.call_number (upper(label),id,owning_lib);
296 CREATE INDEX asset_call_number_label_sortkey ON asset.call_number(label_sortkey);
297 CREATE UNIQUE INDEX asset_call_number_label_once_per_lib ON asset.call_number (record, owning_lib, label) WHERE deleted = FALSE OR deleted IS FALSE;
298 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;
299 CREATE TRIGGER asset_label_sortkey_trigger
300 BEFORE UPDATE OR INSERT ON asset.call_number
301 FOR EACH ROW EXECUTE PROCEDURE asset.label_normalizer();
303 CREATE TABLE asset.uri_call_number_map (
304 id BIGSERIAL PRIMARY KEY,
305 uri INT NOT NULL REFERENCES asset.uri (id),
306 call_number INT NOT NULL REFERENCES asset.call_number (id),
307 CONSTRAINT uri_cn_once UNIQUE (uri,call_number)
309 CREATE INDEX asset_uri_call_number_map_cn_idx ON asset.uri_call_number_map (call_number);
311 CREATE TABLE asset.call_number_note (
312 id BIGSERIAL PRIMARY KEY,
313 call_number BIGINT NOT NULL,
314 creator BIGINT NOT NULL,
315 create_date TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
316 pub BOOL NOT NULL DEFAULT FALSE,
320 CREATE INDEX asset_call_number_note_creator_idx ON asset.call_number_note ( creator );
322 CREATE TABLE asset.copy_template (
323 id SERIAL PRIMARY KEY,
324 owning_lib INT NOT NULL
325 REFERENCES actor.org_unit (id)
326 DEFERRABLE INITIALLY DEFERRED,
327 creator BIGINT NOT NULL
328 REFERENCES actor.usr (id)
329 DEFERRABLE INITIALLY DEFERRED,
330 editor BIGINT NOT NULL
331 REFERENCES actor.usr (id)
332 DEFERRABLE INITIALLY DEFERRED,
333 create_date TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
334 edit_date TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
336 -- columns above this point are attributes of the template itself
337 -- columns after this point are attributes of the copy this template modifies/creates
338 circ_lib INT REFERENCES actor.org_unit (id)
339 DEFERRABLE INITIALLY DEFERRED,
340 status INT REFERENCES config.copy_status (id)
341 DEFERRABLE INITIALLY DEFERRED,
342 location INT REFERENCES asset.copy_location (id)
343 DEFERRABLE INITIALLY DEFERRED,
344 loan_duration INT CONSTRAINT valid_loan_duration CHECK (
345 loan_duration IS NULL OR loan_duration IN (1,2,3)),
346 fine_level INT CONSTRAINT valid_fine_level CHECK (
347 fine_level IS NULL OR loan_duration IN (1,2,3)),
353 deposit_amount NUMERIC(6,2),
363 CREATE VIEW stats.fleshed_copy AS
365 CAST(cp.create_date AS DATE) AS create_date_day,
366 CAST(cp.edit_date AS DATE) AS edit_date_day,
367 DATE_TRUNC('hour', cp.create_date) AS create_date_hour,
368 DATE_TRUNC('hour', cp.edit_date) AS edit_date_hour,
369 cn.label AS call_number_label,
375 JOIN asset.call_number cn ON (cp.call_number = cn.id)
376 JOIN metabib.rec_descriptor rd ON (rd.record = cn.record);
378 CREATE VIEW stats.fleshed_call_number AS
380 CAST(cn.create_date AS DATE) AS create_date_day,
381 CAST(cn.edit_date AS DATE) AS edit_date_day,
382 DATE_TRUNC('hour', cn.create_date) AS create_date_hour,
383 DATE_TRUNC('hour', cn.edit_date) AS edit_date_hour,
387 FROM asset.call_number cn
388 JOIN metabib.rec_descriptor rd ON (rd.record = cn.record);
390 CREATE OR REPLACE FUNCTION asset.opac_ou_record_copy_count (org INT, record BIGINT) RETURNS TABLE (depth INT, org_unit INT, visible BIGINT, available BIGINT, unshadow BIGINT, transcendant INT) AS $f$
395 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 = record;
397 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
402 SUM( CASE WHEN cp.status IN (0,7,12) THEN 1 ELSE 0 END ),
406 actor.org_unit_descendants(ans.id) d
407 JOIN asset.opac_visible_copies av ON (av.record = record AND av.circ_lib = d.id)
408 JOIN asset.copy cp ON (cp.id = av.id)
412 RETURN QUERY SELECT ans.depth, ans.id, 0::BIGINT, 0::BIGINT, 0::BIGINT, trans;
419 $f$ LANGUAGE PLPGSQL;
421 CREATE OR REPLACE FUNCTION asset.opac_lasso_record_copy_count (i_lasso INT, record BIGINT) RETURNS TABLE (depth INT, org_unit INT, visible BIGINT, available BIGINT, unshadow BIGINT, transcendant INT) AS $f$
426 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 = record;
428 FOR ans IN SELECT u.org_unit AS id FROM actor.org_lasso_map AS u WHERE lasso = i_lasso LOOP
433 SUM( CASE WHEN cp.status IN (0,7,12) THEN 1 ELSE 0 END ),
437 actor.org_unit_descendants(ans.id) d
438 JOIN asset.opac_visible_copies av ON (av.record = record AND av.circ_lib = d.id)
439 JOIN asset.copy cp ON (cp.id = av.id)
443 RETURN QUERY SELECT ans.depth, ans.id, 0::BIGINT, 0::BIGINT, 0::BIGINT, trans;
450 $f$ LANGUAGE PLPGSQL;
452 CREATE OR REPLACE FUNCTION asset.staff_ou_record_copy_count (org INT, record BIGINT) RETURNS TABLE (depth INT, org_unit INT, visible BIGINT, available BIGINT, unshadow BIGINT, transcendant INT) AS $f$
457 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 = record;
459 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
464 SUM( CASE WHEN cp.status IN (0,7,12) THEN 1 ELSE 0 END ),
468 actor.org_unit_descendants(ans.id) d
469 JOIN asset.copy cp ON (cp.circ_lib = d.id)
470 JOIN asset.call_number cn ON (cn.record = record AND cn.id = cp.call_number)
474 RETURN QUERY SELECT ans.depth, ans.id, 0::BIGINT, 0::BIGINT, 0::BIGINT, trans;
481 $f$ LANGUAGE PLPGSQL;
483 CREATE OR REPLACE FUNCTION asset.staff_lasso_record_copy_count (i_lasso INT, record BIGINT) RETURNS TABLE (depth INT, org_unit INT, visible BIGINT, available BIGINT, unshadow BIGINT, transcendant INT) AS $f$
488 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 = record;
490 FOR ans IN SELECT u.org_unit AS id FROM actor.org_lasso_map AS u WHERE lasso = i_lasso LOOP
495 SUM( CASE WHEN cp.status IN (0,7,12) THEN 1 ELSE 0 END ),
499 actor.org_unit_descendants(ans.id) d
500 JOIN asset.copy cp ON (cp.circ_lib = d.id)
501 JOIN asset.call_number cn ON (cn.record = record AND cn.id = cp.call_number)
505 RETURN QUERY SELECT ans.depth, ans.id, 0::BIGINT, 0::BIGINT, 0::BIGINT, trans;
512 $f$ LANGUAGE PLPGSQL;
514 CREATE OR REPLACE FUNCTION asset.record_copy_count ( place INT, record BIGINT, staff BOOL) RETURNS TABLE (depth INT, org_unit INT, visible BIGINT, available BIGINT, unshadow BIGINT, transcendant INT) AS $f$
516 IF staff IS TRUE THEN
518 RETURN QUERY SELECT * FROM asset.staff_ou_record_copy_count( place, record );
520 RETURN QUERY SELECT * FROM asset.staff_lasso_record_copy_count( -place, record );
524 RETURN QUERY SELECT * FROM asset.opac_ou_record_copy_count( place, record );
526 RETURN QUERY SELECT * FROM asset.opac_lasso_record_copy_count( -place, record );
532 $f$ LANGUAGE PLPGSQL;
534 CREATE OR REPLACE FUNCTION asset.opac_ou_metarecord_copy_count (org INT, record BIGINT) RETURNS TABLE (depth INT, org_unit INT, visible BIGINT, available BIGINT, unshadow BIGINT, transcendant INT) AS $f$
539 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 = record;
541 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
546 SUM( CASE WHEN cp.status IN (0,7,12) THEN 1 ELSE 0 END ),
550 actor.org_unit_descendants(ans.id) d
551 JOIN asset.opac_visible_copies av ON (av.record = record AND av.circ_lib = d.id)
552 JOIN asset.copy cp ON (cp.id = av.id)
553 JOIN metabib.metarecord_source_map m ON (m.source = av.record)
557 RETURN QUERY SELECT ans.depth, ans.id, 0::BIGINT, 0::BIGINT, 0::BIGINT, trans;
564 $f$ LANGUAGE PLPGSQL;
566 CREATE OR REPLACE FUNCTION asset.opac_lasso_metarecord_copy_count (i_lasso INT, record BIGINT) RETURNS TABLE (depth INT, org_unit INT, visible BIGINT, available BIGINT, unshadow BIGINT, transcendant INT) AS $f$
571 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 = record;
573 FOR ans IN SELECT u.org_unit AS id FROM actor.org_lasso_map AS u WHERE lasso = i_lasso LOOP
578 SUM( CASE WHEN cp.status IN (0,7,12) THEN 1 ELSE 0 END ),
582 actor.org_unit_descendants(ans.id) d
583 JOIN asset.opac_visible_copies av ON (av.record = record AND av.circ_lib = d.id)
584 JOIN asset.copy cp ON (cp.id = av.id)
585 JOIN metabib.metarecord_source_map m ON (m.source = av.record)
589 RETURN QUERY SELECT ans.depth, ans.id, 0::BIGINT, 0::BIGINT, 0::BIGINT, trans;
596 $f$ LANGUAGE PLPGSQL;
598 CREATE OR REPLACE FUNCTION asset.staff_ou_metarecord_copy_count (org INT, record BIGINT) RETURNS TABLE (depth INT, org_unit INT, visible BIGINT, available BIGINT, unshadow BIGINT, transcendant INT) AS $f$
603 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 = record;
605 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
610 SUM( CASE WHEN cp.status IN (0,7,12) THEN 1 ELSE 0 END ),
614 actor.org_unit_descendants(ans.id) d
615 JOIN asset.copy cp ON (cp.circ_lib = d.id)
616 JOIN asset.call_number cn ON (cn.record = record AND cn.id = cp.call_number)
617 JOIN metabib.metarecord_source_map m ON (m.source = cn.record)
621 RETURN QUERY SELECT ans.depth, ans.id, 0::BIGINT, 0::BIGINT, 0::BIGINT, trans;
628 $f$ LANGUAGE PLPGSQL;
630 CREATE OR REPLACE FUNCTION asset.staff_lasso_metarecord_copy_count (i_lasso INT, record BIGINT) RETURNS TABLE (depth INT, org_unit INT, visible BIGINT, available BIGINT, unshadow BIGINT, transcendant INT) AS $f$
635 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 = record;
637 FOR ans IN SELECT u.org_unit AS id FROM actor.org_lasso_map AS u WHERE lasso = i_lasso LOOP
642 SUM( CASE WHEN cp.status IN (0,7,12) THEN 1 ELSE 0 END ),
646 actor.org_unit_descendants(ans.id) d
647 JOIN asset.copy cp ON (cp.circ_lib = d.id)
648 JOIN asset.call_number cn ON (cn.record = record AND cn.id = cp.call_number)
649 JOIN metabib.metarecord_source_map m ON (m.source = cn.record)
653 RETURN QUERY SELECT ans.depth, ans.id, 0::BIGINT, 0::BIGINT, 0::BIGINT, trans;
660 $f$ LANGUAGE PLPGSQL;
662 CREATE OR REPLACE FUNCTION asset.metarecord_copy_count ( place INT, record BIGINT, staff BOOL) RETURNS TABLE (depth INT, org_unit INT, visible BIGINT, available BIGINT, unshadow BIGINT, transcendant INT) AS $f$
664 IF staff IS TRUE THEN
666 RETURN QUERY SELECT * FROM asset.staff_ou_metarecord_copy_count( place, record );
668 RETURN QUERY SELECT * FROM asset.staff_lasso_metarecord_copy_count( -place, record );
672 RETURN QUERY SELECT * FROM asset.opac_ou_metarecord_copy_count( place, record );
674 RETURN QUERY SELECT * FROM asset.opac_lasso_metarecord_copy_count( -place, record );
680 $f$ LANGUAGE PLPGSQL;