]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/action.pm
just get the first "nearest" hold
[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                 LIMIT 1
52         SQL
53         return $id;
54 }
55 __PACKAGE__->register_method(
56         api_name        => 'open-ils.storage.action.hold_request.nearest_hold',
57         api_level       => 1,
58         method          => 'nearest_hold',
59 );
60
61 sub next_resp_group_id {
62         my $self = shift;
63         my $client = shift;
64
65         # XXX This is not replication safe!!!
66
67         my ($id) = action::survey->db_Main->selectrow_array(<<" SQL");
68                 SELECT NEXTVAL('action.survey_response_group_id_seq'::TEXT)
69         SQL
70         return $id;
71 }
72 __PACKAGE__->register_method(
73         api_name        => 'open-ils.storage.action.survey_response.next_group_id',
74         api_level       => 1,
75         method          => 'next_resp_group_id',
76 );
77
78 sub patron_circ_summary {
79         my $self = shift;
80         my $client = shift;
81         my $id = ''.shift();
82
83         return undef unless ($id);
84         my $c_table = action::circulation->table;
85         my $b_table = money::billing->table;
86
87         my $select = <<"        SQL";
88                 SELECT  COUNT(DISTINCT c.id), SUM( COALESCE(b.amount,0) )
89                   FROM  $c_table c
90                         LEFT OUTER JOIN $b_table b ON (c.id = b.xact)
91                   WHERE c.usr = ?
92                         AND c.xact_finish IS NULL
93                         AND c.stop_fines NOT IN ('CLAIMSRETURNED','LOST')
94         SQL
95
96         return action::survey->db_Main->selectrow_arrayref($select, {}, $id);
97 }
98 __PACKAGE__->register_method(
99         api_name        => 'open-ils.storage.action.circulation.patron_summary',
100         api_level       => 1,
101         method          => 'patron_circ_summary',
102 );
103
104 #XXX Fix stored proc calls
105 sub find_local_surveys {
106         my $self = shift;
107         my $client = shift;
108         my $ou = ''.shift();
109
110         return undef unless ($ou);
111         my $s_table = action::survey->table;
112
113         my $select = <<"        SQL";
114                 SELECT  s.*
115                   FROM  $s_table s
116                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
117                   WHERE CURRENT_DATE BETWEEN s.start_date AND s.end_date
118         SQL
119
120         my $sth = action::survey->db_Main->prepare_cached($select);
121         $sth->execute($ou);
122
123         $client->respond( $_->to_fieldmapper ) for ( map { action::survey->construct($_) } $sth->fetchall_hash );
124
125         return undef;
126 }
127 __PACKAGE__->register_method(
128         api_name        => 'open-ils.storage.action.survey.all',
129         api_level       => 1,
130         stream          => 1,
131         method          => 'find_local_surveys',
132 );
133
134 #XXX Fix stored proc calls
135 sub find_opac_surveys {
136         my $self = shift;
137         my $client = shift;
138         my $ou = ''.shift();
139
140         return undef unless ($ou);
141         my $s_table = action::survey->table;
142
143         my $select = <<"        SQL";
144                 SELECT  s.*
145                   FROM  $s_table s
146                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
147                   WHERE CURRENT_DATE BETWEEN s.start_date AND s.end_date
148                         AND s.opac IS TRUE;
149         SQL
150
151         my $sth = action::survey->db_Main->prepare_cached($select);
152         $sth->execute($ou);
153
154         $client->respond( $_->to_fieldmapper ) for ( map { action::survey->construct($_) } $sth->fetchall_hash );
155
156         return undef;
157 }
158 __PACKAGE__->register_method(
159         api_name        => 'open-ils.storage.action.survey.opac',
160         api_level       => 1,
161         stream          => 1,
162         method          => 'find_opac_surveys',
163 );
164
165 sub find_optional_surveys {
166         my $self = shift;
167         my $client = shift;
168         my $ou = ''.shift();
169
170         return undef unless ($ou);
171         my $s_table = action::survey->table;
172
173         my $select = <<"        SQL";
174                 SELECT  s.*
175                   FROM  $s_table s
176                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
177                   WHERE CURRENT_DATE BETWEEN s.start_date AND s.end_date
178                         AND s.required IS FALSE;
179         SQL
180
181         my $sth = action::survey->db_Main->prepare_cached($select);
182         $sth->execute($ou);
183
184         $client->respond( $_->to_fieldmapper ) for ( map { action::survey->construct($_) } $sth->fetchall_hash );
185
186         return undef;
187 }
188 __PACKAGE__->register_method(
189         api_name        => 'open-ils.storage.action.survey.optional',
190         api_level       => 1,
191         stream          => 1,
192         method          => 'find_optional_surveys',
193 );
194
195 sub find_required_surveys {
196         my $self = shift;
197         my $client = shift;
198         my $ou = ''.shift();
199
200         return undef unless ($ou);
201         my $s_table = action::survey->table;
202
203         my $select = <<"        SQL";
204                 SELECT  s.*
205                   FROM  $s_table s
206                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
207                   WHERE CURRENT_DATE BETWEEN s.start_date AND s.end_date
208                         AND s.required IS TRUE;
209         SQL
210
211         my $sth = action::survey->db_Main->prepare_cached($select);
212         $sth->execute($ou);
213
214         $client->respond( $_->to_fieldmapper ) for ( map { action::survey->construct($_) } $sth->fetchall_hash );
215
216         return undef;
217 }
218 __PACKAGE__->register_method(
219         api_name        => 'open-ils.storage.action.survey.required',
220         api_level       => 1,
221         stream          => 1,
222         method          => 'find_required_surveys',
223 );
224
225 sub find_usr_summary_surveys {
226         my $self = shift;
227         my $client = shift;
228         my $ou = ''.shift();
229
230         return undef unless ($ou);
231         my $s_table = action::survey->table;
232
233         my $select = <<"        SQL";
234                 SELECT  s.*
235                   FROM  $s_table s
236                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
237                   WHERE CURRENT_DATE BETWEEN s.start_date AND s.end_date
238                         AND s.usr_summary IS TRUE;
239         SQL
240
241         my $sth = action::survey->db_Main->prepare_cached($select);
242         $sth->execute($ou);
243
244         $client->respond( $_->to_fieldmapper ) for ( map { action::survey->construct($_) } $sth->fetchall_hash );
245
246         return undef;
247 }
248 __PACKAGE__->register_method(
249         api_name        => 'open-ils.storage.action.survey.usr_summary',
250         api_level       => 1,
251         stream          => 1,
252         method          => 'find_usr_summary_surveys',
253 );
254
255
256 1;