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 $out_table = actor::org_unit_type->table;
20 my $descendants = "actor.org_unit_descendants(u.id)";
21 my $ancestors = "actor.org_unit_ancestors(?)";
29 JOIN $cp_table cp ON (cn.id = cp.call_number)
30 JOIN $descendants a ON (cp.circ_lib = a.id)
36 JOIN $cp_table cp ON (cn.id = cp.call_number)
37 JOIN $descendants a ON (cp.circ_lib = a.id)
42 JOIN $out_table t ON (u.ou_type = t.id)
46 my $sth = biblio::record_entry->db_Main->prepare_cached($sql);
47 $sth->execute(''.$args{record}, ''.$args{record}, ''.$args{org_unit});
48 while ( my $row = $sth->fetchrow_hashref ) {
49 $client->respond( $row );
53 __PACKAGE__->register_method(
54 api_name => 'open-ils.storage.biblio.record_entry.copy_count',
55 method => 'record_copy_count',
61 sub record_ranged_tree {
66 my $depth = shift || 0;
67 my $limit = shift || 0;
68 my $offset = shift || 0;
74 'SELECT id FROM actor.org_unit_descendants(?,?)',
80 return undef unless ($ou_list and @$ou_list);
82 $r = biblio::record_entry->retrieve( $r );
83 return undef unless ($r);
85 my $rec = $r->to_fieldmapper;
86 $rec->call_numbers([]);
88 $rec->fixed_fields( $r->record_descriptor->next->to_fieldmapper );
92 for my $cn ( $r->call_numbers ) {
93 my $call_number = $cn->to_fieldmapper;
94 $call_number->copies([]);
97 for my $cp ( $cn->copies(circ_lib => $ou_list) ) {
98 if ($offset > 0 && $offset_count < $offset) {
103 last if ($limit > 0 && $limit_count >= $limit);
105 my $copy = $cp->to_fieldmapper;
106 $copy->status( $cp->status->to_fieldmapper );
107 $copy->location( $cp->status->to_fieldmapper );
108 push @{ $call_number->copies }, $copy;
113 last if ($limit > 0 && $limit_count >= $limit);
115 push @{ $rec->call_numbers }, $call_number if (@{ $call_number->copies });
120 __PACKAGE__->register_method(
121 api_name => 'open-ils.storage.biblio.record_entry.ranged_tree',
122 method => 'record_ranged_tree',
127 sub record_by_barcode {
131 my $cn_table = asset::call_number->table;
132 my $cp_table = asset::copy->table;
135 my ($r) = biblio::record_entry->db_Main->selectrow_array( <<" SQL", {}, $id );
138 JOIN $cp_table cp ON (cp.call_number = cn.id)
142 my $rec = biblio::record_entry->retrieve( $r );
144 return $rec->to_fieldmapper if ($rec);
147 __PACKAGE__->register_method(
148 api_name => 'open-ils.storage.biblio.record_entry.retrieve_by_barcode',
149 method => 'record_by_barcode',
158 my $cn_table = asset::call_number->table;
159 my $cp_table = asset::copy->table;
162 my ($r) = biblio::record_entry->db_Main->selectrow_array( <<" SQL", {}, $id );
165 JOIN $cp_table cp ON (cp.call_number = cn.id)
169 my $rec = biblio::record_entry->retrieve( $r );
170 return undef unless ($rec);
172 my $r_fm = $rec->to_fieldmapper;
173 my $ff = $rec->record_descriptor->next;
174 $r_fm->fixed_fields( $ff->to_fieldmapper ) if ($ff);
178 __PACKAGE__->register_method(
179 api_name => 'open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy',
180 method => 'record_by_copy',
189 sub record_copy_count {
195 if ($self->api_name !~ /batch/o) {
199 throw OpenSRF::EX::InvalidArg ( "No org_unit id passed!" )
202 throw OpenSRF::EX::InvalidArg ( "No record id passed!" )
205 $org_unit_lookup ||= $self->method_lookup('open-ils.storage.direct.actor.org_unit.retrieve');
206 my ($org_unit) = $org_unit_lookup->run($oid);
208 # XXX Use descendancy tree here!!!
209 my $short_name_hack = $org_unit->shortname;
210 $short_name_hack = '' if (!$org_unit->parent_ou);
211 $short_name_hack .= '%';
212 # XXX Use descendancy tree here!!!
214 my $rec_list = join(',',@recs);
216 my $cp_table = asset::copy->table;
217 my $cn_table = asset::call_number->table;
219 my $select =<<" SQL";
220 SELECT count(cp.*) as copies
222 JOIN $cp_table cp ON (cp.call_number = cn.id)
223 WHERE cn.owning_lib LIKE ? AND
224 cn.record IN ($rec_list)
227 my $sth = asset::copy->db_Main->prepare_cached($select);
228 $sth->execute($short_name_hack);
230 my $results = $sth->fetchall_hashref('record');
232 $client->respond($$results{$_}{copies} || 0) for (@recs);
236 __PACKAGE__->register_method(
237 method => 'record_copy_count',
238 api_name => 'open-ils.storage.direct.biblio.record_copy_count',
242 __PACKAGE__->register_method(
243 method => 'record_copy_count',
244 api_name => 'open-ils.storage.direct.biblio.record_copy_count.batch',
252 sub global_record_copy_count {
258 my $cn_table = asset::call_number->table;
259 my $cp_table = asset::copy->table;
260 my $cl_table = asset::copy_location->table;
261 my $cs_table = config::copy_status->table;
263 my $copies_visible = 'AND cp.opac_visible IS TRUE AND cs.holdable IS TRUE AND cl.opac_visible IS TRUE';
264 $copies_visible = '' if ($self->api_name =~ /staff/o);
268 SELECT owning_lib, sum(avail), sum(tot)
270 SELECT cn.owning_lib, count(cp.id) as avail, 0 as tot
272 JOIN $cp_table cp ON (cn.id = cp.call_number)
273 JOIN $cs_table cs ON (cs.id = cp.status)
274 JOIN $cl_table cl ON (cl.id = cp.location)
280 SELECT cn.owning_lib, 0 as avail, count(cp.id) as tot
282 JOIN $cp_table cp ON (cn.id = cp.call_number)
283 JOIN $cs_table cs ON (cs.id = cp.status)
284 JOIN $cl_table cl ON (cl.id = cp.location)
292 my $sth = biblio::record_entry->db_Main->prepare_cached($sql);
293 $sth->execute("$rec", "$rec");
295 $client->respond( $_ ) for (@{$sth->fetchall_arrayref});
298 __PACKAGE__->register_method(
299 api_name => 'open-ils.storage.biblio.record_entry.global_copy_count',
300 method => 'global_record_copy_count',
305 __PACKAGE__->register_method(
306 api_name => 'open-ils.storage.biblio.record_entry.global_copy_count.staff',
307 method => 'global_record_copy_count',
313 sub record_copy_status_count {
319 my $cn_table = asset::call_number->table;
320 my $cp_table = asset::copy->table;
321 my $cl_table = asset::copy_location->table;
322 my $cs_table = config::copy_status->table;
326 SELECT cp.circ_lib, cn.label, cp.status, count(cp.id)
332 AND cp.call_number = cn.id
333 AND cp.location = cl.id
334 AND cp.status = cs.id
335 AND cl.opac_visible IS TRUE
336 AND cp.opac_visible IS TRUE
337 AND cp.deleted IS FALSE
342 my $sth = biblio::record_entry->db_Main->prepare_cached($sql);
343 $sth->execute("$rec");
346 for my $row (@{$sth->fetchall_arrayref}) {
347 $data{$$row[0]}{$$row[1]}{$$row[2]} += $$row[3];
350 for my $ou (keys %data) {
351 for my $cn (keys %{$data{$ou}}) {
352 $client->respond( [$ou, $cn, $data{$ou}{$cn}] );
357 __PACKAGE__->register_method(
358 api_name => 'open-ils.storage.biblio.record_entry.status_copy_count',
359 method => 'record_copy_status_count',