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' ));
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 $$
13 rec vandelay.bib_match%ROWTYPE;
18 max_copy_count INT := 0;
21 PERFORM * FROM vandelay.queued_bib_record WHERE import_time IS NOT NULL AND id = import_id;
24 -- RAISE NOTICE 'already imported, cannot auto-overlay'
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;
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
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;
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
60 WHERE id IN (SELECT * FROM UNNEST(scope_orgs))
61 AND parent_ou IS NOT NULL;
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
75 RETURN vandelay.overlay_bib_record( import_id, eg_id, merge_profile_id );