]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/tests/datasets/sql/assets_extras.sql
LP#1155771 Copy location sample data additions
[working/Evergreen.git] / Open-ILS / tests / datasets / sql / assets_extras.sql
1 -- copy locations
2 -- copy location groups
3 -- copy stat cats
4 -- ...
5
6 -- TODO:
7 -- This applies locations to copies without regard for the type
8 -- of record of each copy.  This leads to nonsensical copy / location 
9 -- pairings.  (E.g. a violin concerto copy with a "Newspapers" location).  
10 -- An improvement would be to create locations first, then select
11 -- locations for copies as they are inserted.  Time will tell if there will
12 -- ever be a large enough variety of data to do this in a meaningful way.
13 -- Also, it's kind of a pain, so, maybe later..
14
15 INSERT INTO asset.copy_location (owning_lib, name) VALUES
16 (1, 'Reference'),
17 (4, 'Reference'),
18 (4, 'Easy Reader'),
19 (5, 'Easy Reader'),
20 (6, 'Easy Reader'),
21 (2, 'Fiction'),
22 (3, 'Fiction'),
23 (2, 'Non-Fiction'),
24 (3, 'Non-Fiction'),
25 (2, 'Juvenile Non-Fiction'),
26 (3, 'Juvenile Non-Fiction'),
27 (2, 'Young Adult Fiction'),
28 (4, 'Young Adult Fiction'),
29 (5, 'Young Adult Fiction'),
30 (6, 'Young Adult Fiction'),
31 (2, 'Children''s Fiction'),
32 (3, 'Children''s Fiction'),
33 (4, 'Audio/Video'),
34 (5, 'Audio/Video'),
35 (5, 'Music'),
36 (6, 'Music'),
37 (6, 'Audio/Video'),
38 (4, 'Science Fiction'),
39 (7, 'Science Fiction'),
40 (5, 'Genealogy'),
41 (6, 'Genealogy'),
42 (4, 'Biography'),
43 (5, 'Biography'),
44 (6, 'Local History'),
45 (7, 'Local History'),
46 (6, 'Federal Documents');
47
48
49 -- non-holdable, non-circulating
50 INSERT INTO asset.copy_location 
51     (owning_lib, name, holdable, circulate) VALUES
52 (4, 'Periodicals', FALSE, FALSE),
53 (6, 'Periodicals', FALSE, FALSE),
54 (5, 'Magazines', FALSE, FALSE),
55 (7, 'Magazines', FALSE, FALSE),
56 (4, 'Newspapers', FALSE, FALSE);
57
58 -- non-holdable
59 INSERT INTO asset.copy_location (owning_lib, name, holdable) VALUES
60 (4, 'Reserves', FALSE),
61 (5, 'Reserves', FALSE),
62 (3, 'Reserves', FALSE),
63 (5, 'Theses', FALSE),
64 (7, 'Theses', FALSE),
65 (2, 'Special Collections', FALSE),
66 (6, 'Special Collections', FALSE),
67 (7, 'Special Collections', FALSE);
68
69
70 -- different settings per org level
71 INSERT INTO asset.copy_location
72     (owning_lib, name, holdable, opac_visible, circulate) VALUES
73 (2, 'Display', FALSE, FALSE, TRUE),
74 (4, 'Display', FALSE, FALSE, FALSE),
75 (5, 'Display', TRUE, FALSE, FALSE),
76 (6, 'Display', TRUE, FALSE, FALSE),
77 (7, 'Display', FALSE, FALSE, FALSE),
78 (1, 'New Arrivals', TRUE, TRUE, TRUE),
79 (2, 'New Arrivals', FALSE, TRUE, TRUE),
80 (4, 'New Arrivals', TRUE, TRUE, FALSE),
81 (5, 'New Arrivals', TRUE, TRUE, TRUE);
82
83 -- copy location groups
84
85 INSERT INTO asset.copy_location_group (name, owner) 
86     VALUES ('Juvenile Collection', 2);
87
88 INSERT INTO asset.copy_location_group_map (lgroup, location)
89     SELECT CURRVAL('asset.copy_location_group_id_seq'), id
90         FROM asset.copy_location 
91         WHERE owning_lib IN (2, 4, 5) AND 
92             opac_visible AND 
93             name IN (
94                 'Young Adult Fiction', 
95                 'Children''s Fiction',
96                 'Easy Reader', 
97                 'Juvenile Non-Fiction'
98             );
99
100 INSERT INTO asset.copy_location_group (name, owner) 
101     VALUES ('Local Interest Collection', 3);
102
103 INSERT INTO asset.copy_location_group_map (lgroup, location)
104     SELECT CURRVAL('asset.copy_location_group_id_seq'), id
105         FROM asset.copy_location 
106         WHERE owning_lib IN (3, 6, 7) AND 
107             opac_visible AND
108             name IN (
109                 'Geneology',
110                 'Special Collections',
111                 'Local History'
112             );
113
114 -- Distribute copies evenly across copy locations whose owning_lib
115 -- matches the copy circ lib.  To provide some level of repeatable
116 -- outcome, we loop instead of applying locations at randon within
117 -- a given owning_lib.
118 DO $$
119     DECLARE cur_loc INTEGER;
120     DECLARE cur_copy asset.copy%ROWTYPE;
121     DECLARE cur_cn INTEGER;
122 BEGIN
123     cur_loc := 0;
124     cur_cn := 0;
125
126     FOR cur_copy IN SELECT * FROM asset.copy 
127             WHERE location = 1 ORDER BY circ_lib, call_number, id LOOP
128
129         -- Move to the next copy location if we are changing call numbers.
130         -- This provides some visual consistency between call numbers and
131         -- copy locations and helps avoid having practically every copy in
132         -- view residing in a different location.
133         IF cur_cn <> cur_copy.call_number THEN
134
135             -- find the next location for the current copy's circ lib
136             SELECT INTO cur_loc id FROM asset.copy_location 
137                 WHERE owning_lib = cur_copy.circ_lib AND id > cur_loc 
138                 ORDER BY id LIMIT 1;
139
140             IF NOT FOUND THEN
141                 -- start back over at the front of the list
142                 cur_loc := 0;
143                 SELECT INTO cur_loc id FROM asset.copy_location 
144                     WHERE owning_lib = cur_copy.circ_lib AND id > cur_loc 
145                     ORDER BY id LIMIT 1;
146             END IF;
147
148             IF NOT FOUND THEN
149                 -- no copy location at this circ lib, leave the default (1)
150                 CONTINUE;
151             END IF;
152
153             cur_cn := cur_copy.call_number;
154         END IF;
155
156         UPDATE asset.copy SET location = cur_loc WHERE id = cur_copy.id;
157     END LOOP;
158 END $$;
159