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);
414 my $penalty = OpenSRF::AppSession->create('open-ils.penalty');
418 my $due_dt = $parser->parse_datetime( clense_ISO8601( $c->due_date ) );
420 my $due = $due_dt->epoch;
422 my $fine_interval = interval_to_seconds( $c->fine_interval );
424 if ( interval_to_seconds( $c->fine_interval ) >= interval_to_seconds('1d') ) {
425 my $tz_offset_s = 0;;
426 if ($due_dt->strftime('%z') =~ /(-|\+)(\d{2}):?(\d{2})/) {
427 $tz_offset_s = $1 . interval_to_seconds( "${2}h ${3}m");
430 $due -= ($due % $fine_interval) + $tz_offset_s;
431 $now -= ($now % $fine_interval) + $tz_offset_s;
435 "ARG! Overdue circulation ".$c->id.
436 " for item ".$c->target_copy.
437 " (user ".$c->usr.").\n".
438 "\tItem was due on or before: ".localtime($due)."\n");
440 my ($fine) = money::billing->search(
441 xact => $c->id, voided => 'f',
442 { order_by => 'billing_ts DESC', limit => '1' }
447 $last_fine = $parser->parse_datetime( clense_ISO8601( $fine->billing_ts ) )->epoch;
450 $last_fine += $fine_interval * $grace;
453 my $pending_fine_count = int( ($now - $last_fine) / $fine_interval );
454 unless($pending_fine_count) {
455 $client->respond( "\tNo fines to create. " );
456 if ($grace && $now < $due + $fine_interval * $grace) {
457 $client->respond( "Still inside grace period of: ". seconds_to_interval( $fine_interval * $grace)."\n" );
459 $client->respond( "Last fine generated for: ".localtime($last_fine)."\n" );
464 $client->respond( "\t$pending_fine_count pending fine(s)\n" );
466 for my $bill (1 .. $pending_fine_count) {
468 my ($total) = money::billable_transaction_summary->retrieve( $c->id );
470 if ($total && $total->balance_owed > $c->max_fine) {
471 $c->update({stop_fines => 'MAXFINES'});
473 "\tMaximum fine level of ".$c->max_fine.
474 " reached for this circulation.\n".
475 "\tNo more fines will be generated.\n" );
479 my $billing = money::billing->create(
481 note => "Overdue Fine",
482 billing_type => "Overdue materials",
483 amount => ''.$c->recuring_fine,
484 billing_ts => DateTime->from_epoch( epoch => $last_fine + $fine_interval * $bill )->strftime('%FT%T%z')
489 "\t\tCreating fine of ".$billing->amount." for period starting ".
491 $parser->parse_datetime(
492 clense_ISO8601( $billing->billing_ts )
498 'open-ils.penalty.patron_penalty.calculate',
499 { patron => $c->usr->to_fieldmapper,
507 $client->respond( "Error processing overdue circulation [".$c->id."]:\n\n$e\n" );
511 __PACKAGE__->register_method(
512 api_name => 'open-ils.storage.action.circulation.overdue.generate_fines',
515 method => 'generate_fines',
520 sub new_hold_copy_targeter {
523 my $check_expire = shift;
524 my $one_hold = shift;
532 $check_expire ||= '12h';
533 $check_expire = interval_to_seconds( $check_expire );
535 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time() - $check_expire);
538 my $expire_threshold = sprintf(
539 '%s-%0.2d-%0.2dT%0.2d:%0.2d:%0.2d-00',
540 $year, $mon, $mday, $hour, $min, $sec
543 $holds = [ action::hold_request->search_where(
545 fulfillment_time => undef,
546 prev_check_time => [ undef, { '<=' => $expire_threshold } ] }
548 } elsif ( $check_expire ) {
551 $check_expire ||= '12h';
552 $check_expire = interval_to_seconds( $check_expire );
554 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time() - $check_expire);
557 my $expire_threshold = sprintf(
558 '%s-%0.2d-%0.2dT%0.2d:%0.2d:%0.2d-00',
559 $year, $mon, $mday, $hour, $min, $sec
562 $holds = [ action::hold_request->search_where(
563 { capture_time => undef,
564 fulfillment_time => undef,
565 prev_check_time => { '<=' => $expire_threshold },
567 { order_by => 'selection_depth DESC, request_time,prev_check_time' } ) ];
568 push @$holds, action::hold_request->search(
569 capture_time => undef,
570 fulfillment_time => undef,
571 prev_check_time => undef,
572 { order_by => 'selection_depth DESC, request_time' } );
574 $holds [ action::hold_request->search(
575 capture_time => undef,
576 fulfillment_time => undef,
577 prev_check_time => undef,
578 { order_by => 'selection_depth DESC, request_time' } ) ];
582 die "Could not retrieve uncaptured hold requests:\n\n$e\n";
586 for my $hold (@$holds) {
588 #action::hold_request->db_Main->begin_work;
589 if ($self->method_lookup('open-ils.storage.transaction.current')->run) {
590 $log->debug("Cleaning up after previous transaction\n");
591 $self->method_lookup('open-ils.storage.transaction.rollback')->run;
593 $self->method_lookup('open-ils.storage.transaction.begin')->run( $client );
594 $log->info("Processing hold ".$hold->id."...\n");
596 action::hold_copy_map->search( { hold => $hold->id } )->delete_all;
600 # find all the potential copies
601 if ($hold->hold_type eq 'M') {
604 metabib::record_descriptor
606 record => [ map { $_->id } metabib::metarecord
607 ->retrieve($hold->target)
609 item_type => [split '', $hold->holdable_formats]
613 ->method_lookup( 'open-ils.storage.biblio.record_entry.ranged_tree')
614 ->run( $r->id, $hold->usr->home_ou->id, $hold->selection_depth );
616 for my $cn ( @{ $rtree->call_numbers } ) {
618 asset::copy->search( id => [map {$_->id} @{ $cn->copies }] );
621 } elsif ($hold->hold_type eq 'T') {
623 ->method_lookup( 'open-ils.storage.biblio.record_entry.ranged_tree')
624 ->run( $hold->target, $hold->usr->home_ou->id, $hold->selection_depth );
627 push @successes, { hold => $hold->id, eligible_copies => 0, error => 'NO_RECORD' };
631 for my $cn ( @{ $rtree->call_numbers } ) {
633 asset::copy->search( id => [map {$_->id} @{ $cn->copies }] );
635 } elsif ($hold->hold_type eq 'V') {
637 ->method_lookup( 'open-ils.storage.asset.call_number.ranged_tree')
638 ->run( $hold->target, $hold->usr->home_ou->id, $hold->selection_depth );
641 asset::copy->search( id => [map {$_->id} @{ $vtree->copies }] );
643 } elsif ($hold->hold_type eq 'C') {
645 $all_copies = [asset::copy->retrieve($hold->target)];
648 @$all_copies = grep { $_->status->holdable &&
649 $_->location->holdable &&
653 # let 'em know we're still working
654 $client->status( new OpenSRF::DomainObject::oilsContinueStatus );
656 if (!ref $all_copies || !@$all_copies) {
657 $log->info("\tNo copies available for targeting at all!\n");
658 $self->method_lookup('open-ils.storage.transaction.commit')->run;
659 push @successes, { hold => $hold->id, eligible_copies => 0, error => 'NO_COPIES' };
664 for my $c ( @$all_copies ) {
666 if ( OpenILS::Utils::PermitHold::permit_copy_hold(
667 { title => $c->call_number->record->to_fieldmapper,
668 title_descriptor => $c->call_number->record->record_descriptor->next->to_fieldmapper,
669 patron => $hold->usr->to_fieldmapper,
670 copy => $c->to_fieldmapper,
671 requestor => $hold->requestor->to_fieldmapper,
672 request_lib => $hold->request_lib->to_fieldmapper,
675 my $copy_count = @$copies;
676 $client->status( new OpenSRF::DomainObject::oilsContinueStatus );
678 # map the potentials, so that we can pick up checkins
679 $log->debug( "\tMapping ".scalar(@$copies)." potential copies for hold ".$hold->id);
680 action::hold_copy_map->create( { hold => $hold->id, target_copy => $_->id } ) for (@$copies);
683 for my $c (@$copies) {
684 next if ($c->id == $hold->current_copy);
685 push @good_copies, $c if ($c);
688 $log->debug("\t".scalar(@good_copies)." (non-current) copies available for targeting...");
690 my $old_best = $hold->current_copy;
691 $hold->update({ current_copy => undef });
693 if (!scalar(@good_copies)) {
694 $log->info("\tNo (non-current) copies eligible to fill the hold.");
695 if ( $old_best && grep { $old_best == $_ } @$copies ) {
696 $log->debug("\tPushing current_copy back onto the targeting list");
697 push @good_copies, $old_best;
699 $log->debug("\tcurrent_copy is no longer available for targeting... NEXT HOLD, PLEASE!");
700 $self->method_lookup('open-ils.storage.transaction.commit')->run;
701 push @successes, { hold => $hold->id, eligible_copies => 0, error => 'NO_TARGETS' };
706 $client->status( new OpenSRF::DomainObject::oilsContinueStatus );
711 $_->circ_lib == $hold->pickup_lib
715 $copies = [grep {$_->circ_lib != $hold->pickup_lib } @good_copies];
717 my $best = $self->choose_nearest_copy($hold, $prox_list);
720 $log->debug("\tNothing at the pickup lib, looking elsewhere among ".scalar(@$copies)." copies");
721 $prox_list = $self->create_prox_list( $hold->pickup_lib, $copies );
722 $best = $self->choose_nearest_copy($hold, $prox_list);
725 $client->status( new OpenSRF::DomainObject::oilsContinueStatus );
727 # hold wasn't fulfilled, record the fact
729 $log->info("\tHold was not (but should have been) fulfilled by ".$old_best->id);
730 action::unfulfilled_hold_list->create(
731 { hold => ''.$hold->id,
732 current_copy => ''.$old_best->id,
733 circ_lib => ''.$old_best->circ_lib,
738 $hold->update( { current_copy => ''.$best->id } );
739 $log->debug("\tUpdating hold [".$hold->id."] with new 'current_copy' [".$best->id."] for hold fulfillment.");
741 $log->info( "\tThere were no targetable copies for the hold" );
744 $hold->update( { prev_check_time => 'now' } );
746 $self->method_lookup('open-ils.storage.transaction.commit')->run;
747 $log->info("\tProcessing of hold ".$hold->id." complete.");
751 old_target => ($old_best ? $old_best->id : undef),
752 eligible_copies => $copy_count,
753 target => ($best ? $best->id : undef) };
758 $log->error("Processing of hold failed: $e");
759 $self->method_lookup('open-ils.storage.transaction.rollback')->run;
766 __PACKAGE__->register_method(
767 api_name => 'open-ils.storage.action.hold_request.copy_targeter',
769 method => 'new_hold_copy_targeter',
774 my %cache = (titles => {}, cns => {});
775 sub hold_copy_targeter {
778 my $check_expire = shift;
779 my $one_hold = shift;
781 $self->{user_filter} = OpenSRF::AppSession->create('open-ils.circ');
782 $self->{user_filter}->connect;
783 $self->{client} = $client;
786 $check_expire ||= '12h';
787 $check_expire = interval_to_seconds( $check_expire );
789 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time() - $check_expire);
792 my $expire_threshold = sprintf(
793 '%s-%0.2d-%0.2dT%0.2d:%0.2d:%0.2d-00',
794 $year, $mon, $mday, $hour, $min, $sec
798 $statuses ||= [ config::copy_status->search(holdable => 't') ];
800 $locations ||= [ asset::copy_location->search(holdable => 't') ];
804 %cache = (titles => {}, cns => {});
808 $holds = [ action::hold_request->search(id => $one_hold) ];
810 $holds = [ action::hold_request->search_where(
811 { capture_time => undef,
812 prev_check_time => { '<=' => $expire_threshold },
814 { order_by => 'request_time,prev_check_time' } ) ];
815 push @$holds, action::hold_request->search(
816 capture_time => undef,
817 prev_check_time => undef,
818 { order_by => 'request_time' } );
822 die "Could not retrieve uncaptured hold requests:\n\n$e\n";
825 for my $hold (@$holds) {
827 #action::hold_request->db_Main->begin_work;
828 if ($self->method_lookup('open-ils.storage.transaction.current')->run) {
829 $client->respond("Cleaning up after previous transaction\n");
830 $self->method_lookup('open-ils.storage.transaction.rollback')->run;
832 $self->method_lookup('open-ils.storage.transaction.begin')->run( $client );
833 $client->respond("Processing hold ".$hold->id."...\n");
837 $copies = $self->metarecord_hold_capture($hold) if ($hold->hold_type eq 'M');
838 $self->{client}->status( new OpenSRF::DomainObject::oilsContinueStatus );
840 $copies = $self->title_hold_capture($hold) if ($hold->hold_type eq 'T');
841 $self->{client}->status( new OpenSRF::DomainObject::oilsContinueStatus );
843 $copies = $self->volume_hold_capture($hold) if ($hold->hold_type eq 'V');
844 $self->{client}->status( new OpenSRF::DomainObject::oilsContinueStatus );
846 $copies = $self->copy_hold_capture($hold) if ($hold->hold_type eq 'C');
848 unless (ref $copies || !@$copies) {
849 $client->respond("\tNo copies available for targeting at all!\n");
853 for my $c (@$copies) {
854 next if ( grep {$c->id == $hold->current_copy} @good_copies);
855 push @good_copies, $c if ($c);
858 $client->respond("\t".scalar(@good_copies)." (non-current) copies available for targeting...\n");
860 my $old_best = $hold->current_copy;
861 $hold->update({ current_copy => undef });
863 if (!scalar(@good_copies)) {
864 $client->respond("\tNo (non-current) copies available to fill the hold.\n");
865 if ( $old_best && grep {$c->id == $hold->current_copy} @$copies ) {
866 $client->respond("\tPushing current_copy back onto the targeting list\n");
867 push @good_copies, asset::copy->retrieve( $old_best );
869 $client->respond("\tcurrent_copy is no longer available for targeting... NEXT HOLD, PLEASE!\n");
875 $$prox_list[0] = [grep {$_->circ_lib == $hold->pickup_lib } @good_copies];
876 $copies = [grep {$_->circ_lib != $hold->pickup_lib } @good_copies];
878 my $best = $self->choose_nearest_copy($hold, $prox_list);
881 $prox_list = $self->create_prox_list( $hold->pickup_lib, $copies );
882 $best = $self->choose_nearest_copy($hold, $prox_list);
886 # hold wasn't fulfilled, record the fact
888 $client->respond("\tHold was not (but should have been) fulfilled by ".$old_best->id.".\n");
889 action::unfulfilled_hold_list->create(
890 { hold => ''.$hold->id,
891 current_copy => ''.$old_best->id,
892 circ_lib => ''.$old_best->circ_lib,
897 $hold->update( { current_copy => ''.$best->id } );
898 $client->respond("\tTargeting copy ".$best->id." for hold fulfillment.\n");
901 $hold->update( { prev_check_time => 'now' } );
902 $client->respond("\tUpdating hold ".$hold->id." with new 'current_copy' for hold fulfillment.\n");
904 $client->respond("\tProcessing of hold ".$hold->id." complete.\n");
905 $self->method_lookup('open-ils.storage.transaction.commit')->run;
907 #action::hold_request->dbi_commit;
911 $log->error("Processing of hold failed: $e");
912 $client->respond("\tProcessing of hold failed!.\n\t\t$e\n");
913 $self->method_lookup('open-ils.storage.transaction.rollback')->run;
914 #action::hold_request->dbi_rollback;
918 $self->{user_filter}->disconnect;
919 $self->{user_filter}->finish;
920 delete $$self{user_filter};
923 __PACKAGE__->register_method(
924 api_name => 'open-ils.storage.action.hold_request.copy_targeter',
927 method => 'hold_copy_targeter',
931 sub copy_hold_capture {
936 if (!defined($cps)) {
938 $cps = [ asset::copy->search( id => $hold->target ) ];
941 die "Could not retrieve initial volume list:\n\n$e\n";
945 my @copies = grep { $_->holdable } @$cps;
947 for (my $i = 0; $i < @$cps; $i++) {
948 next unless $$cps[$i];
950 my $cn = $cache{cns}{$copies[$i]->call_number};
951 my $rec = $cache{titles}{$cn->record};
952 $copies[$i] = undef if ($copies[$i] && !grep{ $copies[$i]->status eq $_->id}@$statuses);
953 $copies[$i] = undef if ($copies[$i] && !grep{ $copies[$i]->location eq $_->id}@$locations);
954 $copies[$i] = undef if (
956 !$self->{user_filter}->request(
957 'open-ils.circ.permit_hold',
958 $hold->to_fieldmapper, do {
959 my $cp_fm = $copies[$i]->to_fieldmapper;
960 $cp_fm->circ_lib( $copies[$i]->circ_lib->to_fieldmapper );
961 $cp_fm->location( $copies[$i]->location->to_fieldmapper );
962 $cp_fm->status( $copies[$i]->status->to_fieldmapper );
965 { title => $rec->to_fieldmapper,
966 usr => actor::user->retrieve($hold->usr)->to_fieldmapper,
967 requestor => actor::user->retrieve($hold->requestor)->to_fieldmapper,
970 $self->{client}->status( new OpenSRF::DomainObject::oilsContinueStatus );
973 @copies = grep { $_ } @copies;
977 return unless ($count);
979 action::hold_copy_map->search( { hold => $hold->id } )->delete_all;
982 $self->{client}->respond( "\tMapping ".scalar(@copies)." eligable copies for hold ".$hold->id."\n");
983 for my $c (@copies) {
984 push @maps, action::hold_copy_map->create( { hold => $hold->id, target_copy => $c->id } );
986 $self->{client}->respond( "\tA total of ".scalar(@maps)." mapping were created for hold ".$hold->id."\n");
992 sub choose_nearest_copy {
995 my $prox_list = shift;
997 for my $p ( 0 .. int( scalar(@$prox_list) - 1) ) {
998 next unless (ref $$prox_list[$p]);
999 my @capturable = grep { $_->status == 0 || $_->status == 7 } @{ $$prox_list[$p] };
1000 next unless (@capturable);
1001 return $capturable[rand(scalar(@capturable))];
1005 sub create_prox_list {
1011 for my $cp (@$copies) {
1012 my ($prox) = $self->method_lookup('open-ils.storage.asset.copy.proximity')->run( $cp->id, $lib );
1013 next unless (defined($prox));
1014 $prox_list[$prox] = [] unless defined($prox_list[$prox]);
1015 push @{$prox_list[$prox]}, $cp;
1020 sub volume_hold_capture {
1025 if (!defined($vols)) {
1027 $vols = [ asset::call_number->search( id => $hold->target ) ];
1028 $cache{cns}{$_->id} = $_ for (@$vols);
1029 } catch Error with {
1031 die "Could not retrieve initial volume list:\n\n$e\n";
1035 my @v_ids = map { $_->id } @$vols;
1039 $cp_list = [ asset::copy->search( call_number => \@v_ids ) ];
1041 } catch Error with {
1043 warn "Could not retrieve copy list:\n\n$e\n";
1046 $self->copy_hold_capture($hold,$cp_list) if (ref $cp_list and @$cp_list);
1049 sub title_hold_capture {
1054 if (!defined($titles)) {
1056 $titles = [ biblio::record_entry->search( id => $hold->target ) ];
1057 $cache{titles}{$_->id} = $_ for (@$titles);
1058 } catch Error with {
1060 die "Could not retrieve initial title list:\n\n$e\n";
1064 my @t_ids = map { $_->id } @$titles;
1067 ($cn_list) = $self->method_lookup('open-ils.storage.direct.asset.call_number.search.record.atomic')->run( \@t_ids );
1069 } catch Error with {
1071 warn "Could not retrieve volume list:\n\n$e\n";
1074 $cache{cns}{$_->id} = $_ for (@$cn_list);
1076 $self->volume_hold_capture($hold,$cn_list) if (ref $cn_list and @$cn_list);
1079 sub metarecord_hold_capture {
1085 $titles = [ metabib::metarecord_source_map->search( metarecord => $hold->target) ];
1087 } catch Error with {
1089 die "Could not retrieve initial title list:\n\n$e\n";
1093 my @recs = map {$_->record} metabib::record_descriptor->search( record => $titles, item_type => [split '', $hold->holdable_formats] );
1095 $titles = [ biblio::record_entry->search( id => \@recs ) ];
1097 } catch Error with {
1099 die "Could not retrieve format-pruned title list:\n\n$e\n";
1103 $cache{titles}{$_->id} = $_ for (@$titles);
1104 $self->title_hold_capture($hold,$titles) if (ref $titles and @$titles);