1 package OpenILS::Application::Trigger::Reactor::Circ::AutoRenew;
2 use strict; use warnings;
5 use OpenSRF::Utils::SettingsClient;
6 use OpenILS::Application::Trigger::Reactor;
7 use OpenSRF::Utils::Logger qw/:logger/;
8 use OpenILS::Utils::CStoreEditor qw/:funcs/;
9 use OpenILS::Application::AppUtils;
10 my $AppUtils = 'OpenILS::Application::AppUtils';
13 $Data::Dumper::Indent = 0;
15 use base 'OpenILS::Application::Trigger::Reactor';
17 my $log = 'OpenSRF::Utils::Logger';
21 This Autorenew reactor will auto renew a circulation on the day it is due.
29 # 1. get a session token for circ user
31 my $circs = $env->{target};
32 my $svc = "open-ils.auth_internal";
33 my $api = $svc . '.session.create';
35 my $auth_internal_svc = OpenSRF::AppSession->create($svc);
37 my $userid = $circs->[0]->usr();
39 my $userObj = new_editor()->retrieve_actor_user($userid);
43 org_unit => $userObj->home_ou(), # all autorenewals occur from patron's Home OU.
47 my $token = $auth_internal_svc->request($api, \%args)->gather(1)->{payload}->{authtoken};
49 # 2. carry out renewal:
52 $logger->info( "AUTORENEW: circ.target_copy: " . Dumper($_->target_copy()) );
53 my $evt = $AppUtils->simplereq(
55 'open-ils.circ.renew',
58 patron_id => $_->usr(),
59 copy_id => $_->target_copy(),
64 $evt = $evt->[0] if ref($evt) eq "ARRAY"; # we got two resp events, likely renewal errors, grab the first.
65 my $is_renewed = $evt->{textcode} eq 'SUCCESS' ? 1 : 0;
67 my $new_circ_due = $is_renewed ? $evt->{payload}->{circ}->due_date : '';
68 my $total_remaining = $is_renewed ? $evt->{payload}->{circ}->renewal_remaining : $_->renewal_remaining;
69 my $auto_remaining = $is_renewed ? $evt->{payload}->{circ}->auto_renewal_remaining : $_->auto_renewal_remaining;
70 # Check for negative renewal remaining. It can happen with an override renewal:
71 $total_remaining = ($total_remaining < 0) ? 0 : $total_remaining;
72 $auto_remaining = ($auto_remaining < 0) ? 0 : $auto_remaining; # Just making sure....
75 copy => $_->target_copy(),
76 is_renewed => $is_renewed,
77 reason => !$is_renewed ? $evt->{desc} : '',
78 new_due_date => $is_renewed ? $evt->{payload}->{circ}->due_date : '',
79 old_due_date => !$is_renewed ? $_->due_date() : '',
80 textcode => $evt->{textcode},
81 total_renewal_remaining => $total_remaining,
82 auto_renewal_remaining => ($auto_remaining < $total_remaining) ? $auto_remaining : $total_remaining,
85 # Create the event from the source circ instead of the
86 # new circ, since the renewal may have failed.
87 # Fire and do not forget so we don't flood A/T.
90 'open-ils.trigger.event.autocreate',
91 'autorenewal', $_, $_->circ_lib, undef, \%user_data