]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/action.pm
adding "nearest_hold" select
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Storage / Publisher / action.pm
1 package OpenILS::Application::Storage::Publisher::action;
2 use base qw/OpenILS::Application::Storage/;
3 use OpenSRF::Utils::Logger qw/:level/;
4 my $log = 'OpenSRF::Utils::Logger';
5
6 sub grab_overdue {
7         my $self = shift;
8         my $client = shift;
9         my $grace = shift || '';
10
11         my $c_t = action::circulation->table;
12
13         $grace = " - ($grace * (fine_interval))" if ($grace);
14
15         my $sql = <<"   SQL";
16                 SELECT  *
17                   FROM  $c_t
18                   WHERE stop_fines IS NULL
19                         AND due_date < ( CURRENT_TIMESTAMP $grace)
20         SQL
21
22         my $sth = action::circulation->db_Main->prepare_cached($sql);
23         $sth->execute;
24
25         $client->respond( $_->to_fieldmapper ) for ( map { action::circulation->construct($_) } $sth->fetchall_hash );
26
27         return undef;
28
29 }
30 __PACKAGE__->register_method(
31         api_name        => 'open-ils.storage.action.circulation.overdue',
32         api_level       => 1,
33         stream          => 1,
34         method          => 'grab_overdue',
35 );
36
37 sub nearest_hold {
38         my $self = shift;
39         my $client = shift;
40         my $pl = shift;
41         my $cp = shift;
42
43         my ($id) = action::hold_request->db_Main->selectrow_array(<<"   SQL", {}, $pl,$cp);
44                 SELECT  h.id
45                   FROM  action.hold_request h
46                         JOIN action.hold_copy_map hm ON (hm.hold = h.id)
47                   WHERE h.pickup_lib = ?
48                         AND hm.target_copy = ?
49                         AND h.capture_time IS NULL
50                 ORDER BY h.pickup_lib - (SELECT home_ou FROM actor.usr a WHERE a.id = h.usr), h.request_time
51         SQL
52         return $id;
53 }
54 __PACKAGE__->register_method(
55         api_name        => 'open-ils.storage.action.hold_request.nearest_hold',
56         api_level       => 1,
57         method          => 'nearest_hold',
58 );
59
60 sub next_resp_group_id {
61         my $self = shift;
62         my $client = shift;
63
64         # XXX This is not replication safe!!!
65
66         my ($id) = action::survey->db_Main->selectrow_array(<<" SQL");
67                 SELECT NEXTVAL('action.survey_response_group_id_seq'::TEXT)
68         SQL
69         return $id;
70 }
71 __PACKAGE__->register_method(
72         api_name        => 'open-ils.storage.action.survey_response.next_group_id',
73         api_level       => 1,
74         method          => 'next_resp_group_id',
75 );
76
77 sub patron_circ_summary {
78         my $self = shift;
79         my $client = shift;
80         my $id = ''.shift();
81
82         return undef unless ($id);
83         my $c_table = action::circulation->table;
84         my $b_table = money::billing->table;
85
86         my $select = <<"        SQL";
87                 SELECT  COUNT(DISTINCT c.id), SUM( COALESCE(b.amount,0) )
88                   FROM  $c_table c
89                         LEFT OUTER JOIN $b_table b ON (c.id = b.xact)
90                   WHERE c.usr = ?
91                         AND c.xact_finish IS NULL
92                         AND c.stop_fines NOT IN ('CLAIMSRETURNED','LOST')
93         SQL
94
95         return action::survey->db_Main->selectrow_arrayref($select, {}, $id);
96 }
97 __PACKAGE__->register_method(
98         api_name        => 'open-ils.storage.action.circulation.patron_summary',
99         api_level       => 1,
100         method          => 'patron_circ_summary',
101 );
102
103 #XXX Fix stored proc calls
104 sub find_local_surveys {
105         my $self = shift;
106         my $client = shift;
107         my $ou = ''.shift();
108
109         return undef unless ($ou);
110         my $s_table = action::survey->table;
111
112         my $select = <<"        SQL";
113                 SELECT  s.*
114                   FROM  $s_table s
115                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
116                   WHERE CURRENT_DATE BETWEEN s.start_date AND s.end_date
117         SQL
118
119         my $sth = action::survey->db_Main->prepare_cached($select);
120         $sth->execute($ou);
121
122         $client->respond( $_->to_fieldmapper ) for ( map { action::survey->construct($_) } $sth->fetchall_hash );
123
124         return undef;
125 }
126 __PACKAGE__->register_method(
127         api_name        => 'open-ils.storage.action.survey.all',
128         api_level       => 1,
129         stream          => 1,
130         method          => 'find_local_surveys',
131 );
132
133 #XXX Fix stored proc calls
134 sub find_opac_surveys {
135         my $self = shift;
136         my $client = shift;
137         my $ou = ''.shift();
138
139         return undef unless ($ou);
140         my $s_table = action::survey->table;
141
142         my $select = <<"        SQL";
143                 SELECT  s.*
144                   FROM  $s_table s
145                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
146                   WHERE CURRENT_DATE BETWEEN s.start_date AND s.end_date
147                         AND s.opac IS TRUE;
148         SQL
149
150         my $sth = action::survey->db_Main->prepare_cached($select);
151         $sth->execute($ou);
152
153         $client->respond( $_->to_fieldmapper ) for ( map { action::survey->construct($_) } $sth->fetchall_hash );
154
155         return undef;
156 }
157 __PACKAGE__->register_method(
158         api_name        => 'open-ils.storage.action.survey.opac',
159         api_level       => 1,
160         stream          => 1,
161         method          => 'find_opac_surveys',
162 );
163
164 sub find_optional_surveys {
165         my $self = shift;
166         my $client = shift;
167         my $ou = ''.shift();
168
169         return undef unless ($ou);
170         my $s_table = action::survey->table;
171
172         my $select = <<"        SQL";
173                 SELECT  s.*
174                   FROM  $s_table s
175                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
176                   WHERE CURRENT_DATE BETWEEN s.start_date AND s.end_date
177                         AND s.required IS FALSE;
178         SQL
179
180         my $sth = action::survey->db_Main->prepare_cached($select);
181         $sth->execute($ou);
182
183         $client->respond( $_->to_fieldmapper ) for ( map { action::survey->construct($_) } $sth->fetchall_hash );
184
185         return undef;
186 }
187 __PACKAGE__->register_method(
188         api_name        => 'open-ils.storage.action.survey.optional',
189         api_level       => 1,
190         stream          => 1,
191         method          => 'find_optional_surveys',
192 );
193
194 sub find_required_surveys {
195         my $self = shift;
196         my $client = shift;
197         my $ou = ''.shift();
198
199         return undef unless ($ou);
200         my $s_table = action::survey->table;
201
202         my $select = <<"        SQL";
203                 SELECT  s.*
204                   FROM  $s_table s
205                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
206                   WHERE CURRENT_DATE BETWEEN s.start_date AND s.end_date
207                         AND s.required IS TRUE;
208         SQL
209
210         my $sth = action::survey->db_Main->prepare_cached($select);
211         $sth->execute($ou);
212
213         $client->respond( $_->to_fieldmapper ) for ( map { action::survey->construct($_) } $sth->fetchall_hash );
214
215         return undef;
216 }
217 __PACKAGE__->register_method(
218         api_name        => 'open-ils.storage.action.survey.required',
219         api_level       => 1,
220         stream          => 1,
221         method          => 'find_required_surveys',
222 );
223
224 sub find_usr_summary_surveys {
225         my $self = shift;
226         my $client = shift;
227         my $ou = ''.shift();
228
229         return undef unless ($ou);
230         my $s_table = action::survey->table;
231
232         my $select = <<"        SQL";
233                 SELECT  s.*
234                   FROM  $s_table s
235                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
236                   WHERE CURRENT_DATE BETWEEN s.start_date AND s.end_date
237                         AND s.usr_summary IS TRUE;
238         SQL
239
240         my $sth = action::survey->db_Main->prepare_cached($select);
241         $sth->execute($ou);
242
243         $client->respond( $_->to_fieldmapper ) for ( map { action::survey->construct($_) } $sth->fetchall_hash );
244
245         return undef;
246 }
247 __PACKAGE__->register_method(
248         api_name        => 'open-ils.storage.action.survey.usr_summary',
249         api_level       => 1,
250         stream          => 1,
251         method          => 'find_usr_summary_surveys',
252 );
253
254
255 1;