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