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