]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/1182.schema.vandelay.auto_overlay_org_unit_copies.sql
13816a30bcb6480b804e5ffc4a7a61ddd64db62c
[working/Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 1182.schema.vandelay.auto_overlay_org_unit_copies.sql
1 BEGIN;
2
3 SELECT evergreen.upgrade_deps_block_check('1182', :eg_version);
4
5 INSERT INTO permission.perm_list ( id, code, description ) VALUES
6  ( 616, 'IMPORT_USE_ORG_UNIT_COPIES', oils_i18n_gettext( 616,
7     'Allows users to import records based on the number of org unit copies attached to a record', 'ppl', 'description' )),
8  ( 617, 'IMPORT_ON_ORDER_CAT_COPY', oils_i18n_gettext( 617,
9     'Allows users to import copies based on the on-order items attached to a record', 'ppl', 'description' ));
10
11 CREATE OR REPLACE FUNCTION vandelay.auto_overlay_org_unit_copies ( import_id BIGINT, merge_profile_id INT, lwm_ratio_value_p NUMERIC ) RETURNS BOOL AS $$
12 DECLARE
13     eg_id           BIGINT;
14     match_count     INT;
15     rec             vandelay.bib_match%ROWTYPE;
16     v_owning_lib    INT;
17     scope_org       INT;
18     scope_orgs      INT[];
19     copy_count      INT := 0;
20     max_copy_count  INT := 0;
21 BEGIN
22
23     PERFORM * FROM vandelay.queued_bib_record WHERE import_time IS NOT NULL AND id = import_id;
24
25     IF FOUND THEN
26         -- RAISE NOTICE 'already imported, cannot auto-overlay'
27         RETURN FALSE;
28     END IF;
29
30     -- Gather all the owning libs for our import items.
31     -- These are our initial scope_orgs.
32     SELECT ARRAY_AGG(DISTINCT owning_lib) INTO scope_orgs
33         FROM vandelay.import_item
34         WHERE queued_record = import_id;
35
36     WHILE CARDINALITY(scope_orgs) > 0 LOOP
37         FOR scope_org IN SELECT * FROM UNNEST(scope_orgs) LOOP
38             -- For each match, get a count of all copies at descendants of our scope org.
39             FOR rec IN SELECT * FROM vandelay.bib_match AS vbm
40                 WHERE queued_record = import_id
41                 ORDER BY vbm.eg_record DESC
42             LOOP
43                 SELECT COUNT(acp.id) INTO copy_count
44                     FROM asset.copy AS acp
45                     INNER JOIN asset.call_number AS acn
46                         ON acp.call_number = acn.id
47                     WHERE acn.owning_lib IN (SELECT id FROM
48                         actor.org_unit_descendants(scope_org))
49                     AND acn.record = rec.eg_record
50                     AND acp.deleted = FALSE;
51                 IF copy_count > max_copy_count THEN
52                     max_copy_count := copy_count;
53                     eg_id := rec.eg_record;
54                 END IF;
55             END LOOP;
56         END LOOP;
57
58         -- If no matching bibs had holdings, gather our next set of orgs to check, and iterate.
59         IF max_copy_count = 0 THEN 
60             SELECT ARRAY_AGG(DISTINCT parent_ou) INTO scope_orgs
61                 FROM actor.org_unit
62                 WHERE id IN (SELECT * FROM UNNEST(scope_orgs))
63                 AND parent_ou IS NOT NULL;
64         END IF;
65     END LOOP;
66
67     IF eg_id IS NULL THEN
68         -- Could not determine best match via copy count
69         -- fall back to default best match
70         IF (SELECT * FROM vandelay.auto_overlay_bib_record_with_best( import_id, merge_profile_id, lwm_ratio_value_p )) THEN
71             RETURN TRUE;
72         ELSE
73             RETURN FALSE;
74         END IF;
75     END IF;
76
77     RETURN vandelay.overlay_bib_record( import_id, eg_id, merge_profile_id );
78 END;
79 $$ LANGUAGE PLPGSQL;
80
81 COMMIT;