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 $loc_table = asset::copy_location->table;
21 my $out_table = actor::org_unit_type->table;
23 my $descendants = "actor.org_unit_descendants(u.id)";
24 my $ancestors = "actor.org_unit_ancestors(?)";
26 my $visible = 'AND st.holdable = TRUE AND loc.opac_visible = TRUE AND cp.opac_visible = TRUE';
27 if ($self->api_name =~ /staff/o) {
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)
43 AND cn.deleted IS FALSE
44 AND cp.deleted IS FALSE)
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)
55 AND cn.deleted IS FALSE
56 AND cp.deleted IS FALSE
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)
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)
73 JOIN $out_table t ON (u.ou_type = t.id)
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 );
84 __PACKAGE__->register_method(
85 api_name => 'open-ils.storage.biblio.record_entry.copy_count',
86 method => 'record_copy_count',
91 __PACKAGE__->register_method(
92 api_name => 'open-ils.storage.biblio.record_entry.copy_count.staff',
93 method => 'record_copy_count',
99 sub record_ranged_tree {
104 my $depth = shift || 0;
105 my $limit = shift || 0;
106 my $offset = shift || 0;
111 ->selectcol_arrayref(
112 'SELECT id FROM actor.org_unit_descendants(?,?)',
118 return undef unless ($ou_list and @$ou_list);
120 $r = biblio::record_entry->retrieve( $r );
121 return undef unless ($r);
123 my $rec = $r->to_fieldmapper;
124 $rec->call_numbers([]);
126 $rec->fixed_fields( $r->record_descriptor->next->to_fieldmapper );
128 my $offset_count = 0;
130 for my $cn ( $r->call_numbers ) {
131 my $call_number = $cn->to_fieldmapper;
132 $call_number->copies([]);
135 for my $cp ( $cn->copies(circ_lib => $ou_list) ) {
136 if ($offset > 0 && $offset_count < $offset) {
141 last if ($limit > 0 && $limit_count >= $limit);
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;
151 last if ($limit > 0 && $limit_count >= $limit);
153 push @{ $rec->call_numbers }, $call_number if (@{ $call_number->copies });
158 __PACKAGE__->register_method(
159 api_name => 'open-ils.storage.biblio.record_entry.ranged_tree',
160 method => 'record_ranged_tree',
165 sub record_by_barcode {
169 my $cn_table = asset::call_number->table;
170 my $cp_table = asset::copy->table;
173 my ($r) = biblio::record_entry->db_Main->selectrow_array( <<" SQL", {}, $id );
176 JOIN $cp_table cp ON (cp.call_number = cn.id)
180 my $rec = biblio::record_entry->retrieve( $r );
182 return $rec->to_fieldmapper if ($rec);
185 __PACKAGE__->register_method(
186 api_name => 'open-ils.storage.biblio.record_entry.retrieve_by_barcode',
187 method => 'record_by_barcode',
196 my $cn_table = asset::call_number->table;
197 my $cp_table = asset::copy->table;
200 my ($r) = biblio::record_entry->db_Main->selectrow_array( <<" SQL", {}, $id );
203 JOIN $cp_table cp ON (cp.call_number = cn.id)
207 my $rec = biblio::record_entry->retrieve( $r );
208 return undef unless ($rec);
210 my $r_fm = $rec->to_fieldmapper;
211 my $ff = $rec->record_descriptor->next;
212 $r_fm->fixed_fields( $ff->to_fieldmapper ) if ($ff);
216 __PACKAGE__->register_method(
217 api_name => 'open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy',
218 method => 'record_by_copy',
227 sub record_copy_count {
233 if ($self->api_name !~ /batch/o) {
237 throw OpenSRF::EX::InvalidArg ( "No org_unit id passed!" )
240 throw OpenSRF::EX::InvalidArg ( "No record id passed!" )
243 $org_unit_lookup ||= $self->method_lookup('open-ils.storage.direct.actor.org_unit.retrieve');
244 my ($org_unit) = $org_unit_lookup->run($oid);
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!!!
252 my $rec_list = join(',',@recs);
254 my $cp_table = asset::copy->table;
255 my $cn_table = asset::call_number->table;
257 my $select =<<" SQL";
258 SELECT count(cp.*) as copies
260 JOIN $cp_table cp ON (cp.call_number = cn.id)
261 WHERE cn.owning_lib LIKE ? AND
262 cn.record IN ($rec_list)
265 my $sth = asset::copy->db_Main->prepare_cached($select);
266 $sth->execute($short_name_hack);
268 my $results = $sth->fetchall_hashref('record');
270 $client->respond($$results{$_}{copies} || 0) for (@recs);
274 __PACKAGE__->register_method(
275 method => 'record_copy_count',
276 api_name => 'open-ils.storage.direct.biblio.record_copy_count',
280 __PACKAGE__->register_method(
281 method => 'record_copy_count',
282 api_name => 'open-ils.storage.direct.biblio.record_copy_count.batch',
290 sub global_record_copy_count {
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;
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);
306 SELECT owning_lib, sum(avail), sum(tot)
308 SELECT cn.owning_lib, count(cp.id) as avail, 0 as tot
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)
318 SELECT cn.owning_lib, 0 as avail, count(cp.id) as tot
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)
330 my $sth = biblio::record_entry->db_Main->prepare_cached($sql);
331 $sth->execute("$rec", "$rec");
333 $client->respond( $_ ) for (@{$sth->fetchall_arrayref});
336 __PACKAGE__->register_method(
337 api_name => 'open-ils.storage.biblio.record_entry.global_copy_count',
338 method => 'global_record_copy_count',
343 __PACKAGE__->register_method(
344 api_name => 'open-ils.storage.biblio.record_entry.global_copy_count.staff',
345 method => 'global_record_copy_count',
351 sub record_copy_status_count {
357 my $depth = shift || 0;
360 my $descendants = "actor.org_unit_descendants(?,?)";
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;
369 SELECT cp.circ_lib, cn.label, cp.status, count(cp.id)
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
387 my $sth = biblio::record_entry->db_Main->prepare_cached($sql);
388 $sth->execute($ou, $depth, "$rec" );
391 for my $row (@{$sth->fetchall_arrayref}) {
392 $data{$$row[0]}{$$row[1]}{$$row[2]} += $$row[3];
395 for my $ou (keys %data) {
396 for my $cn (keys %{$data{$ou}}) {
397 $client->respond( [$ou, $cn, $data{$ou}{$cn}] );
402 __PACKAGE__->register_method(
403 api_name => 'open-ils.storage.biblio.record_entry.status_copy_count',
404 method => 'record_copy_status_count',