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