]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0406.schema.hold-matrix.user-not-requestor.sql
LP#1917826: add release notes entry
[Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0406.schema.hold-matrix.user-not-requestor.sql
1 BEGIN;
2
3 INSERT INTO config.upgrade_log (version) VALUES ('0406'); -- miker
4
5 INSERT INTO config.global_flag (name, label, enabled)
6     VALUES (
7         'circ.holds.usr_not_requestor',
8         oils_i18n_gettext(
9             'circ.holds.usr_not_requestor',
10             'Holds: When testing hold matrix matchpoints, use the profile group of the receiving user instead of that of the requestor (affects staff-placed holds)',
11             'cgf',
12             'label'
13         ),
14         TRUE
15     );
16
17 CREATE OR REPLACE FUNCTION action.find_hold_matrix_matchpoint( pickup_ou INT, request_ou INT, match_item BIGINT, match_user INT, match_requestor INT ) RETURNS INT AS $func$
18 DECLARE
19     current_requestor_group    permission.grp_tree%ROWTYPE;
20     root_ou            actor.org_unit%ROWTYPE;
21     requestor_object    actor.usr%ROWTYPE;
22     user_object        actor.usr%ROWTYPE;
23     item_object        asset.copy%ROWTYPE;
24     item_cn_object        asset.call_number%ROWTYPE;
25     rec_descriptor        metabib.rec_descriptor%ROWTYPE;
26     current_mp_weight    FLOAT;
27     matchpoint_weight    FLOAT;
28     tmp_weight        FLOAT;
29     current_mp        config.hold_matrix_matchpoint%ROWTYPE;
30     matchpoint        config.hold_matrix_matchpoint%ROWTYPE;
31 BEGIN
32     SELECT INTO root_ou * FROM actor.org_unit WHERE parent_ou IS NULL;
33     SELECT INTO user_object * FROM actor.usr WHERE id = match_user;
34     SELECT INTO requestor_object * FROM actor.usr WHERE id = match_requestor;
35     SELECT INTO item_object * FROM asset.copy WHERE id = match_item;
36     SELECT INTO item_cn_object * FROM asset.call_number WHERE id = item_object.call_number;
37     SELECT INTO rec_descriptor r.* FROM metabib.rec_descriptor r WHERE r.record = item_cn_object.record;
38
39     PERFORM * FROM config.internal_flag WHERE name = 'circ.holds.usr_not_requestor' AND enabled;
40
41     IF NOT FOUND THEN
42         SELECT INTO current_requestor_group * FROM permission.grp_tree WHERE id = requestor_object.profile;
43     ELSE
44         SELECT INTO current_requestor_group * FROM permission.grp_tree WHERE id = user_object.profile;
45     END IF;
46
47     LOOP 
48         -- for each potential matchpoint for this ou and group ...
49         FOR current_mp IN
50             SELECT    m.*
51               FROM    config.hold_matrix_matchpoint m
52               WHERE    m.requestor_grp = current_requestor_group.id AND m.active
53               ORDER BY    CASE WHEN m.circ_modifier    IS NOT NULL THEN 16 ELSE 0 END +
54                     CASE WHEN m.juvenile_flag    IS NOT NULL THEN 16 ELSE 0 END +
55                     CASE WHEN m.marc_type        IS NOT NULL THEN 8 ELSE 0 END +
56                     CASE WHEN m.marc_form        IS NOT NULL THEN 4 ELSE 0 END +
57                     CASE WHEN m.marc_vr_format    IS NOT NULL THEN 2 ELSE 0 END +
58                     CASE WHEN m.ref_flag        IS NOT NULL THEN 1 ELSE 0 END DESC LOOP
59
60             current_mp_weight := 5.0;
61
62             IF current_mp.circ_modifier IS NOT NULL THEN
63                 CONTINUE WHEN current_mp.circ_modifier <> item_object.circ_modifier OR item_object.circ_modifier IS NULL;
64             END IF;
65
66             IF current_mp.marc_type IS NOT NULL THEN
67                 IF item_object.circ_as_type IS NOT NULL THEN
68                     CONTINUE WHEN current_mp.marc_type <> item_object.circ_as_type;
69                 ELSE
70                     CONTINUE WHEN current_mp.marc_type <> rec_descriptor.item_type;
71                 END IF;
72             END IF;
73
74             IF current_mp.marc_form IS NOT NULL THEN
75                 CONTINUE WHEN current_mp.marc_form <> rec_descriptor.item_form;
76             END IF;
77
78             IF current_mp.marc_vr_format IS NOT NULL THEN
79                 CONTINUE WHEN current_mp.marc_vr_format <> rec_descriptor.vr_format;
80             END IF;
81
82             IF current_mp.juvenile_flag IS NOT NULL THEN
83                 CONTINUE WHEN current_mp.juvenile_flag <> user_object.juvenile;
84             END IF;
85
86             IF current_mp.ref_flag IS NOT NULL THEN
87                 CONTINUE WHEN current_mp.ref_flag <> item_object.ref;
88             END IF;
89
90
91             -- caclulate the rule match weight
92             IF current_mp.item_owning_ou IS NOT NULL AND current_mp.item_owning_ou <> root_ou.id THEN
93                 SELECT INTO tmp_weight 1.0 / (actor.org_unit_proximity(current_mp.item_owning_ou, item_cn_object.owning_lib)::FLOAT + 1.0)::FLOAT;
94                 current_mp_weight := current_mp_weight - tmp_weight;
95             END IF; 
96
97             IF current_mp.item_circ_ou IS NOT NULL AND current_mp.item_circ_ou <> root_ou.id THEN
98                 SELECT INTO tmp_weight 1.0 / (actor.org_unit_proximity(current_mp.item_circ_ou, item_object.circ_lib)::FLOAT + 1.0)::FLOAT;
99                 current_mp_weight := current_mp_weight - tmp_weight;
100             END IF; 
101
102             IF current_mp.pickup_ou IS NOT NULL AND current_mp.pickup_ou <> root_ou.id THEN
103                 SELECT INTO tmp_weight 1.0 / (actor.org_unit_proximity(current_mp.pickup_ou, pickup_ou)::FLOAT + 1.0)::FLOAT;
104                 current_mp_weight := current_mp_weight - tmp_weight;
105             END IF; 
106
107             IF current_mp.request_ou IS NOT NULL AND current_mp.request_ou <> root_ou.id THEN
108                 SELECT INTO tmp_weight 1.0 / (actor.org_unit_proximity(current_mp.request_ou, request_ou)::FLOAT + 1.0)::FLOAT;
109                 current_mp_weight := current_mp_weight - tmp_weight;
110             END IF; 
111
112             IF current_mp.user_home_ou IS NOT NULL AND current_mp.user_home_ou <> root_ou.id THEN
113                 SELECT INTO tmp_weight 1.0 / (actor.org_unit_proximity(current_mp.user_home_ou, user_object.home_ou)::FLOAT + 1.0)::FLOAT;
114                 current_mp_weight := current_mp_weight - tmp_weight;
115             END IF; 
116
117             -- set the matchpoint if we found the best one
118             IF matchpoint_weight IS NULL OR matchpoint_weight > current_mp_weight THEN
119                 matchpoint = current_mp;
120                 matchpoint_weight = current_mp_weight;
121             END IF;
122
123         END LOOP;
124
125         EXIT WHEN current_requestor_group.parent IS NULL OR matchpoint.id IS NOT NULL;
126
127         SELECT INTO current_requestor_group * FROM permission.grp_tree WHERE id = current_requestor_group.parent;
128     END LOOP;
129
130     RETURN matchpoint.id;
131 END;
132 $func$ LANGUAGE plpgsql;
133
134 COMMIT;
135