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 a.opac_visible = TRUE 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
59 AND cp.status IN (0,7))
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 next if ($cn->deleted);
146 my $call_number = $cn->to_fieldmapper;
147 $call_number->copies([]);
150 for my $cp ( $cn->copies(circ_lib => $ou_list) ) {
151 next if ($cp->deleted);
152 if ($offset > 0 && $offset_count < $offset) {
157 last if ($limit > 0 && $limit_count >= $limit);
159 my $copy = $cp->to_fieldmapper;
160 $copy->status( $cp->status->to_fieldmapper );
161 $copy->location( $cp->location->to_fieldmapper );
162 push @{ $call_number->copies }, $copy;
167 last if ($limit > 0 && $limit_count >= $limit);
169 push @{ $rec->call_numbers }, $call_number if (@{ $call_number->copies });
174 __PACKAGE__->register_method(
175 api_name => 'open-ils.storage.biblio.record_entry.ranged_tree',
176 method => 'record_ranged_tree',
181 sub record_by_barcode {
185 my $cn_table = asset::call_number->table;
186 my $cp_table = asset::copy->table;
189 my ($r) = biblio::record_entry->db_Main->selectrow_array( <<" SQL", {}, $id );
192 JOIN $cp_table cp ON (cp.call_number = cn.id)
196 my $rec = biblio::record_entry->retrieve( $r );
198 return $rec->to_fieldmapper if ($rec);
201 __PACKAGE__->register_method(
202 api_name => 'open-ils.storage.biblio.record_entry.retrieve_by_barcode',
203 method => 'record_by_barcode',
212 my $cn_table = asset::call_number->table;
213 my $cp_table = asset::copy->table;
216 my ($r) = biblio::record_entry->db_Main->selectrow_array( <<" SQL", {}, $id );
219 JOIN $cp_table cp ON (cp.call_number = cn.id)
223 my $rec = biblio::record_entry->retrieve( $r );
224 return undef unless ($rec);
226 my $r_fm = $rec->to_fieldmapper;
227 my $ff = $rec->record_descriptor->next;
228 $r_fm->fixed_fields( $ff->to_fieldmapper ) if ($ff);
232 __PACKAGE__->register_method(
233 api_name => 'open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy',
234 method => 'record_by_copy',
243 sub record_copy_count {
249 if ($self->api_name !~ /batch/o) {
253 throw OpenSRF::EX::InvalidArg ( "No org_unit id passed!" )
256 throw OpenSRF::EX::InvalidArg ( "No record id passed!" )
259 $org_unit_lookup ||= $self->method_lookup('open-ils.storage.direct.actor.org_unit.retrieve');
260 my ($org_unit) = $org_unit_lookup->run($oid);
262 # XXX Use descendancy tree here!!!
263 my $short_name_hack = $org_unit->shortname;
264 $short_name_hack = '' if (!$org_unit->parent_ou);
265 $short_name_hack .= '%';
266 # XXX Use descendancy tree here!!!
268 my $rec_list = join(',',@recs);
270 my $cp_table = asset::copy->table;
271 my $cn_table = asset::call_number->table;
273 my $select =<<" SQL";
274 SELECT count(cp.*) as copies
276 JOIN $cp_table cp ON (cp.call_number = cn.id)
277 WHERE cn.owning_lib LIKE ? AND
278 cn.record IN ($rec_list)
281 my $sth = asset::copy->db_Main->prepare_cached($select);
282 $sth->execute($short_name_hack);
284 my $results = $sth->fetchall_hashref('record');
286 $client->respond($$results{$_}{copies} || 0) for (@recs);
290 __PACKAGE__->register_method(
291 method => 'record_copy_count',
292 api_name => 'open-ils.storage.direct.biblio.record_copy_count',
296 __PACKAGE__->register_method(
297 method => 'record_copy_count',
298 api_name => 'open-ils.storage.direct.biblio.record_copy_count.batch',
306 sub global_record_copy_count {
312 my $cn_table = asset::call_number->table;
313 my $cp_table = asset::copy->table;
314 my $cl_table = asset::copy_location->table;
315 my $cs_table = config::copy_status->table;
317 my $copies_visible = 'AND cp.opac_visible IS TRUE AND cs.holdable IS TRUE AND cl.opac_visible IS TRUE';
318 $copies_visible = '' if ($self->api_name =~ /staff/o);
322 SELECT owning_lib, sum(avail), sum(tot)
324 SELECT cn.owning_lib, count(cp.id) as avail, 0 as tot
326 JOIN $cp_table cp ON (cn.id = cp.call_number)
327 JOIN $cs_table cs ON (cs.id = cp.status)
328 JOIN $cl_table cl ON (cl.id = cp.location)
330 AND cp.status IN (0,7)
334 SELECT cn.owning_lib, 0 as avail, count(cp.id) as tot
336 JOIN $cp_table cp ON (cn.id = cp.call_number)
337 JOIN $cs_table cs ON (cs.id = cp.status)
338 JOIN $cl_table cl ON (cl.id = cp.location)
346 my $sth = biblio::record_entry->db_Main->prepare_cached($sql);
347 $sth->execute("$rec", "$rec");
349 $client->respond( $_ ) for (@{$sth->fetchall_arrayref});
352 __PACKAGE__->register_method(
353 api_name => 'open-ils.storage.biblio.record_entry.global_copy_count',
354 method => 'global_record_copy_count',
359 __PACKAGE__->register_method(
360 api_name => 'open-ils.storage.biblio.record_entry.global_copy_count.staff',
361 method => 'global_record_copy_count',
367 sub record_copy_status_count {
373 my $depth = shift || 0;
376 my $descendants = "actor.org_unit_descendants(?,?)";
378 my $cn_table = asset::call_number->table;
379 my $cp_table = asset::copy->table;
380 my $cl_table = asset::copy_location->table;
381 my $cs_table = config::copy_status->table;
385 SELECT cp.circ_lib, cn.label, cp.status, count(cp.id)
392 AND cp.call_number = cn.id
393 AND cp.location = cl.id
394 AND cp.circ_lib = d.id
395 AND cp.status = cs.id
396 AND cl.opac_visible IS TRUE
397 AND cp.opac_visible IS TRUE
398 AND cp.deleted IS FALSE
403 my $sth = biblio::record_entry->db_Main->prepare_cached($sql);
404 $sth->execute($ou, $depth, "$rec" );
407 for my $row (@{$sth->fetchall_arrayref}) {
408 $data{$$row[0]}{$$row[1]}{$$row[2]} += $$row[3];
411 for my $ou (keys %data) {
412 for my $cn (keys %{$data{$ou}}) {
413 $client->respond( [$ou, $cn, $data{$ou}{$cn}] );
418 __PACKAGE__->register_method(
419 api_name => 'open-ils.storage.biblio.record_entry.status_copy_count',
420 method => 'record_copy_status_count',