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