]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/biblio.pm
allow ou-limited copy status listing
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Storage / Publisher / biblio.pm
1 package OpenILS::Application::Storage::Publisher::biblio;
2 use base qw/OpenILS::Application::Storage/;
3 use vars qw/$VERSION/;
4 use OpenSRF::EX qw/:try/;
5 #use OpenILS::Application::Storage::CDBI::biblio;
6 #use OpenILS::Application::Storage::CDBI::asset;
7 use OpenILS::Utils::Fieldmapper;
8
9 $VERSION = 1;
10
11 sub record_copy_count {
12         my $self = shift;
13         my $client = shift;
14
15         my %args = @_;
16
17         my $cn_table = asset::call_number->table;
18         my $cp_table = asset::copy->table;
19         my $st_table = config::copy_status->table;
20         my $loc_table = asset::copy_location->table;
21         my $out_table = actor::org_unit_type->table;
22
23         my $descendants = "actor.org_unit_descendants(u.id)";
24         my $ancestors = "actor.org_unit_ancestors(?)";
25
26         my $visible = 'AND st.holdable = TRUE AND loc.opac_visible = TRUE AND cp.opac_visible = TRUE';
27         if ($self->api_name =~ /staff/o) {
28                 $visible = ''
29         }
30
31         my $sql = <<"   SQL";
32                 SELECT  t.depth,
33                         u.id AS org_unit,
34                         sum(
35                                 (SELECT count(cp.id)
36                                   FROM  $cn_table cn
37                                         JOIN $cp_table cp ON (cn.id = cp.call_number)
38                                         JOIN $descendants a ON (cp.circ_lib = a.id)
39                                         JOIN $st_table st ON (cp.status = st.id)
40                                         JOIN $loc_table loc ON (cp.location = loc.id)
41                                   WHERE cn.record = ?
42                                         $visible
43                                         AND cn.deleted IS FALSE
44                                         AND cp.deleted IS FALSE)
45                         ) AS count,
46                         sum(
47                                 (SELECT count(cp.id)
48                                   FROM  $cn_table cn
49                                         JOIN $cp_table cp ON (cn.id = cp.call_number)
50                                         JOIN $descendants a ON (cp.circ_lib = a.id)
51                                         JOIN $st_table st ON (cp.status = st.id)
52                                         JOIN $loc_table loc ON (cp.location = loc.id)
53                                   WHERE cn.record = ?
54                                         $visible
55                                         AND cn.deleted IS FALSE
56                                         AND cp.deleted IS FALSE
57                                         AND cp.status = 0)
58                         ) AS available,
59                         sum(
60                                 (SELECT count(cp.id)
61                                   FROM  $cn_table cn
62                                         JOIN $cp_table cp ON (cn.id = cp.call_number)
63                                         JOIN $st_table st ON (cp.status = st.id)
64                                         JOIN $loc_table loc ON (cp.location = loc.id)
65                                   WHERE cn.record = ?
66                                         AND st.holdable = TRUE
67                                         AND loc.opac_visible = TRUE
68                                         AND cp.opac_visible = TRUE
69                                         AND cn.deleted IS FALSE
70                                         AND cp.deleted IS FALSE)
71                         ) AS unshadow
72                   FROM  $ancestors u
73                         JOIN $out_table t ON (u.ou_type = t.id)
74                   GROUP BY 1,2
75         SQL
76
77         my $sth = biblio::record_entry->db_Main->prepare_cached($sql);
78         $sth->execute(''.$args{record}, ''.$args{record}, ''.$args{record}, ''.$args{org_unit});
79         while ( my $row = $sth->fetchrow_hashref ) {
80                 $client->respond( $row );
81         }
82         return undef;
83 }
84 __PACKAGE__->register_method(
85         api_name        => 'open-ils.storage.biblio.record_entry.copy_count',
86         method          => 'record_copy_count',
87         api_level       => 1,
88         stream          => 1,
89         cachable        => 1,
90 );
91 __PACKAGE__->register_method(
92         api_name        => 'open-ils.storage.biblio.record_entry.copy_count.staff',
93         method          => 'record_copy_count',
94         api_level       => 1,
95         stream          => 1,
96         cachable        => 1,
97 );
98
99 sub record_ranged_tree {
100         my $self = shift;
101         my $client = shift;
102         my $r = shift;
103         my $ou = shift;
104         my $depth = shift || 0;
105         my $limit = shift || 0;
106         my $offset = shift || 0;
107
108         my $ou_list =
109                 actor::org_unit
110                         ->db_Main
111                         ->selectcol_arrayref(
112                                 'SELECT id FROM actor.org_unit_descendants(?,?)',
113                                 {},
114                                 $ou,
115                                 $depth
116                         );
117
118         return undef unless ($ou_list and @$ou_list);
119
120         $r = biblio::record_entry->retrieve( $r );
121         return undef unless ($r);
122
123         my $rec = $r->to_fieldmapper;
124         $rec->call_numbers([]);
125
126         $rec->fixed_fields( $r->record_descriptor->next->to_fieldmapper );
127
128         my $offset_count = 0;
129         my $limit_count = 0;
130         for my $cn ( $r->call_numbers  ) {
131                 my $call_number = $cn->to_fieldmapper;
132                 $call_number->copies([]);
133
134
135                 for my $cp ( $cn->copies(circ_lib => $ou_list) ) {
136                         if ($offset > 0 && $offset_count < $offset) {
137                                 $offset_count++;
138                                 next;
139                         }
140                         
141                         last if ($limit > 0 && $limit_count >= $limit);
142
143                         my $copy = $cp->to_fieldmapper;
144                         $copy->status( $cp->status->to_fieldmapper );
145                         $copy->location( $cp->status->to_fieldmapper );
146                         push @{ $call_number->copies }, $copy;
147
148                         $limit_count++;
149                 }
150
151                 last if ($limit > 0 && $limit_count >= $limit);
152
153                 push @{ $rec->call_numbers }, $call_number if (@{ $call_number->copies });
154         }
155
156         return $rec;
157 }
158 __PACKAGE__->register_method(
159         api_name        => 'open-ils.storage.biblio.record_entry.ranged_tree',
160         method          => 'record_ranged_tree',
161         argc            => 1,
162         api_level       => 1,
163 );
164
165 sub record_by_barcode {
166         my $self = shift;
167         my $client = shift;
168
169         my $cn_table = asset::call_number->table;
170         my $cp_table = asset::copy->table;
171
172         my $id = ''.shift;
173         my ($r) = biblio::record_entry->db_Main->selectrow_array( <<"   SQL", {}, $id );
174                 SELECT  cn.record
175                   FROM  $cn_table cn
176                         JOIN $cp_table cp ON (cp.call_number = cn.id)
177                   WHERE cp.barcode = ?
178         SQL
179
180         my $rec = biblio::record_entry->retrieve( $r );
181
182         return $rec->to_fieldmapper if ($rec);
183         return undef;
184 }
185 __PACKAGE__->register_method(
186         api_name        => 'open-ils.storage.biblio.record_entry.retrieve_by_barcode',
187         method          => 'record_by_barcode',
188         api_level       => 1,
189         cachable        => 1,
190 );
191
192 sub record_by_copy {
193         my $self = shift;
194         my $client = shift;
195
196         my $cn_table = asset::call_number->table;
197         my $cp_table = asset::copy->table;
198
199         my $id = ''.shift;
200         my ($r) = biblio::record_entry->db_Main->selectrow_array( <<"   SQL", {}, $id );
201                 SELECT  cn.record
202                   FROM  $cn_table cn
203                         JOIN $cp_table cp ON (cp.call_number = cn.id)
204                   WHERE cp.id = ?
205         SQL
206
207         my $rec = biblio::record_entry->retrieve( $r );
208         return undef unless ($rec);
209
210         my $r_fm = $rec->to_fieldmapper;
211         my $ff = $rec->record_descriptor->next;
212         $r_fm->fixed_fields( $ff->to_fieldmapper ) if ($ff);
213
214         return $r_fm;
215 }
216 __PACKAGE__->register_method(
217         api_name        => 'open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy',
218         method          => 'record_by_copy',
219         api_level       => 1,
220         cachable        => 1,
221 );
222
223
224 =comment Old version
225
226 my $org_unit_lookup;
227 sub record_copy_count {
228         my $self = shift;
229         my $client = shift;
230         my $oid = shift;
231         my @recs = @_;
232
233         if ($self->api_name !~ /batch/o) {
234                 @recs = ($recs[0]);
235         }
236
237         throw OpenSRF::EX::InvalidArg ( "No org_unit id passed!" )
238                 unless ($oid);
239
240         throw OpenSRF::EX::InvalidArg ( "No record id passed!" )
241                 unless (@recs);
242
243         $org_unit_lookup ||= $self->method_lookup('open-ils.storage.direct.actor.org_unit.retrieve');
244         my ($org_unit) = $org_unit_lookup->run($oid);
245
246         # XXX Use descendancy tree here!!!
247         my $short_name_hack = $org_unit->shortname;
248         $short_name_hack = '' if (!$org_unit->parent_ou);
249         $short_name_hack .= '%';
250         # XXX Use descendancy tree here!!!
251
252         my $rec_list = join(',',@recs);
253
254         my $cp_table = asset::copy->table;
255         my $cn_table = asset::call_number->table;
256
257         my $select =<<" SQL";
258                 SELECT  count(cp.*) as copies
259                   FROM  $cn_table cn
260                         JOIN $cp_table cp ON (cp.call_number = cn.id)
261                   WHERE cn.owning_lib LIKE ? AND
262                         cn.record IN ($rec_list)
263         SQL
264
265         my $sth = asset::copy->db_Main->prepare_cached($select);
266         $sth->execute($short_name_hack);
267
268         my $results = $sth->fetchall_hashref('record');
269
270         $client->respond($$results{$_}{copies} || 0) for (@recs);
271
272         return undef;
273 }
274 __PACKAGE__->register_method(
275         method          => 'record_copy_count',
276         api_name        => 'open-ils.storage.direct.biblio.record_copy_count',
277         api_level       => 1,
278         argc            => 1,
279 );
280 __PACKAGE__->register_method(
281         method          => 'record_copy_count',
282         api_name        => 'open-ils.storage.direct.biblio.record_copy_count.batch',
283         api_level       => 1,
284         argc            => 1,
285         stream          => 1,
286 );
287
288 =cut
289
290 sub global_record_copy_count {
291         my $self = shift;
292         my $client = shift;
293
294         my $rec = shift;
295
296         my $cn_table = asset::call_number->table;
297         my $cp_table = asset::copy->table;
298         my $cl_table = asset::copy_location->table;
299         my $cs_table = config::copy_status->table;
300
301         my $copies_visible = 'AND cp.opac_visible IS TRUE AND cs.holdable IS TRUE AND cl.opac_visible IS TRUE';
302         $copies_visible = '' if ($self->api_name =~ /staff/o);
303
304         my $sql = <<"   SQL";
305
306                 SELECT  owning_lib, sum(avail), sum(tot)
307                   FROM  (
308                                 SELECT  cn.owning_lib, count(cp.id) as avail, 0 as tot
309                                   FROM  $cn_table cn
310                                         JOIN $cp_table cp ON (cn.id = cp.call_number)
311                                         JOIN $cs_table cs ON (cs.id = cp.status)
312                                         JOIN $cl_table cl ON (cl.id = cp.location)
313                                   WHERE cn.record = ?
314                                         AND cp.status = 0
315                                         $copies_visible
316                                   GROUP BY 1
317                                                 UNION
318                                 SELECT  cn.owning_lib, 0 as avail, count(cp.id) as tot
319                                   FROM  $cn_table cn
320                                         JOIN $cp_table cp ON (cn.id = cp.call_number)
321                                         JOIN $cs_table cs ON (cs.id = cp.status)
322                                         JOIN $cl_table cl ON (cl.id = cp.location)
323                                   WHERE cn.record = ?
324                                         $copies_visible
325                                   GROUP BY 1
326                         ) x
327                   GROUP BY 1
328         SQL
329
330         my $sth = biblio::record_entry->db_Main->prepare_cached($sql);
331         $sth->execute("$rec", "$rec");
332
333         $client->respond( $_ ) for (@{$sth->fetchall_arrayref});
334         return undef;
335 }
336 __PACKAGE__->register_method(
337         api_name        => 'open-ils.storage.biblio.record_entry.global_copy_count',
338         method          => 'global_record_copy_count',
339         api_level       => 1,
340         stream          => 1,
341         cachable        => 1,
342 );
343 __PACKAGE__->register_method(
344         api_name        => 'open-ils.storage.biblio.record_entry.global_copy_count.staff',
345         method          => 'global_record_copy_count',
346         api_level       => 1,
347         stream          => 1,
348         cachable        => 1,
349 );
350
351 sub record_copy_status_count {
352         my $self = shift;
353         my $client = shift;
354
355         my $rec = shift;
356         my $ou = shift || 1;
357         my $depth = shift || 0;
358
359
360         my $descendants = "actor.org_unit_descendants(?,?)";
361
362         my $cn_table = asset::call_number->table;
363         my $cp_table = asset::copy->table;
364         my $cl_table = asset::copy_location->table;
365         my $cs_table = config::copy_status->table;
366
367         my $sql = <<"   SQL";
368
369                 SELECT  cp.circ_lib, cn.label, cp.status, count(cp.id)
370                   FROM  $cp_table cp,
371                         $cn_table cn,
372                         $cl_table cl,
373                         $cs_table cs,
374                         $descendants d
375                   WHERE cn.record = ?
376                         AND cp.call_number = cn.id
377                         AND cp.location = cl.id
378                         AND cp.circ_lib = d.id
379                         AND cp.status = cs.id
380                         AND cl.opac_visible IS TRUE
381                         AND cp.opac_visible IS TRUE
382                         AND cp.deleted IS FALSE
383                         AND cs.holdable
384                   GROUP BY 1,2,3;
385         SQL
386
387         my $sth = biblio::record_entry->db_Main->prepare_cached($sql);
388         $sth->execute($ou, $depth, "$rec" );
389
390         my %data = ();
391         for my $row (@{$sth->fetchall_arrayref}) {
392                 $data{$$row[0]}{$$row[1]}{$$row[2]} += $$row[3];
393         }
394         
395         for my $ou (keys %data) {
396                 for my $cn (keys %{$data{$ou}}) {
397                         $client->respond( [$ou, $cn, $data{$ou}{$cn}] );
398                 }
399         }
400         return undef;
401 }
402 __PACKAGE__->register_method(
403         api_name        => 'open-ils.storage.biblio.record_entry.status_copy_count',
404         method          => 'record_copy_status_count',
405         api_level       => 1,
406         stream          => 1,
407         cachable        => 1,
408 );
409
410 1;