1 package OpenILS::Application::Storage::Publisher::action;
2 use base qw/OpenILS::Application::Storage::Publisher/;
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 );
79 sub complete_reshelving {
84 throw OpenSRF::EX::InvalidArg ("I need an interval of more than 0 seconds!")
85 unless (interval_to_seconds( $window ));
87 my $circ = action::circulation->table;
88 my $cp = asset::copy->table;
93 WHERE id IN ( SELECT cp.id
95 JOIN $circ circ ON (circ.target_copy = cp.id)
96 WHERE circ.checkin_time IS NOT NULL
97 AND circ.checkin_time < NOW() - CAST(? AS INTERVAL)
101 my $sth = action::circulation->db_Main->prepare_cached($sql);
102 $sth->execute($window);
107 __PACKAGE__->register_method(
108 api_name => 'open-ils.storage.action.circulation.reshelving.complete',
112 method => 'complete_reshelving',
118 my $grace = shift || '';
120 $client->respond( $_->to_fieldmapper ) for ( overdue_circs($grace) );
125 __PACKAGE__->register_method(
126 api_name => 'open-ils.storage.action.circulation.overdue',
129 method => 'grab_overdue',
138 my ($id) = action::hold_request->db_Main->selectrow_array(<<" SQL", {}, $pl,$cp);
140 FROM action.hold_request h
141 JOIN action.hold_copy_map hm ON (hm.hold = h.id)
142 WHERE h.pickup_lib = ?
143 AND hm.target_copy = ?
144 AND h.capture_time IS NULL
145 ORDER BY h.pickup_lib - (SELECT home_ou FROM actor.usr a WHERE a.id = h.usr), h.selection_depth DESC, h.request_time
150 __PACKAGE__->register_method(
151 api_name => 'open-ils.storage.action.hold_request.nearest_hold',
153 method => 'nearest_hold',
156 sub next_resp_group_id {
160 # XXX This is not replication safe!!!
162 my ($id) = action::survey->db_Main->selectrow_array(<<" SQL");
163 SELECT NEXTVAL('action.survey_response_group_id_seq'::TEXT)
167 __PACKAGE__->register_method(
168 api_name => 'open-ils.storage.action.survey_response.next_group_id',
170 method => 'next_resp_group_id',
173 sub patron_circ_summary {
178 return undef unless ($id);
179 my $c_table = action::circulation->table;
180 my $b_table = money::billing->table;
182 $log->debug("Retrieving patron summary for id $id", DEBUG);
184 my $select = <<" SQL";
185 SELECT COUNT(DISTINCT c.id), SUM( COALESCE(b.amount,0) )
187 LEFT OUTER JOIN $b_table b ON (c.id = b.xact AND b.voided = FALSE)
189 AND c.xact_finish IS NULL
191 c.stop_fines NOT IN ('CLAIMSRETURNED','LOST')
192 OR c.stop_fines IS NULL
196 return action::survey->db_Main->selectrow_arrayref($select, {}, $id);
198 __PACKAGE__->register_method(
199 api_name => 'open-ils.storage.action.circulation.patron_summary',
201 method => 'patron_circ_summary',
204 #XXX Fix stored proc calls
205 sub find_local_surveys {
210 return undef unless ($ou);
211 my $s_table = action::survey->table;
213 my $select = <<" SQL";
216 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
217 WHERE CURRENT_DATE BETWEEN s.start_date AND s.end_date
220 my $sth = action::survey->db_Main->prepare_cached($select);
223 $client->respond( $_->to_fieldmapper ) for ( map { action::survey->construct($_) } $sth->fetchall_hash );
227 __PACKAGE__->register_method(
228 api_name => 'open-ils.storage.action.survey.all',
231 method => 'find_local_surveys',
234 #XXX Fix stored proc calls
235 sub find_opac_surveys {
240 return undef unless ($ou);
241 my $s_table = action::survey->table;
243 my $select = <<" SQL";
246 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
247 WHERE CURRENT_DATE BETWEEN s.start_date AND s.end_date
251 my $sth = action::survey->db_Main->prepare_cached($select);
254 $client->respond( $_->to_fieldmapper ) for ( map { action::survey->construct($_) } $sth->fetchall_hash );
258 __PACKAGE__->register_method(
259 api_name => 'open-ils.storage.action.survey.opac',
262 method => 'find_opac_surveys',
269 my $limit = shift || 10;
270 my $offset = shift || 0;
272 return undef unless ($ou);
273 my $h_table = action::hold_request->table;
274 my $a_table = asset::copy->table;
276 my $select = <<" SQL";
279 JOIN $a_table a ON (h.current_copy = a.id)
281 AND h.capture_time IS NULL
282 ORDER BY h.request_time ASC
287 my $sth = action::survey->db_Main->prepare_cached($select);
290 $client->respond( $_->to_fieldmapper ) for ( map { action::hold_request->construct($_) } $sth->fetchall_hash );
294 __PACKAGE__->register_method(
295 api_name => 'open-ils.storage.direct.action.hold_request.pull_list.search.current_copy_circ_lib',
299 "Returns the holds for a specific library's pull list.",
300 [ [org_unit => "The library's org id", "number"],
301 [limit => 'An optional page size, defaults to 10', 'number'],
302 [offset => 'Offset for paging, defaults to 0, 0 based', 'number'],
304 ['A list of holds for the stated library to pull for', 'array']
306 method => 'hold_pull_list',
309 sub find_optional_surveys {
314 return undef unless ($ou);
315 my $s_table = action::survey->table;
317 my $select = <<" SQL";
320 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
321 WHERE CURRENT_DATE BETWEEN s.start_date AND s.end_date
322 AND s.required IS FALSE;
325 my $sth = action::survey->db_Main->prepare_cached($select);
328 $client->respond( $_->to_fieldmapper ) for ( map { action::survey->construct($_) } $sth->fetchall_hash );
332 __PACKAGE__->register_method(
333 api_name => 'open-ils.storage.action.survey.optional',
336 method => 'find_optional_surveys',
339 sub find_required_surveys {
344 return undef unless ($ou);
345 my $s_table = action::survey->table;
347 my $select = <<" SQL";
350 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
351 WHERE CURRENT_DATE BETWEEN s.start_date AND s.end_date
352 AND s.required IS TRUE;
355 my $sth = action::survey->db_Main->prepare_cached($select);
358 $client->respond( $_->to_fieldmapper ) for ( map { action::survey->construct($_) } $sth->fetchall_hash );
362 __PACKAGE__->register_method(
363 api_name => 'open-ils.storage.action.survey.required',
366 method => 'find_required_surveys',
369 sub find_usr_summary_surveys {
374 return undef unless ($ou);
375 my $s_table = action::survey->table;
377 my $select = <<" SQL";
380 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
381 WHERE CURRENT_DATE BETWEEN s.start_date AND s.end_date
382 AND s.usr_summary IS TRUE;
385 my $sth = action::survey->db_Main->prepare_cached($select);
388 $client->respond( $_->to_fieldmapper ) for ( map { action::survey->construct($_) } $sth->fetchall_hash );
392 __PACKAGE__->register_method(
393 api_name => 'open-ils.storage.action.survey.usr_summary',
396 method => 'find_usr_summary_surveys',
409 push @circs, action::circulation->search_where( { id => $circ, stop_fines => undef } );
411 push @circs, overdue_circs($grace);
417 my $due_dt = $parser->parse_datetime( clense_ISO8601( $c->due_date ) );
419 my $due = $due_dt->epoch;
421 my $fine_interval = interval_to_seconds( $c->fine_interval );
423 if ( interval_to_seconds( $c->fine_interval ) >= interval_to_seconds('1d') ) {
424 my $tz_offset_s = 0;;
425 if ($due_dt->strftime('%z') =~ /(-|\+)(\d{2}):?(\d{2})/) {
426 $tz_offset_s = $1 . interval_to_seconds( "${2}h ${3}m");
429 $due -= ($due % $fine_interval) + $tz_offset_s;
430 $now -= ($now % $fine_interval) + $tz_offset_s;
434 "ARG! Overdue circulation ".$c->id.
435 " for item ".$c->target_copy.
436 " (user ".$c->usr.").\n".
437 "\tItem was due on or before: ".localtime($due)."\n");
439 my ($fine) = money::billing->search(
440 xact => $c->id, voided => 'f',
441 { order_by => 'billing_ts DESC', limit => '1' }
446 $last_fine = $parser->parse_datetime( clense_ISO8601( $fine->billing_ts ) )->epoch;
449 $last_fine += $fine_interval * $grace;
452 my $pending_fine_count = int( ($now - $last_fine) / $fine_interval );
453 unless($pending_fine_count) {
454 $client->respond( "\tNo fines to create. " );
455 if ($grace && $now < $due + $fine_interval * $grace) {
456 $client->respond( "Still inside grace period of: ". seconds_to_interval( $fine_interval * $grace)."\n" );
458 $client->respond( "Last fine generated for: ".localtime($last_fine)."\n" );
463 $client->respond( "\t$pending_fine_count pending fine(s)\n" );
465 for my $bill (1 .. $pending_fine_count) {
467 my ($total) = money::billable_transaction_summary->retrieve( $c->id );
469 if ($total && $total->balance_owed > $c->max_fine) {
470 $c->update({stop_fines => 'MAXFINES'});
472 "\tMaximum fine level of ".$c->max_fine.
473 " reached for this circulation.\n".
474 "\tNo more fines will be generated.\n" );
478 my $billing = money::billing->create(
480 note => "Overdue Fine",
481 billing_type => "Overdue materials",
482 amount => ''.$c->recuring_fine,
483 billing_ts => DateTime->from_epoch( epoch => $last_fine + $fine_interval * $bill )->strftime('%FT%T%z')
488 "\t\tCreating fine of ".$billing->amount." for period starting ".
490 $parser->parse_datetime(
491 clense_ISO8601( $billing->billing_ts )
497 $client->respond( "Error processing overdue circulation [".$c->id."]:\n\n$e\n" );
501 __PACKAGE__->register_method(
502 api_name => 'open-ils.storage.action.circulation.overdue.generate_fines',
505 method => 'generate_fines',
510 sub new_hold_copy_targeter {
513 my $check_expire = shift;
514 my $one_hold = shift;
522 $check_expire ||= '12h';
523 $check_expire = interval_to_seconds( $check_expire );
525 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time() - $check_expire);
528 my $expire_threshold = sprintf(
529 '%s-%0.2d-%0.2dT%0.2d:%0.2d:%0.2d-00',
530 $year, $mon, $mday, $hour, $min, $sec
533 $holds = [ action::hold_request->search_where(
535 fulfillment_time => undef,
536 prev_check_time => [ undef, { '<=' => $expire_threshold } ] }
538 } elsif ( $check_expire ) {
541 $check_expire ||= '12h';
542 $check_expire = interval_to_seconds( $check_expire );
544 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time() - $check_expire);
547 my $expire_threshold = sprintf(
548 '%s-%0.2d-%0.2dT%0.2d:%0.2d:%0.2d-00',
549 $year, $mon, $mday, $hour, $min, $sec
552 $holds = [ action::hold_request->search_where(
553 { capture_time => undef,
554 fulfillment_time => undef,
555 prev_check_time => { '<=' => $expire_threshold },
557 { order_by => 'selection_depth DESC, request_time,prev_check_time' } ) ];
558 push @$holds, action::hold_request->search(
559 capture_time => undef,
560 fulfillment_time => undef,
561 prev_check_time => undef,
562 { order_by => 'selection_depth DESC, request_time' } );
564 $holds [ action::hold_request->search(
565 capture_time => undef,
566 fulfillment_time => undef,
567 prev_check_time => undef,
568 { order_by => 'selection_depth DESC, request_time' } ) ];
572 die "Could not retrieve uncaptured hold requests:\n\n$e\n";
576 for my $hold (@$holds) {
578 #action::hold_request->db_Main->begin_work;
579 if ($self->method_lookup('open-ils.storage.transaction.current')->run) {
580 $log->debug("Cleaning up after previous transaction\n");
581 $self->method_lookup('open-ils.storage.transaction.rollback')->run;
583 $self->method_lookup('open-ils.storage.transaction.begin')->run( $client );
584 $log->info("Processing hold ".$hold->id."...\n");
586 action::hold_copy_map->search( { hold => $hold->id } )->delete_all;
590 # find all the potential copies
591 if ($hold->hold_type eq 'M') {
594 metabib::record_descriptor
596 record => [ map { $_->id } metabib::metarecord
597 ->retrieve($hold->target)
599 item_type => [split '', $hold->holdable_formats]
603 ->method_lookup( 'open-ils.storage.biblio.record_entry.ranged_tree')
604 ->run( $r->id, $hold->usr->home_ou->id, $hold->selection_depth );
606 for my $cn ( @{ $rtree->call_numbers } ) {
608 asset::copy->search( id => [map {$_->id} @{ $cn->copies }] );
611 } elsif ($hold->hold_type eq 'T') {
613 ->method_lookup( 'open-ils.storage.biblio.record_entry.ranged_tree')
614 ->run( $hold->target, $hold->usr->home_ou->id, $hold->selection_depth );
617 push @successes, { hold => $hold->id, eligible_copies => 0, error => 'NO_RECORD' };
621 for my $cn ( @{ $rtree->call_numbers } ) {
623 asset::copy->search( id => [map {$_->id} @{ $cn->copies }] );
625 } elsif ($hold->hold_type eq 'V') {
627 ->method_lookup( 'open-ils.storage.asset.call_number.ranged_tree')
628 ->run( $hold->target, $hold->usr->home_ou->id, $hold->selection_depth );
631 asset::copy->search( id => [map {$_->id} @{ $vtree->copies }] );
633 } elsif ($hold->hold_type eq 'C') {
635 $all_copies = [asset::copy->retrieve($hold->target)];
638 @$all_copies = grep { $_->status->holdable &&
639 $_->location->holdable &&
643 # let 'em know we're still working
644 $client->status( new OpenSRF::DomainObject::oilsContinueStatus );
646 if (!ref $all_copies || !@$all_copies) {
647 $log->info("\tNo copies available for targeting at all!\n");
648 $self->method_lookup('open-ils.storage.transaction.commit')->run;
649 push @successes, { hold => $hold->id, eligible_copies => 0, error => 'NO_COPIES' };
654 for my $c ( @$all_copies ) {
656 if ( OpenILS::Utils::PermitHold::permit_copy_hold(
657 { title => $c->call_number->record->to_fieldmapper,
658 title_descriptor => $c->call_number->record->record_descriptor->next->to_fieldmapper,
659 patron => $hold->usr->to_fieldmapper,
660 copy => $c->to_fieldmapper,
661 requestor => $hold->requestor->to_fieldmapper,
662 request_lib => $hold->request_lib->to_fieldmapper,
665 my $copy_count = @$copies;
666 $client->status( new OpenSRF::DomainObject::oilsContinueStatus );
668 # map the potentials, so that we can pick up checkins
669 $log->debug( "\tMapping ".scalar(@$copies)." potential copies for hold ".$hold->id);
670 action::hold_copy_map->create( { hold => $hold->id, target_copy => $_->id } ) for (@$copies);
673 for my $c (@$copies) {
674 next if ($c->id == $hold->current_copy);
675 push @good_copies, $c if ($c);
678 $log->debug("\t".scalar(@good_copies)." (non-current) copies available for targeting...");
680 my $old_best = $hold->current_copy;
681 $hold->update({ current_copy => undef });
683 if (!scalar(@good_copies)) {
684 $log->info("\tNo (non-current) copies eligible to fill the hold.");
685 if ( $old_best && grep { $old_best == $_ } @$copies ) {
686 $log->debug("\tPushing current_copy back onto the targeting list");
687 push @good_copies, $old_best;
689 $log->debug("\tcurrent_copy is no longer available for targeting... NEXT HOLD, PLEASE!");
690 $self->method_lookup('open-ils.storage.transaction.commit')->run;
691 push @successes, { hold => $hold->id, eligible_copies => 0, error => 'NO_TARGETS' };
696 $client->status( new OpenSRF::DomainObject::oilsContinueStatus );
701 $_->circ_lib == $hold->pickup_lib
705 $copies = [grep {$_->circ_lib != $hold->pickup_lib } @good_copies];
707 my $best = $self->choose_nearest_copy($hold, $prox_list);
710 $log->debug("\tNothing at the pickup lib, looking elsewhere among ".scalar(@$copies)." copies");
711 $prox_list = $self->create_prox_list( $hold->pickup_lib, $copies );
712 $best = $self->choose_nearest_copy($hold, $prox_list);
715 $client->status( new OpenSRF::DomainObject::oilsContinueStatus );
717 # hold wasn't fulfilled, record the fact
719 $log->info("\tHold was not (but should have been) fulfilled by ".$old_best->id);
720 action::unfulfilled_hold_list->create(
721 { hold => ''.$hold->id,
722 current_copy => ''.$old_best->id,
723 circ_lib => ''.$old_best->circ_lib,
728 $hold->update( { current_copy => ''.$best->id } );
729 $log->debug("\tUpdating hold [".$hold->id."] with new 'current_copy' [".$best->id."] for hold fulfillment.");
731 $log->info( "\tThere were no targetable copies for the hold" );
734 $hold->update( { prev_check_time => 'now' } );
736 $self->method_lookup('open-ils.storage.transaction.commit')->run;
737 $log->info("\tProcessing of hold ".$hold->id." complete.");
741 old_target => ($old_best ? $old_best->id : undef),
742 eligible_copies => $copy_count,
743 target => ($best ? $best->id : undef) };
748 $log->error("Processing of hold failed: $e");
749 $self->method_lookup('open-ils.storage.transaction.rollback')->run;
756 __PACKAGE__->register_method(
757 api_name => 'open-ils.storage.action.hold_request.copy_targeter',
759 method => 'new_hold_copy_targeter',
764 my %cache = (titles => {}, cns => {});
765 sub hold_copy_targeter {
768 my $check_expire = shift;
769 my $one_hold = shift;
771 $self->{user_filter} = OpenSRF::AppSession->create('open-ils.circ');
772 $self->{user_filter}->connect;
773 $self->{client} = $client;
776 $check_expire ||= '12h';
777 $check_expire = interval_to_seconds( $check_expire );
779 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time() - $check_expire);
782 my $expire_threshold = sprintf(
783 '%s-%0.2d-%0.2dT%0.2d:%0.2d:%0.2d-00',
784 $year, $mon, $mday, $hour, $min, $sec
788 $statuses ||= [ config::copy_status->search(holdable => 't') ];
790 $locations ||= [ asset::copy_location->search(holdable => 't') ];
794 %cache = (titles => {}, cns => {});
798 $holds = [ action::hold_request->search(id => $one_hold) ];
800 $holds = [ action::hold_request->search_where(
801 { capture_time => undef,
802 prev_check_time => { '<=' => $expire_threshold },
804 { order_by => 'request_time,prev_check_time' } ) ];
805 push @$holds, action::hold_request->search(
806 capture_time => undef,
807 prev_check_time => undef,
808 { order_by => 'request_time' } );
812 die "Could not retrieve uncaptured hold requests:\n\n$e\n";
815 for my $hold (@$holds) {
817 #action::hold_request->db_Main->begin_work;
818 if ($self->method_lookup('open-ils.storage.transaction.current')->run) {
819 $client->respond("Cleaning up after previous transaction\n");
820 $self->method_lookup('open-ils.storage.transaction.rollback')->run;
822 $self->method_lookup('open-ils.storage.transaction.begin')->run( $client );
823 $client->respond("Processing hold ".$hold->id."...\n");
827 $copies = $self->metarecord_hold_capture($hold) if ($hold->hold_type eq 'M');
828 $self->{client}->status( new OpenSRF::DomainObject::oilsContinueStatus );
830 $copies = $self->title_hold_capture($hold) if ($hold->hold_type eq 'T');
831 $self->{client}->status( new OpenSRF::DomainObject::oilsContinueStatus );
833 $copies = $self->volume_hold_capture($hold) if ($hold->hold_type eq 'V');
834 $self->{client}->status( new OpenSRF::DomainObject::oilsContinueStatus );
836 $copies = $self->copy_hold_capture($hold) if ($hold->hold_type eq 'C');
838 unless (ref $copies || !@$copies) {
839 $client->respond("\tNo copies available for targeting at all!\n");
843 for my $c (@$copies) {
844 next if ( grep {$c->id == $hold->current_copy} @good_copies);
845 push @good_copies, $c if ($c);
848 $client->respond("\t".scalar(@good_copies)." (non-current) copies available for targeting...\n");
850 my $old_best = $hold->current_copy;
851 $hold->update({ current_copy => undef });
853 if (!scalar(@good_copies)) {
854 $client->respond("\tNo (non-current) copies available to fill the hold.\n");
855 if ( $old_best && grep {$c->id == $hold->current_copy} @$copies ) {
856 $client->respond("\tPushing current_copy back onto the targeting list\n");
857 push @good_copies, asset::copy->retrieve( $old_best );
859 $client->respond("\tcurrent_copy is no longer available for targeting... NEXT HOLD, PLEASE!\n");
865 $$prox_list[0] = [grep {$_->circ_lib == $hold->pickup_lib } @good_copies];
866 $copies = [grep {$_->circ_lib != $hold->pickup_lib } @good_copies];
868 my $best = $self->choose_nearest_copy($hold, $prox_list);
871 $prox_list = $self->create_prox_list( $hold->pickup_lib, $copies );
872 $best = $self->choose_nearest_copy($hold, $prox_list);
876 # hold wasn't fulfilled, record the fact
878 $client->respond("\tHold was not (but should have been) fulfilled by ".$old_best->id.".\n");
879 action::unfulfilled_hold_list->create(
880 { hold => ''.$hold->id,
881 current_copy => ''.$old_best->id,
882 circ_lib => ''.$old_best->circ_lib,
887 $hold->update( { current_copy => ''.$best->id } );
888 $client->respond("\tTargeting copy ".$best->id." for hold fulfillment.\n");
891 $hold->update( { prev_check_time => 'now' } );
892 $client->respond("\tUpdating hold ".$hold->id." with new 'current_copy' for hold fulfillment.\n");
894 $client->respond("\tProcessing of hold ".$hold->id." complete.\n");
895 $self->method_lookup('open-ils.storage.transaction.commit')->run;
897 #action::hold_request->dbi_commit;
901 $log->error("Processing of hold failed: $e");
902 $client->respond("\tProcessing of hold failed!.\n\t\t$e\n");
903 $self->method_lookup('open-ils.storage.transaction.rollback')->run;
904 #action::hold_request->dbi_rollback;
908 $self->{user_filter}->disconnect;
909 $self->{user_filter}->finish;
910 delete $$self{user_filter};
913 __PACKAGE__->register_method(
914 api_name => 'open-ils.storage.action.hold_request.copy_targeter',
917 method => 'hold_copy_targeter',
921 sub copy_hold_capture {
926 if (!defined($cps)) {
928 $cps = [ asset::copy->search( id => $hold->target ) ];
931 die "Could not retrieve initial volume list:\n\n$e\n";
935 my @copies = grep { $_->holdable } @$cps;
937 for (my $i = 0; $i < @$cps; $i++) {
938 next unless $$cps[$i];
940 my $cn = $cache{cns}{$copies[$i]->call_number};
941 my $rec = $cache{titles}{$cn->record};
942 $copies[$i] = undef if ($copies[$i] && !grep{ $copies[$i]->status eq $_->id}@$statuses);
943 $copies[$i] = undef if ($copies[$i] && !grep{ $copies[$i]->location eq $_->id}@$locations);
944 $copies[$i] = undef if (
946 !$self->{user_filter}->request(
947 'open-ils.circ.permit_hold',
948 $hold->to_fieldmapper, do {
949 my $cp_fm = $copies[$i]->to_fieldmapper;
950 $cp_fm->circ_lib( $copies[$i]->circ_lib->to_fieldmapper );
951 $cp_fm->location( $copies[$i]->location->to_fieldmapper );
952 $cp_fm->status( $copies[$i]->status->to_fieldmapper );
955 { title => $rec->to_fieldmapper,
956 usr => actor::user->retrieve($hold->usr)->to_fieldmapper,
957 requestor => actor::user->retrieve($hold->requestor)->to_fieldmapper,
960 $self->{client}->status( new OpenSRF::DomainObject::oilsContinueStatus );
963 @copies = grep { $_ } @copies;
967 return unless ($count);
969 action::hold_copy_map->search( { hold => $hold->id } )->delete_all;
972 $self->{client}->respond( "\tMapping ".scalar(@copies)." eligable copies for hold ".$hold->id."\n");
973 for my $c (@copies) {
974 push @maps, action::hold_copy_map->create( { hold => $hold->id, target_copy => $c->id } );
976 $self->{client}->respond( "\tA total of ".scalar(@maps)." mapping were created for hold ".$hold->id."\n");
982 sub choose_nearest_copy {
985 my $prox_list = shift;
987 for my $p ( 0 .. int( scalar(@$prox_list) - 1) ) {
988 next unless (ref $$prox_list[$p]);
989 my @capturable = grep { $_->status == 0 || $_->status == 7 } @{ $$prox_list[$p] };
990 next unless (@capturable);
991 return $capturable[rand(scalar(@capturable))];
995 sub create_prox_list {
1001 for my $cp (@$copies) {
1002 my ($prox) = $self->method_lookup('open-ils.storage.asset.copy.proximity')->run( $cp->id, $lib );
1003 next unless (defined($prox));
1004 $prox_list[$prox] = [] unless defined($prox_list[$prox]);
1005 push @{$prox_list[$prox]}, $cp;
1010 sub volume_hold_capture {
1015 if (!defined($vols)) {
1017 $vols = [ asset::call_number->search( id => $hold->target ) ];
1018 $cache{cns}{$_->id} = $_ for (@$vols);
1019 } catch Error with {
1021 die "Could not retrieve initial volume list:\n\n$e\n";
1025 my @v_ids = map { $_->id } @$vols;
1029 $cp_list = [ asset::copy->search( call_number => \@v_ids ) ];
1031 } catch Error with {
1033 warn "Could not retrieve copy list:\n\n$e\n";
1036 $self->copy_hold_capture($hold,$cp_list) if (ref $cp_list and @$cp_list);
1039 sub title_hold_capture {
1044 if (!defined($titles)) {
1046 $titles = [ biblio::record_entry->search( id => $hold->target ) ];
1047 $cache{titles}{$_->id} = $_ for (@$titles);
1048 } catch Error with {
1050 die "Could not retrieve initial title list:\n\n$e\n";
1054 my @t_ids = map { $_->id } @$titles;
1057 ($cn_list) = $self->method_lookup('open-ils.storage.direct.asset.call_number.search.record.atomic')->run( \@t_ids );
1059 } catch Error with {
1061 warn "Could not retrieve volume list:\n\n$e\n";
1064 $cache{cns}{$_->id} = $_ for (@$cn_list);
1066 $self->volume_hold_capture($hold,$cn_list) if (ref $cn_list and @$cn_list);
1069 sub metarecord_hold_capture {
1075 $titles = [ metabib::metarecord_source_map->search( metarecord => $hold->target) ];
1077 } catch Error with {
1079 die "Could not retrieve initial title list:\n\n$e\n";
1083 my @recs = map {$_->record} metabib::record_descriptor->search( record => $titles, item_type => [split '', $hold->holdable_formats] );
1085 $titles = [ biblio::record_entry->search( id => \@recs ) ];
1087 } catch Error with {
1089 die "Could not retrieve format-pruned title list:\n\n$e\n";
1093 $cache{titles}{$_->id} = $_ for (@$titles);
1094 $self->title_hold_capture($hold,$titles) if (ref $titles and @$titles);