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