]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/1112.schema.lp1775216_consistent_avail_counts.sql
b4d36c53e752fbb3034463783cb0d5813b9212d4
[working/Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 1112.schema.lp1775216_consistent_avail_counts.sql
1 CREATE OR REPLACE FUNCTION asset.staff_ou_record_copy_count(org integer, rid bigint)
2  RETURNS TABLE(depth integer, org_unit integer, visible bigint, available bigint, unshadow bigint, transcendant integer)
3  LANGUAGE plpgsql
4 AS $function$
5 DECLARE
6     ans RECORD;
7     trans INT;
8 BEGIN
9     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;
10
11     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
12         RETURN QUERY
13         WITH available_statuses AS (SELECT ARRAY_AGG(id) AS ids FROM config.copy_status WHERE is_available)
14         SELECT  ans.depth,
15                 ans.id,
16                 COUNT( cp.id ),
17                 SUM( (cp.status = ANY (available_statuses.ids))::INT ),
18                 SUM( CASE WHEN cl.opac_visible AND cp.opac_visible THEN 1 ELSE 0 END),
19                 trans
20           FROM
21                 available_statuses,
22                 actor.org_unit_descendants(ans.id) d
23                 JOIN asset.copy cp ON (cp.circ_lib = d.id AND NOT cp.deleted)
24                 JOIN asset.copy_location cl ON (cp.location = cl.id AND NOT cl.deleted)
25                 JOIN asset.call_number cn ON (cn.record = rid AND cn.id = cp.call_number AND NOT cn.deleted)
26           GROUP BY 1,2,6;
27
28         IF NOT FOUND THEN
29             RETURN QUERY SELECT ans.depth, ans.id, 0::BIGINT, 0::BIGINT, 0::BIGINT, trans;
30         END IF;
31
32     END LOOP;
33
34     RETURN;
35 END;
36 $function$;
37
38 CREATE OR REPLACE FUNCTION asset.opac_ou_record_copy_count(org integer, rid bigint)
39  RETURNS TABLE(depth integer, org_unit integer, visible bigint, available bigint, unshadow bigint, transcendant integer)
40  LANGUAGE plpgsql
41 AS $function$
42 DECLARE
43     ans RECORD;
44     trans INT;
45 BEGIN
46     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;
47
48     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
49         RETURN QUERY
50         WITH org_list AS (SELECT ARRAY_AGG(id)::BIGINT[] AS orgs FROM actor.org_unit_descendants(ans.id) x),
51              available_statuses AS (SELECT ARRAY_AGG(id) AS ids FROM config.copy_status WHERE is_available),
52              mask AS (SELECT c_attrs FROM asset.patron_default_visibility_mask() x)
53         SELECT  ans.depth,
54                 ans.id,
55                 COUNT( av.id ),
56                 SUM( (cp.status = ANY (available_statuses.ids))::INT ),
57                 COUNT( av.id ),
58                 trans
59           FROM  mask,
60                 available_statuses,
61                 org_list,
62                 asset.copy_vis_attr_cache av
63                 JOIN asset.copy cp ON (cp.id = av.target_copy AND av.record = rid)
64                 JOIN asset.call_number cn ON (cp.call_number = cn.id AND not cn.deleted)
65           WHERE cp.circ_lib = ANY (org_list.orgs) AND av.vis_attr_vector @@ mask.c_attrs::query_int
66           GROUP BY 1,2,6;
67
68         IF NOT FOUND THEN
69             RETURN QUERY SELECT ans.depth, ans.id, 0::BIGINT, 0::BIGINT, 0::BIGINT, trans;
70         END IF;
71
72     END LOOP;
73
74     RETURN;
75 END;
76 $function$;
77