3 SELECT evergreen.upgrade_deps_block_check('1182', :eg_version);
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' ));
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 $$
15 rec vandelay.bib_match%ROWTYPE;
20 max_copy_count INT := 0;
23 PERFORM * FROM vandelay.queued_bib_record WHERE import_time IS NOT NULL AND id = import_id;
26 -- RAISE NOTICE 'already imported, cannot auto-overlay'
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;
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
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;
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
62 WHERE id IN (SELECT * FROM UNNEST(scope_orgs))
63 AND parent_ou IS NOT NULL;
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
77 RETURN vandelay.overlay_bib_record( import_id, eg_id, merge_profile_id );