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 local $OpenILS::Application::Storage::WRITE = 1;
86 throw OpenSRF::EX::InvalidArg ("I need an interval of more than 0 seconds!")
87 unless (interval_to_seconds( $window ));
89 my $circ = action::circulation->table;
90 my $cp = asset::copy->table;
95 WHERE id IN ( SELECT cp.id
97 JOIN $circ circ ON (circ.target_copy = cp.id)
98 WHERE circ.checkin_time IS NOT NULL
99 AND circ.checkin_time < NOW() - CAST(? AS INTERVAL)
103 my $sth = action::circulation->db_Main->prepare_cached($sql);
104 $sth->execute($window);
109 __PACKAGE__->register_method(
110 api_name => 'open-ils.storage.action.circulation.reshelving.complete',
114 method => 'complete_reshelving',
120 my $grace = shift || '';
122 $client->respond( $_->to_fieldmapper ) for ( overdue_circs($grace) );
127 __PACKAGE__->register_method(
128 api_name => 'open-ils.storage.action.circulation.overdue',
131 method => 'grab_overdue',
140 my ($id) = action::hold_request->db_Main->selectrow_array(<<" SQL", {}, $pl,$cp);
142 FROM action.hold_request h
143 JOIN action.hold_copy_map hm ON (hm.hold = h.id)
144 WHERE h.pickup_lib = ?
145 AND hm.target_copy = ?
146 AND h.capture_time IS NULL
147 ORDER BY h.pickup_lib - (SELECT home_ou FROM actor.usr a WHERE a.id = h.usr), h.selection_depth DESC, h.request_time
152 __PACKAGE__->register_method(
153 api_name => 'open-ils.storage.action.hold_request.nearest_hold',
155 method => 'nearest_hold',
158 sub next_resp_group_id {
162 # XXX This is not replication safe!!!
164 my ($id) = action::survey->db_Main->selectrow_array(<<" SQL");
165 SELECT NEXTVAL('action.survey_response_group_id_seq'::TEXT)
169 __PACKAGE__->register_method(
170 api_name => 'open-ils.storage.action.survey_response.next_group_id',
172 method => 'next_resp_group_id',
175 sub patron_circ_summary {
180 return undef unless ($id);
181 my $c_table = action::circulation->table;
182 my $b_table = money::billing->table;
184 $log->debug("Retrieving patron summary for id $id", DEBUG);
186 my $select = <<" SQL";
187 SELECT COUNT(DISTINCT c.id), SUM( COALESCE(b.amount,0) )
189 LEFT OUTER JOIN $b_table b ON (c.id = b.xact AND b.voided = FALSE)
191 AND c.xact_finish IS NULL
193 c.stop_fines NOT IN ('CLAIMSRETURNED','LOST')
194 OR c.stop_fines IS NULL
198 return action::survey->db_Main->selectrow_arrayref($select, {}, $id);
200 __PACKAGE__->register_method(
201 api_name => 'open-ils.storage.action.circulation.patron_summary',
203 method => 'patron_circ_summary',
206 #XXX Fix stored proc calls
207 sub find_local_surveys {
212 return undef unless ($ou);
213 my $s_table = action::survey->table;
215 my $select = <<" SQL";
218 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
219 WHERE CURRENT_DATE BETWEEN s.start_date AND s.end_date
222 my $sth = action::survey->db_Main->prepare_cached($select);
225 $client->respond( $_->to_fieldmapper ) for ( map { action::survey->construct($_) } $sth->fetchall_hash );
229 __PACKAGE__->register_method(
230 api_name => 'open-ils.storage.action.survey.all',
233 method => 'find_local_surveys',
236 #XXX Fix stored proc calls
237 sub find_opac_surveys {
242 return undef unless ($ou);
243 my $s_table = action::survey->table;
245 my $select = <<" SQL";
248 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
249 WHERE CURRENT_DATE BETWEEN s.start_date AND s.end_date
253 my $sth = action::survey->db_Main->prepare_cached($select);
256 $client->respond( $_->to_fieldmapper ) for ( map { action::survey->construct($_) } $sth->fetchall_hash );
260 __PACKAGE__->register_method(
261 api_name => 'open-ils.storage.action.survey.opac',
264 method => 'find_opac_surveys',
271 my $limit = shift || 10;
272 my $offset = shift || 0;
274 return undef unless ($ou);
275 my $h_table = action::hold_request->table;
276 my $a_table = asset::copy->table;
278 my $select = <<" SQL";
281 JOIN $a_table a ON (h.current_copy = a.id)
283 AND h.capture_time IS NULL
284 ORDER BY h.request_time ASC
289 my $sth = action::survey->db_Main->prepare_cached($select);
292 $client->respond( $_->to_fieldmapper ) for ( map { action::hold_request->construct($_) } $sth->fetchall_hash );
296 __PACKAGE__->register_method(
297 api_name => 'open-ils.storage.direct.action.hold_request.pull_list.search.current_copy_circ_lib',
301 "Returns the holds for a specific library's pull list.",
302 [ [org_unit => "The library's org id", "number"],
303 [limit => 'An optional page size, defaults to 10', 'number'],
304 [offset => 'Offset for paging, defaults to 0, 0 based', 'number'],
306 ['A list of holds for the stated library to pull for', 'array']
308 method => 'hold_pull_list',
311 sub find_optional_surveys {
316 return undef unless ($ou);
317 my $s_table = action::survey->table;
319 my $select = <<" SQL";
322 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
323 WHERE CURRENT_DATE BETWEEN s.start_date AND s.end_date
324 AND s.required IS FALSE;
327 my $sth = action::survey->db_Main->prepare_cached($select);
330 $client->respond( $_->to_fieldmapper ) for ( map { action::survey->construct($_) } $sth->fetchall_hash );
334 __PACKAGE__->register_method(
335 api_name => 'open-ils.storage.action.survey.optional',
338 method => 'find_optional_surveys',
341 sub find_required_surveys {
346 return undef unless ($ou);
347 my $s_table = action::survey->table;
349 my $select = <<" SQL";
352 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
353 WHERE CURRENT_DATE BETWEEN s.start_date AND s.end_date
354 AND s.required IS TRUE;
357 my $sth = action::survey->db_Main->prepare_cached($select);
360 $client->respond( $_->to_fieldmapper ) for ( map { action::survey->construct($_) } $sth->fetchall_hash );
364 __PACKAGE__->register_method(
365 api_name => 'open-ils.storage.action.survey.required',
368 method => 'find_required_surveys',
371 sub find_usr_summary_surveys {
376 return undef unless ($ou);
377 my $s_table = action::survey->table;
379 my $select = <<" SQL";
382 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
383 WHERE CURRENT_DATE BETWEEN s.start_date AND s.end_date
384 AND s.usr_summary IS TRUE;
387 my $sth = action::survey->db_Main->prepare_cached($select);
390 $client->respond( $_->to_fieldmapper ) for ( map { action::survey->construct($_) } $sth->fetchall_hash );
394 __PACKAGE__->register_method(
395 api_name => 'open-ils.storage.action.survey.usr_summary',
398 method => 'find_usr_summary_surveys',
408 local $OpenILS::Application::Storage::WRITE = 1;
412 push @circs, action::circulation->search_where( { id => $circ, stop_fines => undef } );
414 push @circs, overdue_circs($grace);
417 my $penalty = OpenSRF::AppSession->create('open-ils.penalty');
421 my $due_dt = $parser->parse_datetime( clense_ISO8601( $c->due_date ) );
423 my $due = $due_dt->epoch;
425 my $fine_interval = interval_to_seconds( $c->fine_interval );
427 if ( interval_to_seconds( $c->fine_interval ) >= interval_to_seconds('1d') ) {
429 if ($due_dt->strftime('%z') =~ /(-|\+)(\d{2}):?(\d{2})/) {
430 $tz_offset_s = $1 . interval_to_seconds( "${2}h ${3}m");
433 $due -= ($due % $fine_interval) + $tz_offset_s;
434 $now -= ($now % $fine_interval) + $tz_offset_s;
438 "ARG! Overdue circulation ".$c->id.
439 " for item ".$c->target_copy.
440 " (user ".$c->usr.").\n".
441 "\tItem was due on or before: ".localtime($due)."\n");
443 my ($fine) = money::billing->search(
444 xact => $c->id, voided => 'f',
445 { order_by => 'billing_ts DESC', limit => '1' }
450 $client->respond( "Last billing time: ".$fine->billing_ts." (clensed fromat: ".clense_ISO8601( $fine->billing_ts ).")");
451 $last_fine = $parser->parse_datetime( clense_ISO8601( $fine->billing_ts ) )->epoch;
454 $last_fine += $fine_interval * $grace;
457 my $pending_fine_count = int( ($now - $last_fine) / $fine_interval );
458 unless($pending_fine_count) {
459 $client->respond( "\tNo fines to create. " );
460 if ($grace && $now < $due + $fine_interval * $grace) {
461 $client->respond( "Still inside grace period of: ". seconds_to_interval( $fine_interval * $grace)."\n" );
463 $client->respond( "Last fine generated for: ".localtime($last_fine)."\n" );
468 $client->respond( "\t$pending_fine_count pending fine(s)\n" );
470 for my $bill (1 .. $pending_fine_count) {
472 my ($total) = money::billable_transaction_summary->retrieve( $c->id );
474 if ($total && $total->balance_owed > $c->max_fine) {
475 $c->update({stop_fines => 'MAXFINES'});
477 "\tMaximum fine level of ".$c->max_fine.
478 " reached for this circulation.\n".
479 "\tNo more fines will be generated.\n" );
483 my $billing = money::billing->create(
485 note => "Overdue Fine",
486 billing_type => "Overdue materials",
487 amount => ''.$c->recuring_fine,
488 billing_ts => DateTime->from_epoch( epoch => $last_fine + $fine_interval * $bill )->strftime('%FT%T%z')
493 "\t\tCreating fine of ".$billing->amount." for period starting ".
495 $parser->parse_datetime(
496 clense_ISO8601( $billing->billing_ts )
502 'open-ils.penalty.patron_penalty.calculate',
503 { patron => $c->usr->to_fieldmapper,
511 $client->respond( "Error processing overdue circulation [".$c->id."]:\n\n$e\n" );
515 __PACKAGE__->register_method(
516 api_name => 'open-ils.storage.action.circulation.overdue.generate_fines',
519 method => 'generate_fines',
524 sub new_hold_copy_targeter {
527 my $check_expire = shift;
528 my $one_hold = shift;
530 local $OpenILS::Application::Storage::WRITE = 1;
538 $check_expire ||= '12h';
539 $check_expire = interval_to_seconds( $check_expire );
541 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time() - $check_expire);
544 my $expire_threshold = sprintf(
545 '%s-%0.2d-%0.2dT%0.2d:%0.2d:%0.2d-00',
546 $year, $mon, $mday, $hour, $min, $sec
549 $holds = [ action::hold_request->search_where(
551 fulfillment_time => undef,
552 prev_check_time => [ undef, { '<=' => $expire_threshold } ] }
554 } elsif ( $check_expire ) {
557 $check_expire ||= '12h';
558 $check_expire = interval_to_seconds( $check_expire );
560 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time() - $check_expire);
563 my $expire_threshold = sprintf(
564 '%s-%0.2d-%0.2dT%0.2d:%0.2d:%0.2d-00',
565 $year, $mon, $mday, $hour, $min, $sec
568 $holds = [ action::hold_request->search_where(
569 { capture_time => undef,
570 fulfillment_time => undef,
571 prev_check_time => { '<=' => $expire_threshold },
573 { order_by => 'selection_depth DESC, request_time,prev_check_time' } ) ];
574 push @$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' } );
580 $holds [ action::hold_request->search(
581 capture_time => undef,
582 fulfillment_time => undef,
583 prev_check_time => undef,
584 { order_by => 'selection_depth DESC, request_time' } ) ];
588 die "Could not retrieve uncaptured hold requests:\n\n$e\n";
592 for my $hold (@$holds) {
594 #action::hold_request->db_Main->begin_work;
595 if ($self->method_lookup('open-ils.storage.transaction.current')->run) {
596 $log->debug("Cleaning up after previous transaction\n");
597 $self->method_lookup('open-ils.storage.transaction.rollback')->run;
599 $self->method_lookup('open-ils.storage.transaction.begin')->run( $client );
600 $log->info("Processing hold ".$hold->id."...\n");
602 action::hold_copy_map->search( { hold => $hold->id } )->delete_all;
606 # find all the potential copies
607 if ($hold->hold_type eq 'M') {
610 metabib::record_descriptor
612 record => [ map { $_->id } metabib::metarecord
613 ->retrieve($hold->target)
615 item_type => [split '', $hold->holdable_formats]
619 ->method_lookup( 'open-ils.storage.biblio.record_entry.ranged_tree')
620 ->run( $r->id, $hold->usr->home_ou->id, $hold->selection_depth );
622 for my $cn ( @{ $rtree->call_numbers } ) {
624 asset::copy->search( id => [map {$_->id} @{ $cn->copies }] );
627 } elsif ($hold->hold_type eq 'T') {
629 ->method_lookup( 'open-ils.storage.biblio.record_entry.ranged_tree')
630 ->run( $hold->target, $hold->usr->home_ou->id, $hold->selection_depth );
633 push @successes, { hold => $hold->id, eligible_copies => 0, error => 'NO_RECORD' };
637 for my $cn ( @{ $rtree->call_numbers } ) {
639 asset::copy->search( id => [map {$_->id} @{ $cn->copies }] );
641 } elsif ($hold->hold_type eq 'V') {
643 ->method_lookup( 'open-ils.storage.asset.call_number.ranged_tree')
644 ->run( $hold->target, $hold->usr->home_ou->id, $hold->selection_depth );
647 asset::copy->search( id => [map {$_->id} @{ $vtree->copies }] );
649 } elsif ($hold->hold_type eq 'C') {
651 $all_copies = [asset::copy->retrieve($hold->target)];
654 @$all_copies = grep { $_->status->holdable &&
655 $_->location->holdable &&
659 # let 'em know we're still working
660 $client->status( new OpenSRF::DomainObject::oilsContinueStatus );
662 if (!ref $all_copies || !@$all_copies) {
663 $log->info("\tNo copies available for targeting at all!\n");
664 $self->method_lookup('open-ils.storage.transaction.commit')->run;
665 push @successes, { hold => $hold->id, eligible_copies => 0, error => 'NO_COPIES' };
670 for my $c ( @$all_copies ) {
672 if ( OpenILS::Utils::PermitHold::permit_copy_hold(
673 { title => $c->call_number->record->to_fieldmapper,
674 title_descriptor => $c->call_number->record->record_descriptor->next->to_fieldmapper,
675 patron => $hold->usr->to_fieldmapper,
676 copy => $c->to_fieldmapper,
677 requestor => $hold->requestor->to_fieldmapper,
678 request_lib => $hold->request_lib->to_fieldmapper,
681 my $copy_count = @$copies;
682 $client->status( new OpenSRF::DomainObject::oilsContinueStatus );
684 # map the potentials, so that we can pick up checkins
685 $log->debug( "\tMapping ".scalar(@$copies)." potential copies for hold ".$hold->id);
686 action::hold_copy_map->create( { hold => $hold->id, target_copy => $_->id } ) for (@$copies);
689 for my $c (@$copies) {
690 next if ($c->id == $hold->current_copy);
691 push @good_copies, $c if ($c);
694 $log->debug("\t".scalar(@good_copies)." (non-current) copies available for targeting...");
696 my $old_best = $hold->current_copy;
697 $hold->update({ current_copy => undef });
699 if (!scalar(@good_copies)) {
700 $log->info("\tNo (non-current) copies eligible to fill the hold.");
701 if ( $old_best && grep { $old_best == $_ } @$copies ) {
702 $log->debug("\tPushing current_copy back onto the targeting list");
703 push @good_copies, $old_best;
705 $log->debug("\tcurrent_copy is no longer available for targeting... NEXT HOLD, PLEASE!");
706 $self->method_lookup('open-ils.storage.transaction.commit')->run;
707 push @successes, { hold => $hold->id, eligible_copies => 0, error => 'NO_TARGETS' };
712 $client->status( new OpenSRF::DomainObject::oilsContinueStatus );
717 $_->circ_lib == $hold->pickup_lib
721 $copies = [grep {$_->circ_lib != $hold->pickup_lib } @good_copies];
723 my $best = choose_nearest_copy($hold, $prox_list);
726 $log->debug("\tNothing at the pickup lib, looking elsewhere among ".scalar(@$copies)." copies");
727 $prox_list = $self->create_prox_list( $hold->pickup_lib, $copies );
728 $best = choose_nearest_copy($hold, $prox_list);
731 $client->status( new OpenSRF::DomainObject::oilsContinueStatus );
733 # hold wasn't fulfilled, record the fact
735 $log->info("\tHold was not (but should have been) fulfilled by ".$old_best->id);
736 action::unfulfilled_hold_list->create(
737 { hold => ''.$hold->id,
738 current_copy => ''.$old_best->id,
739 circ_lib => ''.$old_best->circ_lib,
744 $hold->update( { current_copy => ''.$best->id } );
745 $log->debug("\tUpdating hold [".$hold->id."] with new 'current_copy' [".$best->id."] for hold fulfillment.");
747 $log->info( "\tThere were no targetable copies for the hold" );
750 $hold->update( { prev_check_time => 'now' } );
752 $self->method_lookup('open-ils.storage.transaction.commit')->run;
753 $log->info("\tProcessing of hold ".$hold->id." complete.");
757 old_target => ($old_best ? $old_best->id : undef),
758 eligible_copies => $copy_count,
759 target => ($best ? $best->id : undef) };
764 $log->error("Processing of hold failed: $e");
765 $self->method_lookup('open-ils.storage.transaction.rollback')->run;
772 __PACKAGE__->register_method(
773 api_name => 'open-ils.storage.action.hold_request.copy_targeter',
775 method => 'new_hold_copy_targeter',
780 my %cache = (titles => {}, cns => {});
781 sub hold_copy_targeter {
784 my $check_expire = shift;
785 my $one_hold = shift;
787 $self->{user_filter} = OpenSRF::AppSession->create('open-ils.circ');
788 $self->{user_filter}->connect;
789 $self->{client} = $client;
792 $check_expire ||= '12h';
793 $check_expire = interval_to_seconds( $check_expire );
795 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time() - $check_expire);
798 my $expire_threshold = sprintf(
799 '%s-%0.2d-%0.2dT%0.2d:%0.2d:%0.2d-00',
800 $year, $mon, $mday, $hour, $min, $sec
804 $statuses ||= [ config::copy_status->search(holdable => 't') ];
806 $locations ||= [ asset::copy_location->search(holdable => 't') ];
810 %cache = (titles => {}, cns => {});
814 $holds = [ action::hold_request->search(id => $one_hold) ];
816 $holds = [ action::hold_request->search_where(
817 { capture_time => undef,
818 prev_check_time => { '<=' => $expire_threshold },
820 { order_by => 'request_time,prev_check_time' } ) ];
821 push @$holds, action::hold_request->search(
822 capture_time => undef,
823 prev_check_time => undef,
824 { order_by => 'request_time' } );
828 die "Could not retrieve uncaptured hold requests:\n\n$e\n";
831 for my $hold (@$holds) {
833 #action::hold_request->db_Main->begin_work;
834 if ($self->method_lookup('open-ils.storage.transaction.current')->run) {
835 $client->respond("Cleaning up after previous transaction\n");
836 $self->method_lookup('open-ils.storage.transaction.rollback')->run;
838 $self->method_lookup('open-ils.storage.transaction.begin')->run( $client );
839 $client->respond("Processing hold ".$hold->id."...\n");
843 $copies = $self->metarecord_hold_capture($hold) if ($hold->hold_type eq 'M');
844 $self->{client}->status( new OpenSRF::DomainObject::oilsContinueStatus );
846 $copies = $self->title_hold_capture($hold) if ($hold->hold_type eq 'T');
847 $self->{client}->status( new OpenSRF::DomainObject::oilsContinueStatus );
849 $copies = $self->volume_hold_capture($hold) if ($hold->hold_type eq 'V');
850 $self->{client}->status( new OpenSRF::DomainObject::oilsContinueStatus );
852 $copies = $self->copy_hold_capture($hold) if ($hold->hold_type eq 'C');
854 unless (ref $copies || !@$copies) {
855 $client->respond("\tNo copies available for targeting at all!\n");
859 for my $c (@$copies) {
860 next if ( grep {$c->id == $hold->current_copy} @good_copies);
861 push @good_copies, $c if ($c);
864 $client->respond("\t".scalar(@good_copies)." (non-current) copies available for targeting...\n");
866 my $old_best = $hold->current_copy;
867 $hold->update({ current_copy => undef });
869 if (!scalar(@good_copies)) {
870 $client->respond("\tNo (non-current) copies available to fill the hold.\n");
871 if ( $old_best && grep {$c->id == $hold->current_copy} @$copies ) {
872 $client->respond("\tPushing current_copy back onto the targeting list\n");
873 push @good_copies, asset::copy->retrieve( $old_best );
875 $client->respond("\tcurrent_copy is no longer available for targeting... NEXT HOLD, PLEASE!\n");
881 $$prox_list[0] = [grep {$_->circ_lib == $hold->pickup_lib } @good_copies];
882 $copies = [grep {$_->circ_lib != $hold->pickup_lib } @good_copies];
884 my $best = choose_nearest_copy($hold, $prox_list);
887 $prox_list = $self->create_prox_list( $hold->pickup_lib, $copies );
888 $best = choose_nearest_copy($hold, $prox_list);
892 # hold wasn't fulfilled, record the fact
894 $client->respond("\tHold was not (but should have been) fulfilled by ".$old_best->id.".\n");
895 action::unfulfilled_hold_list->create(
896 { hold => ''.$hold->id,
897 current_copy => ''.$old_best->id,
898 circ_lib => ''.$old_best->circ_lib,
903 $hold->update( { current_copy => ''.$best->id } );
904 $client->respond("\tTargeting copy ".$best->id." for hold fulfillment.\n");
907 $hold->update( { prev_check_time => 'now' } );
908 $client->respond("\tUpdating hold ".$hold->id." with new 'current_copy' for hold fulfillment.\n");
910 $client->respond("\tProcessing of hold ".$hold->id." complete.\n");
911 $self->method_lookup('open-ils.storage.transaction.commit')->run;
913 #action::hold_request->dbi_commit;
917 $log->error("Processing of hold failed: $e");
918 $client->respond("\tProcessing of hold failed!.\n\t\t$e\n");
919 $self->method_lookup('open-ils.storage.transaction.rollback')->run;
920 #action::hold_request->dbi_rollback;
924 $self->{user_filter}->disconnect;
925 $self->{user_filter}->finish;
926 delete $$self{user_filter};
929 __PACKAGE__->register_method(
930 api_name => 'open-ils.storage.action.hold_request.copy_targeter',
933 method => 'hold_copy_targeter',
937 sub copy_hold_capture {
942 if (!defined($cps)) {
944 $cps = [ asset::copy->search( id => $hold->target ) ];
947 die "Could not retrieve initial volume list:\n\n$e\n";
951 my @copies = grep { $_->holdable } @$cps;
953 for (my $i = 0; $i < @$cps; $i++) {
954 next unless $$cps[$i];
956 my $cn = $cache{cns}{$copies[$i]->call_number};
957 my $rec = $cache{titles}{$cn->record};
958 $copies[$i] = undef if ($copies[$i] && !grep{ $copies[$i]->status eq $_->id}@$statuses);
959 $copies[$i] = undef if ($copies[$i] && !grep{ $copies[$i]->location eq $_->id}@$locations);
960 $copies[$i] = undef if (
962 !$self->{user_filter}->request(
963 'open-ils.circ.permit_hold',
964 $hold->to_fieldmapper, do {
965 my $cp_fm = $copies[$i]->to_fieldmapper;
966 $cp_fm->circ_lib( $copies[$i]->circ_lib->to_fieldmapper );
967 $cp_fm->location( $copies[$i]->location->to_fieldmapper );
968 $cp_fm->status( $copies[$i]->status->to_fieldmapper );
971 { title => $rec->to_fieldmapper,
972 usr => actor::user->retrieve($hold->usr)->to_fieldmapper,
973 requestor => actor::user->retrieve($hold->requestor)->to_fieldmapper,
976 $self->{client}->status( new OpenSRF::DomainObject::oilsContinueStatus );
979 @copies = grep { $_ } @copies;
983 return unless ($count);
985 action::hold_copy_map->search( { hold => $hold->id } )->delete_all;
988 $self->{client}->respond( "\tMapping ".scalar(@copies)." eligable copies for hold ".$hold->id."\n");
989 for my $c (@copies) {
990 push @maps, action::hold_copy_map->create( { hold => $hold->id, target_copy => $c->id } );
992 $self->{client}->respond( "\tA total of ".scalar(@maps)." mapping were created for hold ".$hold->id."\n");
998 sub choose_nearest_copy {
1000 my $prox_list = shift;
1002 for my $p ( 0 .. int( scalar(@$prox_list) - 1) ) {
1003 next unless (ref $$prox_list[$p]);
1004 my @capturable = grep { $_->status == 0 || $_->status == 7 } @{ $$prox_list[$p] };
1005 next unless (@capturable);
1006 return $capturable[rand(scalar(@capturable))];
1010 sub create_prox_list {
1016 for my $cp (@$copies) {
1017 my ($prox) = $self->method_lookup('open-ils.storage.asset.copy.proximity')->run( $cp->id, $lib );
1018 next unless (defined($prox));
1019 $prox_list[$prox] = [] unless defined($prox_list[$prox]);
1020 push @{$prox_list[$prox]}, $cp;
1025 sub volume_hold_capture {
1030 if (!defined($vols)) {
1032 $vols = [ asset::call_number->search( id => $hold->target ) ];
1033 $cache{cns}{$_->id} = $_ for (@$vols);
1034 } catch Error with {
1036 die "Could not retrieve initial volume list:\n\n$e\n";
1040 my @v_ids = map { $_->id } @$vols;
1044 $cp_list = [ asset::copy->search( call_number => \@v_ids ) ];
1046 } catch Error with {
1048 warn "Could not retrieve copy list:\n\n$e\n";
1051 $self->copy_hold_capture($hold,$cp_list) if (ref $cp_list and @$cp_list);
1054 sub title_hold_capture {
1059 if (!defined($titles)) {
1061 $titles = [ biblio::record_entry->search( id => $hold->target ) ];
1062 $cache{titles}{$_->id} = $_ for (@$titles);
1063 } catch Error with {
1065 die "Could not retrieve initial title list:\n\n$e\n";
1069 my @t_ids = map { $_->id } @$titles;
1072 ($cn_list) = $self->method_lookup('open-ils.storage.direct.asset.call_number.search.record.atomic')->run( \@t_ids );
1074 } catch Error with {
1076 warn "Could not retrieve volume list:\n\n$e\n";
1079 $cache{cns}{$_->id} = $_ for (@$cn_list);
1081 $self->volume_hold_capture($hold,$cn_list) if (ref $cn_list and @$cn_list);
1084 sub metarecord_hold_capture {
1090 $titles = [ metabib::metarecord_source_map->search( metarecord => $hold->target) ];
1092 } catch Error with {
1094 die "Could not retrieve initial title list:\n\n$e\n";
1098 my @recs = map {$_->record} metabib::record_descriptor->search( record => $titles, item_type => [split '', $hold->holdable_formats] );
1100 $titles = [ biblio::record_entry->search( id => \@recs ) ];
1102 } catch Error with {
1104 die "Could not retrieve format-pruned title list:\n\n$e\n";
1108 $cache{titles}{$_->id} = $_ for (@$titles);
1109 $self->title_hold_capture($hold,$titles) if (ref $titles and @$titles);