]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/tests/datasets/sql/transactions.sql
LP 1198465: Load negative balance test transactions in load_all.sql
[working/Evergreen.git] / Open-ILS / tests / datasets / sql / transactions.sql
1
2 /**
3  * For the first (by ID) 20 users in each Patron group and the first 3 in 
4  * each Staff group:
5  *
6  * 1. create 3 regular circs w/ varying (stock) rules and 3 overdue
7  * circs. 
8  *
9  * 2. create 2 regular title holds and one frozen title hold.  If the user
10  * is in a Staff group, also create one copy-level hold.
11  *
12  */
13
14 /**
15  * NOTE: The fine generator and hold targeter should be run after this is
16  * loaded to creating overdue billings and target copies for holds.
17  */
18
19 DO $$                                                                           
20     DECLARE grp INTEGER;                                                      
21     DECLARE recipient actor.usr%ROWTYPE;
22     DECLARE copy asset.copy%ROWTYPE;
23     DECLARE bre biblio.record_entry%ROWTYPE;
24     DECLARE user_count INTEGER;
25     DECLARE requestor INTEGER;
26 BEGIN                                                                           
27
28     copy := evergreen.next_copy(0);
29     bre := evergreen.next_bib(0);
30
31     FOR grp IN SELECT id FROM permission.grp_tree WHERE id > 1 ORDER BY id LOOP
32
33         IF 2 IN (SELECT id FROM permission.grp_ancestors(grp)) THEN
34             -- patron group
35             user_count := 50;
36         ELSE
37             user_count := 3;
38         END IF;
39
40         FOR recipient IN SELECT * FROM actor.usr 
41             WHERE NOT deleted AND profile = grp 
42                 AND expire_date > NOW() - '1 month'::interval
43                 ORDER BY id LIMIT user_count LOOP
44
45             -- find a suitable circulator/requestor for these transactions
46             SELECT INTO requestor id 
47                 FROM actor.usr 
48                 WHERE home_ou = recipient.home_ou AND
49                       profile = 5 AND -- Circulators
50                       NOT deleted
51                       AND expire_date > NOW()
52                 ORDER BY id LIMIT 1;
53
54             -- regular circs --------------------------------
55
56             copy := evergreen.next_copy(copy.id);
57             EXIT WHEN copy IS NULL;
58             PERFORM evergreen.populate_circ(
59                 recipient.id, requestor, copy.id, copy.circ_lib,
60                 'default', 'default', 'default', FALSE
61             );
62
63             copy := evergreen.next_copy(copy.id);
64             EXIT WHEN copy IS NULL;
65             PERFORM evergreen.populate_circ(
66                 recipient.id, requestor, copy.id, copy.circ_lib,
67                 '1_hour_2_renew', 'default', 'overdue_min', FALSE
68             );
69
70             copy := evergreen.next_copy(copy.id);
71             EXIT WHEN copy IS NULL;
72             PERFORM evergreen.populate_circ(
73                 recipient.id, requestor, copy.id, copy.circ_lib,
74                 '7_days_0_renew', 'default', 'overdue_max', FALSE
75             );
76
77             -- overdue circs ----------------------------------
78
79             copy := evergreen.next_copy(copy.id);
80             EXIT WHEN copy IS NULL;
81             PERFORM evergreen.populate_circ(
82                 recipient.id, requestor, copy.id, copy.circ_lib,
83                 'default', 'default', 'default', TRUE
84             );
85
86             copy := evergreen.next_copy(copy.id);
87             EXIT WHEN copy IS NULL;
88             PERFORM evergreen.populate_circ(
89                 recipient.id, requestor, copy.id, copy.circ_lib,
90                 '1_hour_2_renew', 'default', 'overdue_min', TRUE
91             );
92
93             copy := evergreen.next_copy(copy.id);
94             EXIT WHEN copy IS NULL;
95             PERFORM evergreen.populate_circ(
96                 recipient.id, requestor, copy.id, copy.circ_lib,
97                 '7_days_0_renew', 'default', 'overdue_max', TRUE
98             );
99
100             -- holds ------------------------------------------
101
102             -- title hold
103             bre := evergreen.next_bib(bre.id);
104             EXIT WHEN bre IS NULL;
105             PERFORM evergreen.populate_hold(
106                 'T', bre.id, recipient.id, recipient.id,
107                 recipient.home_ou, FALSE, NULL
108             );
109
110             -- title hold, circulator-placed 
111             bre := evergreen.next_bib(bre.id);
112             EXIT WHEN bre IS NULL;
113             PERFORM evergreen.populate_hold(
114                 'T', bre.id, recipient.id, requestor,
115                 recipient.home_ou, FALSE, NULL
116             );
117
118             -- frozen title hold
119             bre := evergreen.next_bib(bre.id);
120             EXIT WHEN bre IS NULL;
121             PERFORM evergreen.populate_hold(
122                 'T', bre.id, recipient.id, recipient.id,
123                 recipient.home_ou,
124                 TRUE, NOW() + '3 months'::INTERVAL
125             );
126
127             -- Staff accounts get a copy-level hold
128             IF 3 IN (SELECT id FROM permission.grp_ancestors(grp)) THEN
129                 copy := evergreen.next_copy(copy.id);
130                 EXIT WHEN copy IS NULL;
131                 PERFORM evergreen.populate_hold(
132                     'C', copy.id, recipient.id, recipient.id,
133                     recipient.home_ou, FALSE, NULL
134                 );
135             END IF;
136
137         END LOOP;                                                                   
138     END LOOP;                                                                   
139 END $$;
140
141