]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/action.pm
0df3437b70ee33f4487f356b2958aab7e2edf12e
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Storage / Publisher / action.pm
1 package OpenILS::Application::Storage::Publisher::action;
2 use base qw/OpenILS::Application::Storage/;
3 use OpenSRF::Utils::Logger qw/:level/;
4 use OpenSRF::Utils qw/:datetime/;
5 use OpenSRF::AppSession;
6 use OpenSRF::EX qw/:try/;
7 use OpenILS::Utils::Fieldmapper;
8 use DateTime;
9 use DateTime::Format::ISO8601;
10
11 my $parser = DateTime::Format::ISO8601->new;
12 my $log = 'OpenSRF::Utils::Logger';
13
14 sub overdue_circs {
15         my $grace = shift;
16
17         my $c_t = action::circulation->table;
18
19         $grace = " - ($grace * (fine_interval))" if ($grace);
20
21         my $sql = <<"   SQL";
22                 SELECT  *
23                   FROM  $c_t
24                   WHERE stop_fines IS NULL
25                         AND due_date < ( CURRENT_TIMESTAMP $grace)
26         SQL
27
28         my $sth = action::circulation->db_Main->prepare_cached($sql);
29         $sth->execute;
30
31         return ( map { action::circulation->construct($_) } $sth->fetchall_hash );
32
33 }
34
35 sub grab_overdue {
36         my $self = shift;
37         my $client = shift;
38         my $grace = shift || '';
39
40         $client->respond( $_->to_fieldmapper ) for ( overdue_circs($grace) );
41
42         return undef;
43
44 }
45 __PACKAGE__->register_method(
46         api_name        => 'open-ils.storage.action.circulation.overdue',
47         api_level       => 1,
48         stream          => 1,
49         method          => 'grab_overdue',
50 );
51
52 sub nearest_hold {
53         my $self = shift;
54         my $client = shift;
55         my $pl = shift;
56         my $cp = shift;
57
58         my ($id) = action::hold_request->db_Main->selectrow_array(<<"   SQL", {}, $pl,$cp);
59                 SELECT  h.id
60                   FROM  action.hold_request h
61                         JOIN action.hold_copy_map hm ON (hm.hold = h.id)
62                   WHERE h.pickup_lib = ?
63                         AND hm.target_copy = ?
64                         AND h.capture_time IS NULL
65                 ORDER BY h.pickup_lib - (SELECT home_ou FROM actor.usr a WHERE a.id = h.usr), h.request_time
66                 LIMIT 1
67         SQL
68         return $id;
69 }
70 __PACKAGE__->register_method(
71         api_name        => 'open-ils.storage.action.hold_request.nearest_hold',
72         api_level       => 1,
73         method          => 'nearest_hold',
74 );
75
76 sub next_resp_group_id {
77         my $self = shift;
78         my $client = shift;
79
80         # XXX This is not replication safe!!!
81
82         my ($id) = action::survey->db_Main->selectrow_array(<<" SQL");
83                 SELECT NEXTVAL('action.survey_response_group_id_seq'::TEXT)
84         SQL
85         return $id;
86 }
87 __PACKAGE__->register_method(
88         api_name        => 'open-ils.storage.action.survey_response.next_group_id',
89         api_level       => 1,
90         method          => 'next_resp_group_id',
91 );
92
93 sub patron_circ_summary {
94         my $self = shift;
95         my $client = shift;
96         my $id = ''.shift();
97
98         return undef unless ($id);
99         my $c_table = action::circulation->table;
100         my $b_table = money::billing->table;
101
102         $log->debug("Retrieving patron summary for id $id", DEBUG);
103
104         my $select = <<"        SQL";
105                 SELECT  COUNT(DISTINCT c.id), SUM( COALESCE(b.amount,0) )
106                   FROM  $c_table c
107                         LEFT OUTER JOIN $b_table b ON (c.id = b.xact AND b.voided = FALSE)
108                   WHERE c.usr = ?
109                         AND c.xact_finish IS NULL
110                         AND (
111                                 c.stop_fines NOT IN ('CLAIMSRETURNED','LOST')
112                                 OR c.stop_fines IS NULL
113                         )
114         SQL
115
116         return action::survey->db_Main->selectrow_arrayref($select, {}, $id);
117 }
118 __PACKAGE__->register_method(
119         api_name        => 'open-ils.storage.action.circulation.patron_summary',
120         api_level       => 1,
121         method          => 'patron_circ_summary',
122 );
123
124 #XXX Fix stored proc calls
125 sub find_local_surveys {
126         my $self = shift;
127         my $client = shift;
128         my $ou = ''.shift();
129
130         return undef unless ($ou);
131         my $s_table = action::survey->table;
132
133         my $select = <<"        SQL";
134                 SELECT  s.*
135                   FROM  $s_table s
136                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
137                   WHERE CURRENT_DATE BETWEEN s.start_date AND s.end_date
138         SQL
139
140         my $sth = action::survey->db_Main->prepare_cached($select);
141         $sth->execute($ou);
142
143         $client->respond( $_->to_fieldmapper ) for ( map { action::survey->construct($_) } $sth->fetchall_hash );
144
145         return undef;
146 }
147 __PACKAGE__->register_method(
148         api_name        => 'open-ils.storage.action.survey.all',
149         api_level       => 1,
150         stream          => 1,
151         method          => 'find_local_surveys',
152 );
153
154 #XXX Fix stored proc calls
155 sub find_opac_surveys {
156         my $self = shift;
157         my $client = shift;
158         my $ou = ''.shift();
159
160         return undef unless ($ou);
161         my $s_table = action::survey->table;
162
163         my $select = <<"        SQL";
164                 SELECT  s.*
165                   FROM  $s_table s
166                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
167                   WHERE CURRENT_DATE BETWEEN s.start_date AND s.end_date
168                         AND s.opac IS TRUE;
169         SQL
170
171         my $sth = action::survey->db_Main->prepare_cached($select);
172         $sth->execute($ou);
173
174         $client->respond( $_->to_fieldmapper ) for ( map { action::survey->construct($_) } $sth->fetchall_hash );
175
176         return undef;
177 }
178 __PACKAGE__->register_method(
179         api_name        => 'open-ils.storage.action.survey.opac',
180         api_level       => 1,
181         stream          => 1,
182         method          => 'find_opac_surveys',
183 );
184
185 sub find_optional_surveys {
186         my $self = shift;
187         my $client = shift;
188         my $ou = ''.shift();
189
190         return undef unless ($ou);
191         my $s_table = action::survey->table;
192
193         my $select = <<"        SQL";
194                 SELECT  s.*
195                   FROM  $s_table s
196                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
197                   WHERE CURRENT_DATE BETWEEN s.start_date AND s.end_date
198                         AND s.required IS FALSE;
199         SQL
200
201         my $sth = action::survey->db_Main->prepare_cached($select);
202         $sth->execute($ou);
203
204         $client->respond( $_->to_fieldmapper ) for ( map { action::survey->construct($_) } $sth->fetchall_hash );
205
206         return undef;
207 }
208 __PACKAGE__->register_method(
209         api_name        => 'open-ils.storage.action.survey.optional',
210         api_level       => 1,
211         stream          => 1,
212         method          => 'find_optional_surveys',
213 );
214
215 sub find_required_surveys {
216         my $self = shift;
217         my $client = shift;
218         my $ou = ''.shift();
219
220         return undef unless ($ou);
221         my $s_table = action::survey->table;
222
223         my $select = <<"        SQL";
224                 SELECT  s.*
225                   FROM  $s_table s
226                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
227                   WHERE CURRENT_DATE BETWEEN s.start_date AND s.end_date
228                         AND s.required IS TRUE;
229         SQL
230
231         my $sth = action::survey->db_Main->prepare_cached($select);
232         $sth->execute($ou);
233
234         $client->respond( $_->to_fieldmapper ) for ( map { action::survey->construct($_) } $sth->fetchall_hash );
235
236         return undef;
237 }
238 __PACKAGE__->register_method(
239         api_name        => 'open-ils.storage.action.survey.required',
240         api_level       => 1,
241         stream          => 1,
242         method          => 'find_required_surveys',
243 );
244
245 sub find_usr_summary_surveys {
246         my $self = shift;
247         my $client = shift;
248         my $ou = ''.shift();
249
250         return undef unless ($ou);
251         my $s_table = action::survey->table;
252
253         my $select = <<"        SQL";
254                 SELECT  s.*
255                   FROM  $s_table s
256                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
257                   WHERE CURRENT_DATE BETWEEN s.start_date AND s.end_date
258                         AND s.usr_summary IS TRUE;
259         SQL
260
261         my $sth = action::survey->db_Main->prepare_cached($select);
262         $sth->execute($ou);
263
264         $client->respond( $_->to_fieldmapper ) for ( map { action::survey->construct($_) } $sth->fetchall_hash );
265
266         return undef;
267 }
268 __PACKAGE__->register_method(
269         api_name        => 'open-ils.storage.action.survey.usr_summary',
270         api_level       => 1,
271         stream          => 1,
272         method          => 'find_usr_summary_surveys',
273 );
274
275
276 sub generate_fines {
277         my $self = shift;
278         my $client = shift;
279         my $grace = shift;
280         my $circ = shift;
281         
282         
283         my @circs;
284         if ($circ) {
285                 push @circs, action::circulation->search_where( { id => $circ, stop_fines => undef } );
286         } else {
287                 push @circs, overdue_circs($grace);
288         }
289
290         for my $c (@circs) {
291         
292                 try {
293                         my $due_dt = $parser->parse_datetime( clense_ISO8601( $c->due_date ) );
294         
295                         my $due = $due_dt->epoch;
296                         my $now = time;
297                         my $fine_interval = interval_to_seconds( $c->fine_interval );
298         
299                         if ( interval_to_seconds( $c->fine_interval ) >= interval_to_seconds('1d') ) {  
300                                 my $tz_offset_s = 0;;
301                                 if ($due_dt->strftime('%z') =~ /(-|\+)(\d{2}):?(\d{2})/) {
302                                         $tz_offset_s = $1 . interval_to_seconds( "${2}h ${3}m"); 
303                                 }
304         
305                                 $due -= ($due % $fine_interval) + $tz_offset_s;
306                                 $now -= ($now % $fine_interval) + $tz_offset_s;
307                         }
308         
309                         $client->respond(
310                                 "ARG! Overdue circulation ".$c->id.
311                                 " for item ".$c->target_copy.
312                                 " (user ".$c->usr.").\n".
313                                 "\tItem was due on or before: ".localtime($due)."\n");
314         
315                         my ($fine) = money::billing->search(
316                                 xact => $c->id, voided => 'f',
317                                 { order_by => 'billing_ts DESC', limit => '1' }
318                         );
319         
320                         my $last_fine;
321                         if ($fine) {
322                                 $last_fine = $parser->parse_datetime( clense_ISO8601( $fine->billing_ts ) )->epoch;
323                         } else {
324                                 $last_fine = $due;
325                                 $last_fine += $fine_interval * $grace;
326                         }
327         
328                         my $pending_fine_count = int( ($now - $last_fine) / $fine_interval ); 
329                         unless($pending_fine_count) {
330                                 $client->respond( "\tNo fines to create.  " );
331                                 if ($grace && $now < $due + $fine_interval * $grace) {
332                                         $client->respond( "Still inside grace period of: ". seconds_to_interval( $fine_interval * $grace)."\n" );
333                                 } else {
334                                         $client->respond( "Last fine generated for: ".localtime($last_fine)."\n" );
335                                 }
336                                 next;
337                         }
338         
339                         $client->respond( "\t$pending_fine_count pending fine(s)\n" );
340         
341                         for my $bill (1 .. $pending_fine_count) {
342         
343                                 my ($total) = money::billable_transaction_summary->retrieve( $c->id );
344         
345                                 if ($total && $total->balance_owed > $c->max_fine) {
346                                         $c->update({stop_fines => 'MAXFINES'});
347                                         $client->respond(
348                                                 "\tMaximum fine level of ".$c->max_fine.
349                                                 " reached for this circulation.\n".
350                                                 "\tNo more fines will be generated.\n" );
351                                         last;
352                                 }
353         
354                                 my $billing = money::billing->create(
355                                         { xact          => ''.$c->id,
356                                           note          => "Overdue Fine",
357                                           billing_type  => "Overdue materials",
358                                           amount        => ''.$c->recuring_fine,
359                                           billing_ts    => DateTime->from_epoch( epoch => $last_fine + $fine_interval * $bill )->strftime('%FT%T%z')
360                                         }
361                                 );
362         
363                                 $client->respond(
364                                         "\t\tCreating fine of ".$billing->amount." for period starting ".
365                                         localtime(
366                                                 $parser->parse_datetime(
367                                                         clense_ISO8601( $billing->billing_ts )
368                                                 )->epoch
369                                         )."\n" );
370                         }
371                 } catch Error with {
372                         my $e = shift;
373                         $client->respond( "Error processing overdue circulation [".$c->id."]:\n\n$e\n" );
374                 };
375         }
376 }
377 __PACKAGE__->register_method(
378         api_name        => 'open-ils.storage.action.circulation.overdue.generate_fines',
379         api_level       => 1,
380         stream          => 1,
381         method          => 'generate_fines',
382 );
383
384
385
386 my $locations;
387 my $statuses;
388 my %cache = (titles => {}, cns => {});
389 sub hold_copy_targeter {
390         my $self = shift;
391         my $client = shift;
392         my $check_expire = shift;
393         my $one_hold = shift;
394
395         $self->{user_filter} = OpenSRF::AppSession->create('open-ils.circ');
396         $self->{user_filter}->connect;
397         $self->{client} = $client;
398
399         my $time = time;
400         $check_expire ||= '12h';
401         $check_expire = interval_to_seconds( $check_expire );
402
403         my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time() - $check_expire);
404         $year += 1900;
405         $mon += 1;
406         my $expire_threshold = sprintf(
407                 '%s-%0.2d-%0.2dT%0.2d:%0.2d:%0.2d-00',
408                 $year, $mon, $mday, $hour, $min, $sec
409         );
410
411
412         $statuses ||= [ config::copy_status->search(holdable => 't') ];
413
414         $locations ||= [ asset::copy_location->search(holdable => 't') ];
415
416         my $holds;
417
418         %cache = (titles => {}, cns => {});
419
420         try {
421                 if ($one_hold) {
422                         $holds = [ action::hold_request->search(id => $one_hold) ];
423                 } else {
424                         $holds = [ action::hold_request->search_where(
425                                                         { capture_time => undef,
426                                                           prev_check_time => { '<=' => $expire_threshold },
427                                                         },
428                                                         { order_by => 'request_time,prev_check_time' } ) ];
429                         push @$holds, action::hold_request->search(
430                                                         capture_time => undef,
431                                                         prev_check_time => undef,
432                                                         { order_by => 'request_time' } );
433                 }
434         } catch Error with {
435                 my $e = shift;
436                 die "Could not retrieve uncaptured hold requests:\n\n$e\n";
437         };
438
439         for my $hold (@$holds) {
440                 try {
441                         #action::hold_request->db_Main->begin_work;
442                         if ($self->method_lookup('open-ils.storage.transaction.current')->run) {
443                                 $client->respond("Cleaning up after previous transaction\n");
444                                 $self->method_lookup('open-ils.storage.transaction.rollback')->run;
445                         }
446                         $self->method_lookup('open-ils.storage.transaction.begin')->run( $client );
447                         $client->respond("Processing hold ".$hold->id."...\n");
448
449                         my $copies;
450
451                         $copies = $self->metarecord_hold_capture($hold) if ($hold->hold_type eq 'M');
452                         $self->{client}->status( new OpenSRF::DomainObject::oilsContinueStatus );
453
454                         $copies = $self->title_hold_capture($hold) if ($hold->hold_type eq 'T');
455                         $self->{client}->status( new OpenSRF::DomainObject::oilsContinueStatus );
456                         
457                         $copies = $self->volume_hold_capture($hold) if ($hold->hold_type eq 'V');
458                         $self->{client}->status( new OpenSRF::DomainObject::oilsContinueStatus );
459                         
460                         $copies = $self->copy_hold_capture($hold) if ($hold->hold_type eq 'C');
461
462                         unless (ref $copies || !@$copies) {
463                                 $client->respond("\tNo copies available for targeting at all!\n");
464                         }
465
466                         my @good_copies;
467                         for my $c (@$copies) {
468                                 next if ( grep {$c->id == $hold->current_copy} @good_copies);
469                                 push @good_copies, $c if ($c);
470                         }
471
472                         $client->respond("\t".scalar(@good_copies)." (non-current) copies available for targeting...\n");
473
474                         my $old_best = $hold->current_copy;
475                         $hold->update({ current_copy => undef });
476         
477                         if (!scalar(@good_copies)) {
478                                 $client->respond("\tNo (non-current) copies available to fill the hold.\n");
479                                 if ( $old_best && grep {$c->id == $hold->current_copy} @$copies ) {
480                                         $client->respond("\tPushing current_copy back onto the targeting list\n");
481                                         push @good_copies, asset::copy->retrieve( $old_best );
482                                 } else {
483                                         $client->respond("\tcurrent_copy is no longer available for targeting... NEXT HOLD, PLEASE!\n");
484                                         next;
485                                 }
486                         }
487
488                         my $prox_list;
489                         $$prox_list[0] = [grep {$_->circ_lib == $hold->pickup_lib } @good_copies];
490                         $copies = [grep {$_->circ_lib != $hold->pickup_lib } @good_copies];
491
492                         my $best = $self->choose_nearest_copy($hold, $prox_list);
493
494                         if (!$best) {
495                                 $prox_list = $self->create_prox_list( $hold->pickup_lib, $copies );
496                                 $best = $self->choose_nearest_copy($hold, $prox_list);
497                         }
498
499                         if ($old_best) {
500                                 # hold wasn't fulfilled, record the fact
501                         
502                                 $client->respond("\tHold was not (but should have been) fulfilled by ".$old_best->id.".\n");
503                                 action::unfulfilled_hold_list->create(
504                                                 { hold => ''.$hold->id,
505                                                   current_copy => ''.$old_best->id,
506                                                   circ_lib => ''.$old_best->circ_lib,
507                                                 });
508                         }
509
510                         if ($best) {
511                                 $hold->update( { current_copy => ''.$best->id } );
512                                 $client->respond("\tTargeting copy ".$best->id." for hold fulfillment.\n");
513                         }
514
515                         $hold->update( { prev_check_time => 'now' } );
516                         $client->respond("\tUpdating hold ".$hold->id." with new 'current_copy' for hold fulfillment.\n");
517
518                         $client->respond("\tProcessing of hold ".$hold->id." complete.\n");
519                         $self->method_lookup('open-ils.storage.transaction.commit')->run;
520
521                         #action::hold_request->dbi_commit;
522
523                 } otherwise {
524                         my $e = shift;
525                         $log->error("Processing of hold failed:  $e");
526                         $client->respond("\tProcessing of hold failed!.\n\t\t$e\n");
527                         $self->method_lookup('open-ils.storage.transaction.rollback')->run;
528                         #action::hold_request->dbi_rollback;
529                 };
530         }
531
532         $self->{user_filter}->disconnect;
533         $self->{user_filter}->finish;
534         delete $$self{user_filter};
535         return undef;
536 }
537 __PACKAGE__->register_method(
538         api_name        => 'open-ils.storage.action.hold_request.copy_targeter',
539         api_level       => 1,
540         stream          => 1,
541         method          => 'hold_copy_targeter',
542 );
543
544
545
546 sub copy_hold_capture {
547         my $self = shift;
548         my $hold = shift;
549         my $cps = shift;
550
551         if (!defined($cps)) {
552                 try {
553                         $cps = [ asset::copy->search( id => $hold->target ) ];
554                 } catch Error with {
555                         my $e = shift;
556                         die "Could not retrieve initial volume list:\n\n$e\n";
557                 };
558         }
559
560         my @copies = grep { $_->holdable and !$_->ref } @$cps;
561
562         for (my $i = 0; $i < @copies; $i++) {
563                 next unless $copies[$i];
564                 
565                 my $cn = $cache{cns}{$copies[$i]->call_number};
566                 my $rec = $cache{titles}{$cn->record};
567                 $copies[$i] = undef if ($copies[$i] && !grep{ $copies[$i]->status eq $_->id}@$statuses);
568                 $copies[$i] = undef if ($copies[$i] && !grep{ $copies[$i]->location eq $_->id}@$locations);
569                 $copies[$i] = undef if (
570                         !$copies[$i] ||
571                         !$self->{user_filter}->request(
572                                 'open-ils.circ.permit_hold',
573                                 $hold->to_fieldmapper, do {
574                                         my $cp_fm = $copies[$i]->to_fieldmapper;
575                                         $cp_fm->circ_lib( $copies[$i]->circ_lib->to_fieldmapper );
576                                         $cp_fm->location( $copies[$i]->location->to_fieldmapper );
577                                         $cp_fm->status( $copies[$i]->status->to_fieldmapper );
578                                         $cp_fm;
579                                 },
580                                 { title => $rec->to_fieldmapper,
581                                   usr => actor::user->retrieve($hold->usr)->to_fieldmapper,
582                                   requestor => actor::user->retrieve($hold->requestor)->to_fieldmapper,
583                                 })->gather(1)
584                 );
585                 $self->{client}->status( new OpenSRF::DomainObject::oilsContinueStatus );
586         }
587
588         @copies = grep { $_ } @copies;
589
590         my $count = @copies;
591
592         return unless ($count);
593         
594         action::hold_copy_map->search( { hold => $hold->id } )->delete_all;
595         
596         my @maps;
597         $self->{client}->respond( "\tMapping ".scalar(@copies)." eligable copies for hold ".$hold->id."\n");
598         for my $c (@copies) {
599                 push @maps, action::hold_copy_map->create( { hold => $hold->id, target_copy => $c->id } );
600         }
601         $self->{client}->respond( "\tA total of ".scalar(@maps)." mapping were created for hold ".$hold->id."\n");
602
603         return \@copies;
604 }
605
606
607 sub choose_nearest_copy {
608         my $self = shift;
609         my $hold = shift;
610         my $prox_list = shift;
611
612         for my $p ( 0 .. int( scalar(@$prox_list) - 1) ) {
613                 next unless (ref $$prox_list[$p]);
614                 my @capturable = grep { $_->status == 0 } @{ $$prox_list[$p] };
615                 next unless (@capturable);
616                 return $capturable[rand(scalar(@capturable))];
617         }
618 }
619
620 sub create_prox_list {
621         my $self = shift;
622         my $lib = shift;
623         my $copies = shift;
624
625         my @prox_list;
626         for my $cp (@$copies) {
627                 my ($prox) = $self->method_lookup('open-ils.storage.asset.copy.proximity')->run( $cp->id, $lib );
628                 $prox_list[$prox] = [] unless defined($prox_list[$prox]);
629                 push @{$prox_list[$prox]}, $cp;
630         }
631         return \@prox_list;
632 }
633
634 sub volume_hold_capture {
635         my $self = shift;
636         my $hold = shift;
637         my $vols = shift;
638
639         if (!defined($vols)) {
640                 try {
641                         $vols = [ asset::call_number->search( id => $hold->target ) ];
642                         $cache{cns}{$_->id} = $_ for (@$vols);
643                 } catch Error with {
644                         my $e = shift;
645                         die "Could not retrieve initial volume list:\n\n$e\n";
646                 };
647         }
648
649         my @v_ids = map { $_->id } @$vols;
650
651         my $cp_list;
652         try {
653                 $cp_list = [ asset::copy->search( call_number => \@v_ids ) ];
654         
655         } catch Error with {
656                 my $e = shift;
657                 warn "Could not retrieve copy list:\n\n$e\n";
658         };
659
660         $self->copy_hold_capture($hold,$cp_list) if (ref $cp_list and @$cp_list);
661 }
662
663 sub title_hold_capture {
664         my $self = shift;
665         my $hold = shift;
666         my $titles = shift;
667
668         if (!defined($titles)) {
669                 try {
670                         $titles = [ biblio::record_entry->search( id => $hold->target ) ];
671                         $cache{titles}{$_->id} = $_ for (@$titles);
672                 } catch Error with {
673                         my $e = shift;
674                         die "Could not retrieve initial title list:\n\n$e\n";
675                 };
676         }
677
678         my @t_ids = map { $_->id } @$titles;
679         my $cn_list;
680         try {
681                 ($cn_list) = $self->method_lookup('open-ils.storage.direct.asset.call_number.search.record.atomic')->run( \@t_ids );
682         
683         } catch Error with {
684                 my $e = shift;
685                 warn "Could not retrieve volume list:\n\n$e\n";
686         };
687
688         $cache{cns}{$_->id} = $_ for (@$cn_list);
689
690         $self->volume_hold_capture($hold,$cn_list) if (ref $cn_list and @$cn_list);
691 }
692
693 sub metarecord_hold_capture {
694         my $self = shift;
695         my $hold = shift;
696
697         my $titles;
698         try {
699                 $titles = [ metabib::metarecord_source_map->search( metarecord => $hold->target) ];
700         
701         } catch Error with {
702                 my $e = shift;
703                 die "Could not retrieve initial title list:\n\n$e\n";
704         };
705
706         try {
707                 my @recs = map {$_->record} metabib::record_descriptor->search( record => $titles, item_type => [split '', $hold->holdable_formats] ); 
708
709                 $titles = [ biblio::record_entry->search( id => \@recs ) ];
710         
711         } catch Error with {
712                 my $e = shift;
713                 die "Could not retrieve format-pruned title list:\n\n$e\n";
714         };
715
716
717         $cache{titles}{$_->id} = $_ for (@$titles);
718         $self->title_hold_capture($hold,$titles) if (ref $titles and @$titles);
719 }
720
721 1;