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 OpenILS::Utils::PermitHold;
10 use DateTime::Format::ISO8601;
13 my $parser = DateTime::Format::ISO8601->new;
14 my $log = 'OpenSRF::Utils::Logger';
16 sub ou_hold_requests {
21 my $h_table = action::hold_request->table;
22 my $c_table = asset::copy->table;
23 my $o_table = actor::org_unit->table;
28 JOIN $c_table cp ON (cp.id = h.current_copy)
29 JOIN $o_table ou ON (ou.id = cp.circ_lib)
31 AND h.capture_time IS NULL
32 ORDER BY h.request_time
35 my $sth = action::hold_request->db_Main->prepare_cached($SQL);
38 $client->respond($_) for (
41 ->method_lookup('open-ils.storage.direct.action.hold_request.retrieve')
45 } @{ $sth->fetchall_arrayref }
49 __PACKAGE__->register_method(
50 api_name => 'open-ils.storage.action.targeted_hold_request.org_unit',
54 method => 'ou_hold_requests',
61 my $c_t = action::circulation->table;
63 $grace = " - ($grace * (fine_interval))" if ($grace);
68 WHERE stop_fines IS NULL
69 AND due_date < ( CURRENT_TIMESTAMP $grace)
72 my $sth = action::circulation->db_Main->prepare_cached($sql);
75 return ( map { action::circulation->construct($_) } $sth->fetchall_hash );
82 my $grace = shift || '';
84 $client->respond( $_->to_fieldmapper ) for ( overdue_circs($grace) );
89 __PACKAGE__->register_method(
90 api_name => 'open-ils.storage.action.circulation.overdue',
93 method => 'grab_overdue',
102 my ($id) = action::hold_request->db_Main->selectrow_array(<<" SQL", {}, $pl,$cp);
104 FROM action.hold_request h
105 JOIN action.hold_copy_map hm ON (hm.hold = h.id)
106 WHERE h.pickup_lib = ?
107 AND hm.target_copy = ?
108 AND h.capture_time IS NULL
109 ORDER BY h.pickup_lib - (SELECT home_ou FROM actor.usr a WHERE a.id = h.usr), h.selection_depth DESC, h.request_time
114 __PACKAGE__->register_method(
115 api_name => 'open-ils.storage.action.hold_request.nearest_hold',
117 method => 'nearest_hold',
120 sub next_resp_group_id {
124 # XXX This is not replication safe!!!
126 my ($id) = action::survey->db_Main->selectrow_array(<<" SQL");
127 SELECT NEXTVAL('action.survey_response_group_id_seq'::TEXT)
131 __PACKAGE__->register_method(
132 api_name => 'open-ils.storage.action.survey_response.next_group_id',
134 method => 'next_resp_group_id',
137 sub patron_circ_summary {
142 return undef unless ($id);
143 my $c_table = action::circulation->table;
144 my $b_table = money::billing->table;
146 $log->debug("Retrieving patron summary for id $id", DEBUG);
148 my $select = <<" SQL";
149 SELECT COUNT(DISTINCT c.id), SUM( COALESCE(b.amount,0) )
151 LEFT OUTER JOIN $b_table b ON (c.id = b.xact AND b.voided = FALSE)
153 AND c.xact_finish IS NULL
155 c.stop_fines NOT IN ('CLAIMSRETURNED','LOST')
156 OR c.stop_fines IS NULL
160 return action::survey->db_Main->selectrow_arrayref($select, {}, $id);
162 __PACKAGE__->register_method(
163 api_name => 'open-ils.storage.action.circulation.patron_summary',
165 method => 'patron_circ_summary',
168 #XXX Fix stored proc calls
169 sub find_local_surveys {
174 return undef unless ($ou);
175 my $s_table = action::survey->table;
177 my $select = <<" SQL";
180 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
181 WHERE CURRENT_DATE BETWEEN s.start_date AND s.end_date
184 my $sth = action::survey->db_Main->prepare_cached($select);
187 $client->respond( $_->to_fieldmapper ) for ( map { action::survey->construct($_) } $sth->fetchall_hash );
191 __PACKAGE__->register_method(
192 api_name => 'open-ils.storage.action.survey.all',
195 method => 'find_local_surveys',
198 #XXX Fix stored proc calls
199 sub find_opac_surveys {
204 return undef unless ($ou);
205 my $s_table = action::survey->table;
207 my $select = <<" SQL";
210 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
211 WHERE CURRENT_DATE BETWEEN s.start_date AND s.end_date
215 my $sth = action::survey->db_Main->prepare_cached($select);
218 $client->respond( $_->to_fieldmapper ) for ( map { action::survey->construct($_) } $sth->fetchall_hash );
222 __PACKAGE__->register_method(
223 api_name => 'open-ils.storage.action.survey.opac',
226 method => 'find_opac_surveys',
229 sub find_optional_surveys {
234 return undef unless ($ou);
235 my $s_table = action::survey->table;
237 my $select = <<" SQL";
240 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
241 WHERE CURRENT_DATE BETWEEN s.start_date AND s.end_date
242 AND s.required IS FALSE;
245 my $sth = action::survey->db_Main->prepare_cached($select);
248 $client->respond( $_->to_fieldmapper ) for ( map { action::survey->construct($_) } $sth->fetchall_hash );
252 __PACKAGE__->register_method(
253 api_name => 'open-ils.storage.action.survey.optional',
256 method => 'find_optional_surveys',
259 sub find_required_surveys {
264 return undef unless ($ou);
265 my $s_table = action::survey->table;
267 my $select = <<" SQL";
270 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
271 WHERE CURRENT_DATE BETWEEN s.start_date AND s.end_date
272 AND s.required IS TRUE;
275 my $sth = action::survey->db_Main->prepare_cached($select);
278 $client->respond( $_->to_fieldmapper ) for ( map { action::survey->construct($_) } $sth->fetchall_hash );
282 __PACKAGE__->register_method(
283 api_name => 'open-ils.storage.action.survey.required',
286 method => 'find_required_surveys',
289 sub find_usr_summary_surveys {
294 return undef unless ($ou);
295 my $s_table = action::survey->table;
297 my $select = <<" SQL";
300 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
301 WHERE CURRENT_DATE BETWEEN s.start_date AND s.end_date
302 AND s.usr_summary IS TRUE;
305 my $sth = action::survey->db_Main->prepare_cached($select);
308 $client->respond( $_->to_fieldmapper ) for ( map { action::survey->construct($_) } $sth->fetchall_hash );
312 __PACKAGE__->register_method(
313 api_name => 'open-ils.storage.action.survey.usr_summary',
316 method => 'find_usr_summary_surveys',
329 push @circs, action::circulation->search_where( { id => $circ, stop_fines => undef } );
331 push @circs, overdue_circs($grace);
337 my $due_dt = $parser->parse_datetime( clense_ISO8601( $c->due_date ) );
339 my $due = $due_dt->epoch;
341 my $fine_interval = interval_to_seconds( $c->fine_interval );
343 if ( interval_to_seconds( $c->fine_interval ) >= interval_to_seconds('1d') ) {
344 my $tz_offset_s = 0;;
345 if ($due_dt->strftime('%z') =~ /(-|\+)(\d{2}):?(\d{2})/) {
346 $tz_offset_s = $1 . interval_to_seconds( "${2}h ${3}m");
349 $due -= ($due % $fine_interval) + $tz_offset_s;
350 $now -= ($now % $fine_interval) + $tz_offset_s;
354 "ARG! Overdue circulation ".$c->id.
355 " for item ".$c->target_copy.
356 " (user ".$c->usr.").\n".
357 "\tItem was due on or before: ".localtime($due)."\n");
359 my ($fine) = money::billing->search(
360 xact => $c->id, voided => 'f',
361 { order_by => 'billing_ts DESC', limit => '1' }
366 $last_fine = $parser->parse_datetime( clense_ISO8601( $fine->billing_ts ) )->epoch;
369 $last_fine += $fine_interval * $grace;
372 my $pending_fine_count = int( ($now - $last_fine) / $fine_interval );
373 unless($pending_fine_count) {
374 $client->respond( "\tNo fines to create. " );
375 if ($grace && $now < $due + $fine_interval * $grace) {
376 $client->respond( "Still inside grace period of: ". seconds_to_interval( $fine_interval * $grace)."\n" );
378 $client->respond( "Last fine generated for: ".localtime($last_fine)."\n" );
383 $client->respond( "\t$pending_fine_count pending fine(s)\n" );
385 for my $bill (1 .. $pending_fine_count) {
387 my ($total) = money::billable_transaction_summary->retrieve( $c->id );
389 if ($total && $total->balance_owed > $c->max_fine) {
390 $c->update({stop_fines => 'MAXFINES'});
392 "\tMaximum fine level of ".$c->max_fine.
393 " reached for this circulation.\n".
394 "\tNo more fines will be generated.\n" );
398 my $billing = money::billing->create(
400 note => "Overdue Fine",
401 billing_type => "Overdue materials",
402 amount => ''.$c->recuring_fine,
403 billing_ts => DateTime->from_epoch( epoch => $last_fine + $fine_interval * $bill )->strftime('%FT%T%z')
408 "\t\tCreating fine of ".$billing->amount." for period starting ".
410 $parser->parse_datetime(
411 clense_ISO8601( $billing->billing_ts )
417 $client->respond( "Error processing overdue circulation [".$c->id."]:\n\n$e\n" );
421 __PACKAGE__->register_method(
422 api_name => 'open-ils.storage.action.circulation.overdue.generate_fines',
425 method => 'generate_fines',
430 sub new_hold_copy_targeter {
433 my $check_expire = shift;
434 my $one_hold = shift;
442 $check_expire ||= '12h';
443 $check_expire = interval_to_seconds( $check_expire );
445 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time() - $check_expire);
448 my $expire_threshold = sprintf(
449 '%s-%0.2d-%0.2dT%0.2d:%0.2d:%0.2d-00',
450 $year, $mon, $mday, $hour, $min, $sec
453 $holds = [ action::hold_request->search_where(
455 fulfillment_time => undef,
456 prev_check_time => [ undef, { '<=' => $expire_threshold } ] }
458 } elsif ( $check_expire ) {
461 $check_expire ||= '12h';
462 $check_expire = interval_to_seconds( $check_expire );
464 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time() - $check_expire);
467 my $expire_threshold = sprintf(
468 '%s-%0.2d-%0.2dT%0.2d:%0.2d:%0.2d-00',
469 $year, $mon, $mday, $hour, $min, $sec
472 $holds = [ action::hold_request->search_where(
473 { capture_time => undef,
474 fulfillment_time => undef,
475 prev_check_time => { '<=' => $expire_threshold },
477 { order_by => 'selection_depth DESC, request_time,prev_check_time' } ) ];
478 push @$holds, action::hold_request->search(
479 capture_time => undef,
480 fulfillment_time => undef,
481 prev_check_time => undef,
482 { order_by => 'selection_depth DESC, request_time' } );
484 $holds [ action::hold_request->search(
485 capture_time => undef,
486 fulfillment_time => undef,
487 prev_check_time => undef,
488 { order_by => 'selection_depth DESC, request_time' } ) ];
492 die "Could not retrieve uncaptured hold requests:\n\n$e\n";
496 for my $hold (@$holds) {
498 #action::hold_request->db_Main->begin_work;
499 if ($self->method_lookup('open-ils.storage.transaction.current')->run) {
500 $log->debug("Cleaning up after previous transaction\n");
501 $self->method_lookup('open-ils.storage.transaction.rollback')->run;
503 $self->method_lookup('open-ils.storage.transaction.begin')->run( $client );
504 $log->info("Processing hold ".$hold->id."...\n");
506 action::hold_copy_map->search( { hold => $hold->id } )->delete_all;
510 # find all the potential copies
511 if ($hold->hold_type eq 'M') {
514 metabib::record_descriptor
516 record => [ map { $_->id } metabib::metarecord
517 ->retrieve($hold->target)
519 item_type => [split '', $hold->holdable_formats]
523 ->method_lookup( 'open-ils.storage.biblio.record_entry.ranged_tree')
524 ->run( $r->id, $hold->usr->home_ou->id, $hold->selection_depth );
526 for my $cn ( @{ $rtree->call_numbers } ) {
528 asset::copy->search( id => [map {$_->id} @{ $cn->copies }] );
531 } elsif ($hold->hold_type eq 'T') {
533 ->method_lookup( 'open-ils.storage.biblio.record_entry.ranged_tree')
534 ->run( $hold->target, $hold->usr->home_ou->id, $hold->selection_depth );
537 push @successes, { hold => $hold->id, eligible_copies => 0, error => 'NO_RECORD' };
541 for my $cn ( @{ $rtree->call_numbers } ) {
543 asset::copy->search( id => [map {$_->id} @{ $cn->copies }] );
545 } elsif ($hold->hold_type eq 'V') {
547 ->method_lookup( 'open-ils.storage.asset.call_number.ranged_tree')
548 ->run( $hold->target, $hold->usr->home_ou->id, $hold->selection_depth );
551 asset::copy->search( id => [map {$_->id} @{ $vtree->copies }] );
553 } elsif ($hold->hold_type eq 'C') {
555 $all_copies = [asset::copy->retrieve($hold->target)];
558 @$all_copies = grep { $_->status->holdable &&
559 $_->location->holdable &&
563 # let 'em know we're still working
564 $client->status( new OpenSRF::DomainObject::oilsContinueStatus );
566 if (!ref $all_copies || !@$all_copies) {
567 $log->info("\tNo copies available for targeting at all!\n");
568 $self->method_lookup('open-ils.storage.transaction.commit')->run;
569 push @successes, { hold => $hold->id, eligible_copies => 0, error => 'NO_COPIES' };
574 for my $c ( @$all_copies ) {
576 if ( OpenILS::Utils::PermitHold::permit_copy_hold(
577 { title => $c->call_number->record->to_fieldmapper,
578 title_descriptor => $c->call_number->record->record_descriptor->next->to_fieldmapper,
579 patron => $hold->usr->to_fieldmapper,
580 copy => $c->to_fieldmapper,
581 requestor => $hold->requestor->to_fieldmapper,
582 request_lib => $hold->request_lib->to_fieldmapper,
585 my $copy_count = @$copies;
586 $client->status( new OpenSRF::DomainObject::oilsContinueStatus );
588 # map the potentials, so that we can pick up checkins
589 $log->debug( "\tMapping ".scalar(@$copies)." potential copies for hold ".$hold->id);
590 action::hold_copy_map->create( { hold => $hold->id, target_copy => $_->id } ) for (@$copies);
593 for my $c (@$copies) {
594 next if ($c->id == $hold->current_copy);
595 push @good_copies, $c if ($c);
598 $log->debug("\t".scalar(@good_copies)." (non-current) copies available for targeting...");
600 my $old_best = $hold->current_copy;
601 $hold->update({ current_copy => undef });
603 if (!scalar(@good_copies)) {
604 $log->info("\tNo (non-current) copies eligible to fill the hold.");
605 if ( $old_best && grep { $old_best == $_ } @$copies ) {
606 $log->debug("\tPushing current_copy back onto the targeting list");
607 push @good_copies, $old_best;
609 $log->debug("\tcurrent_copy is no longer available for targeting... NEXT HOLD, PLEASE!");
610 $self->method_lookup('open-ils.storage.transaction.commit')->run;
611 push @successes, { hold => $hold->id, eligible_copies => 0, error => 'NO_TARGETS' };
616 $client->status( new OpenSRF::DomainObject::oilsContinueStatus );
621 $_->circ_lib == $hold->pickup_lib
625 $copies = [grep {$_->circ_lib != $hold->pickup_lib } @good_copies];
627 my $best = $self->choose_nearest_copy($hold, $prox_list);
630 $log->debug("\tNothing at the pickup lib, looking elsewhere among ".scalar(@$copies)." copies");
631 $prox_list = $self->create_prox_list( $hold->pickup_lib, $copies );
632 $best = $self->choose_nearest_copy($hold, $prox_list);
635 $client->status( new OpenSRF::DomainObject::oilsContinueStatus );
637 # hold wasn't fulfilled, record the fact
639 $log->info("\tHold was not (but should have been) fulfilled by ".$old_best->id);
640 action::unfulfilled_hold_list->create(
641 { hold => ''.$hold->id,
642 current_copy => ''.$old_best->id,
643 circ_lib => ''.$old_best->circ_lib,
648 $hold->update( { current_copy => ''.$best->id } );
649 $log->debug("\tUpdating hold [".$hold->id."] with new 'current_copy' [".$best->id."] for hold fulfillment.");
651 $log->info( "\tThere were no targetable copies for the hold" );
654 $hold->update( { prev_check_time => 'now' } );
656 $self->method_lookup('open-ils.storage.transaction.commit')->run;
657 $log->info("\tProcessing of hold ".$hold->id." complete.");
661 old_target => ($old_best ? $old_best->id : undef),
662 eligible_copies => $copy_count,
663 target => ($best ? $best->id : undef) };
668 $log->error("Processing of hold failed: $e");
669 $self->method_lookup('open-ils.storage.transaction.rollback')->run;
676 __PACKAGE__->register_method(
677 api_name => 'open-ils.storage.action.hold_request.copy_targeter',
679 method => 'new_hold_copy_targeter',
684 my %cache = (titles => {}, cns => {});
685 sub hold_copy_targeter {
688 my $check_expire = shift;
689 my $one_hold = shift;
691 $self->{user_filter} = OpenSRF::AppSession->create('open-ils.circ');
692 $self->{user_filter}->connect;
693 $self->{client} = $client;
696 $check_expire ||= '12h';
697 $check_expire = interval_to_seconds( $check_expire );
699 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time() - $check_expire);
702 my $expire_threshold = sprintf(
703 '%s-%0.2d-%0.2dT%0.2d:%0.2d:%0.2d-00',
704 $year, $mon, $mday, $hour, $min, $sec
708 $statuses ||= [ config::copy_status->search(holdable => 't') ];
710 $locations ||= [ asset::copy_location->search(holdable => 't') ];
714 %cache = (titles => {}, cns => {});
718 $holds = [ action::hold_request->search(id => $one_hold) ];
720 $holds = [ action::hold_request->search_where(
721 { capture_time => undef,
722 prev_check_time => { '<=' => $expire_threshold },
724 { order_by => 'request_time,prev_check_time' } ) ];
725 push @$holds, action::hold_request->search(
726 capture_time => undef,
727 prev_check_time => undef,
728 { order_by => 'request_time' } );
732 die "Could not retrieve uncaptured hold requests:\n\n$e\n";
735 for my $hold (@$holds) {
737 #action::hold_request->db_Main->begin_work;
738 if ($self->method_lookup('open-ils.storage.transaction.current')->run) {
739 $client->respond("Cleaning up after previous transaction\n");
740 $self->method_lookup('open-ils.storage.transaction.rollback')->run;
742 $self->method_lookup('open-ils.storage.transaction.begin')->run( $client );
743 $client->respond("Processing hold ".$hold->id."...\n");
747 $copies = $self->metarecord_hold_capture($hold) if ($hold->hold_type eq 'M');
748 $self->{client}->status( new OpenSRF::DomainObject::oilsContinueStatus );
750 $copies = $self->title_hold_capture($hold) if ($hold->hold_type eq 'T');
751 $self->{client}->status( new OpenSRF::DomainObject::oilsContinueStatus );
753 $copies = $self->volume_hold_capture($hold) if ($hold->hold_type eq 'V');
754 $self->{client}->status( new OpenSRF::DomainObject::oilsContinueStatus );
756 $copies = $self->copy_hold_capture($hold) if ($hold->hold_type eq 'C');
758 unless (ref $copies || !@$copies) {
759 $client->respond("\tNo copies available for targeting at all!\n");
763 for my $c (@$copies) {
764 next if ( grep {$c->id == $hold->current_copy} @good_copies);
765 push @good_copies, $c if ($c);
768 $client->respond("\t".scalar(@good_copies)." (non-current) copies available for targeting...\n");
770 my $old_best = $hold->current_copy;
771 $hold->update({ current_copy => undef });
773 if (!scalar(@good_copies)) {
774 $client->respond("\tNo (non-current) copies available to fill the hold.\n");
775 if ( $old_best && grep {$c->id == $hold->current_copy} @$copies ) {
776 $client->respond("\tPushing current_copy back onto the targeting list\n");
777 push @good_copies, asset::copy->retrieve( $old_best );
779 $client->respond("\tcurrent_copy is no longer available for targeting... NEXT HOLD, PLEASE!\n");
785 $$prox_list[0] = [grep {$_->circ_lib == $hold->pickup_lib } @good_copies];
786 $copies = [grep {$_->circ_lib != $hold->pickup_lib } @good_copies];
788 my $best = $self->choose_nearest_copy($hold, $prox_list);
791 $prox_list = $self->create_prox_list( $hold->pickup_lib, $copies );
792 $best = $self->choose_nearest_copy($hold, $prox_list);
796 # hold wasn't fulfilled, record the fact
798 $client->respond("\tHold was not (but should have been) fulfilled by ".$old_best->id.".\n");
799 action::unfulfilled_hold_list->create(
800 { hold => ''.$hold->id,
801 current_copy => ''.$old_best->id,
802 circ_lib => ''.$old_best->circ_lib,
807 $hold->update( { current_copy => ''.$best->id } );
808 $client->respond("\tTargeting copy ".$best->id." for hold fulfillment.\n");
811 $hold->update( { prev_check_time => 'now' } );
812 $client->respond("\tUpdating hold ".$hold->id." with new 'current_copy' for hold fulfillment.\n");
814 $client->respond("\tProcessing of hold ".$hold->id." complete.\n");
815 $self->method_lookup('open-ils.storage.transaction.commit')->run;
817 #action::hold_request->dbi_commit;
821 $log->error("Processing of hold failed: $e");
822 $client->respond("\tProcessing of hold failed!.\n\t\t$e\n");
823 $self->method_lookup('open-ils.storage.transaction.rollback')->run;
824 #action::hold_request->dbi_rollback;
828 $self->{user_filter}->disconnect;
829 $self->{user_filter}->finish;
830 delete $$self{user_filter};
833 __PACKAGE__->register_method(
834 api_name => 'open-ils.storage.action.hold_request.copy_targeter',
837 method => 'hold_copy_targeter',
841 sub copy_hold_capture {
846 if (!defined($cps)) {
848 $cps = [ asset::copy->search( id => $hold->target ) ];
851 die "Could not retrieve initial volume list:\n\n$e\n";
855 my @copies = grep { $_->holdable } @$cps;
857 for (my $i = 0; $i < @$cps; $i++) {
858 next unless $$cps[$i];
860 my $cn = $cache{cns}{$copies[$i]->call_number};
861 my $rec = $cache{titles}{$cn->record};
862 $copies[$i] = undef if ($copies[$i] && !grep{ $copies[$i]->status eq $_->id}@$statuses);
863 $copies[$i] = undef if ($copies[$i] && !grep{ $copies[$i]->location eq $_->id}@$locations);
864 $copies[$i] = undef if (
866 !$self->{user_filter}->request(
867 'open-ils.circ.permit_hold',
868 $hold->to_fieldmapper, do {
869 my $cp_fm = $copies[$i]->to_fieldmapper;
870 $cp_fm->circ_lib( $copies[$i]->circ_lib->to_fieldmapper );
871 $cp_fm->location( $copies[$i]->location->to_fieldmapper );
872 $cp_fm->status( $copies[$i]->status->to_fieldmapper );
875 { title => $rec->to_fieldmapper,
876 usr => actor::user->retrieve($hold->usr)->to_fieldmapper,
877 requestor => actor::user->retrieve($hold->requestor)->to_fieldmapper,
880 $self->{client}->status( new OpenSRF::DomainObject::oilsContinueStatus );
883 @copies = grep { $_ } @copies;
887 return unless ($count);
889 action::hold_copy_map->search( { hold => $hold->id } )->delete_all;
892 $self->{client}->respond( "\tMapping ".scalar(@copies)." eligable copies for hold ".$hold->id."\n");
893 for my $c (@copies) {
894 push @maps, action::hold_copy_map->create( { hold => $hold->id, target_copy => $c->id } );
896 $self->{client}->respond( "\tA total of ".scalar(@maps)." mapping were created for hold ".$hold->id."\n");
902 sub choose_nearest_copy {
905 my $prox_list = shift;
907 for my $p ( 0 .. int( scalar(@$prox_list) - 1) ) {
908 next unless (ref $$prox_list[$p]);
909 my @capturable = grep { $_->status == 0 || $_->status == 7 } @{ $$prox_list[$p] };
910 next unless (@capturable);
911 return $capturable[rand(scalar(@capturable))];
915 sub create_prox_list {
921 for my $cp (@$copies) {
922 my ($prox) = $self->method_lookup('open-ils.storage.asset.copy.proximity')->run( $cp->id, $lib );
923 next unless (defined($prox));
924 $prox_list[$prox] = [] unless defined($prox_list[$prox]);
925 push @{$prox_list[$prox]}, $cp;
930 sub volume_hold_capture {
935 if (!defined($vols)) {
937 $vols = [ asset::call_number->search( id => $hold->target ) ];
938 $cache{cns}{$_->id} = $_ for (@$vols);
941 die "Could not retrieve initial volume list:\n\n$e\n";
945 my @v_ids = map { $_->id } @$vols;
949 $cp_list = [ asset::copy->search( call_number => \@v_ids ) ];
953 warn "Could not retrieve copy list:\n\n$e\n";
956 $self->copy_hold_capture($hold,$cp_list) if (ref $cp_list and @$cp_list);
959 sub title_hold_capture {
964 if (!defined($titles)) {
966 $titles = [ biblio::record_entry->search( id => $hold->target ) ];
967 $cache{titles}{$_->id} = $_ for (@$titles);
970 die "Could not retrieve initial title list:\n\n$e\n";
974 my @t_ids = map { $_->id } @$titles;
977 ($cn_list) = $self->method_lookup('open-ils.storage.direct.asset.call_number.search.record.atomic')->run( \@t_ids );
981 warn "Could not retrieve volume list:\n\n$e\n";
984 $cache{cns}{$_->id} = $_ for (@$cn_list);
986 $self->volume_hold_capture($hold,$cn_list) if (ref $cn_list and @$cn_list);
989 sub metarecord_hold_capture {
995 $titles = [ metabib::metarecord_source_map->search( metarecord => $hold->target) ];
999 die "Could not retrieve initial title list:\n\n$e\n";
1003 my @recs = map {$_->record} metabib::record_descriptor->search( record => $titles, item_type => [split '', $hold->holdable_formats] );
1005 $titles = [ biblio::record_entry->search( id => \@recs ) ];
1007 } catch Error with {
1009 die "Could not retrieve format-pruned title list:\n\n$e\n";
1013 $cache{titles}{$_->id} = $_ for (@$titles);
1014 $self->title_hold_capture($hold,$titles) if (ref $titles and @$titles);