1 package OpenILS::Application::Storage::Publisher::biblio;
2 use base qw/OpenILS::Application::Storage/;
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;
11 sub record_copy_count {
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;
25 my $descendants = "actor.org_unit_descendants(u.id)";
26 my $ancestors = "actor.org_unit_ancestors(?)";
28 my $visible = 'AND st.holdable = TRUE AND loc.opac_visible = TRUE AND cp.opac_visible = TRUE';
29 if ($self->api_name =~ /staff/o) {
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)
45 AND cn.deleted IS FALSE
46 AND cp.deleted IS FALSE)
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)
57 AND cn.deleted IS FALSE
58 AND cp.deleted IS FALSE
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)
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)
77 JOIN $src_table src ON (src.id = br.source)
79 AND src.transcendant IS TRUE
83 JOIN $out_table t ON (u.ou_type = t.id)
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 );
94 __PACKAGE__->register_method(
95 api_name => 'open-ils.storage.biblio.record_entry.copy_count',
96 method => 'record_copy_count',
101 __PACKAGE__->register_method(
102 api_name => 'open-ils.storage.biblio.record_entry.copy_count.staff',
103 method => 'record_copy_count',
109 sub record_ranged_tree {
115 my $limit = shift || 0;
116 my $offset = shift || 0;
118 my $ou_sql = defined($depth) ?
119 "SELECT id FROM actor.org_unit_descendants(?,?)":
120 "SELECT id FROM actor.org_unit_descendants(?)";
125 ->selectcol_arrayref(
129 (defined($depth) ? ($depth) : ()),
132 return undef unless ($ou_list and @$ou_list);
134 $r = biblio::record_entry->retrieve( $r );
135 return undef unless ($r);
137 my $rec = $r->to_fieldmapper;
138 $rec->call_numbers([]);
140 $rec->fixed_fields( $r->record_descriptor->next->to_fieldmapper );
142 my $offset_count = 0;
144 for my $cn ( $r->call_numbers ) {
145 my $call_number = $cn->to_fieldmapper;
146 $call_number->copies([]);
149 for my $cp ( $cn->copies(circ_lib => $ou_list) ) {
150 if ($offset > 0 && $offset_count < $offset) {
155 last if ($limit > 0 && $limit_count >= $limit);
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;
165 last if ($limit > 0 && $limit_count >= $limit);
167 push @{ $rec->call_numbers }, $call_number if (@{ $call_number->copies });
172 __PACKAGE__->register_method(
173 api_name => 'open-ils.storage.biblio.record_entry.ranged_tree',
174 method => 'record_ranged_tree',
179 sub record_by_barcode {
183 my $cn_table = asset::call_number->table;
184 my $cp_table = asset::copy->table;
187 my ($r) = biblio::record_entry->db_Main->selectrow_array( <<" SQL", {}, $id );
190 JOIN $cp_table cp ON (cp.call_number = cn.id)
194 my $rec = biblio::record_entry->retrieve( $r );
196 return $rec->to_fieldmapper if ($rec);
199 __PACKAGE__->register_method(
200 api_name => 'open-ils.storage.biblio.record_entry.retrieve_by_barcode',
201 method => 'record_by_barcode',
210 my $cn_table = asset::call_number->table;
211 my $cp_table = asset::copy->table;
214 my ($r) = biblio::record_entry->db_Main->selectrow_array( <<" SQL", {}, $id );
217 JOIN $cp_table cp ON (cp.call_number = cn.id)
221 my $rec = biblio::record_entry->retrieve( $r );
222 return undef unless ($rec);
224 my $r_fm = $rec->to_fieldmapper;
225 my $ff = $rec->record_descriptor->next;
226 $r_fm->fixed_fields( $ff->to_fieldmapper ) if ($ff);
230 __PACKAGE__->register_method(
231 api_name => 'open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy',
232 method => 'record_by_copy',
241 sub record_copy_count {
247 if ($self->api_name !~ /batch/o) {
251 throw OpenSRF::EX::InvalidArg ( "No org_unit id passed!" )
254 throw OpenSRF::EX::InvalidArg ( "No record id passed!" )
257 $org_unit_lookup ||= $self->method_lookup('open-ils.storage.direct.actor.org_unit.retrieve');
258 my ($org_unit) = $org_unit_lookup->run($oid);
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!!!
266 my $rec_list = join(',',@recs);
268 my $cp_table = asset::copy->table;
269 my $cn_table = asset::call_number->table;
271 my $select =<<" SQL";
272 SELECT count(cp.*) as copies
274 JOIN $cp_table cp ON (cp.call_number = cn.id)
275 WHERE cn.owning_lib LIKE ? AND
276 cn.record IN ($rec_list)
279 my $sth = asset::copy->db_Main->prepare_cached($select);
280 $sth->execute($short_name_hack);
282 my $results = $sth->fetchall_hashref('record');
284 $client->respond($$results{$_}{copies} || 0) for (@recs);
288 __PACKAGE__->register_method(
289 method => 'record_copy_count',
290 api_name => 'open-ils.storage.direct.biblio.record_copy_count',
294 __PACKAGE__->register_method(
295 method => 'record_copy_count',
296 api_name => 'open-ils.storage.direct.biblio.record_copy_count.batch',
304 sub global_record_copy_count {
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;
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);
320 SELECT owning_lib, sum(avail), sum(tot)
322 SELECT cn.owning_lib, count(cp.id) as avail, 0 as tot
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)
332 SELECT cn.owning_lib, 0 as avail, count(cp.id) as tot
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)
344 my $sth = biblio::record_entry->db_Main->prepare_cached($sql);
345 $sth->execute("$rec", "$rec");
347 $client->respond( $_ ) for (@{$sth->fetchall_arrayref});
350 __PACKAGE__->register_method(
351 api_name => 'open-ils.storage.biblio.record_entry.global_copy_count',
352 method => 'global_record_copy_count',
357 __PACKAGE__->register_method(
358 api_name => 'open-ils.storage.biblio.record_entry.global_copy_count.staff',
359 method => 'global_record_copy_count',
365 sub record_copy_status_count {
371 my $depth = shift || 0;
374 my $descendants = "actor.org_unit_descendants(?,?)";
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;
383 SELECT cp.circ_lib, cn.label, cp.status, count(cp.id)
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
401 my $sth = biblio::record_entry->db_Main->prepare_cached($sql);
402 $sth->execute($ou, $depth, "$rec" );
405 for my $row (@{$sth->fetchall_arrayref}) {
406 $data{$$row[0]}{$$row[1]}{$$row[2]} += $$row[3];
409 for my $ou (keys %data) {
410 for my $cn (keys %{$data{$ou}}) {
411 $client->respond( [$ou, $cn, $data{$ou}{$cn}] );
416 __PACKAGE__->register_method(
417 api_name => 'open-ils.storage.biblio.record_entry.status_copy_count',
418 method => 'record_copy_status_count',