]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Reactor/Circ/AutoRenew.pm
LP2045292 Color contrast for AngularJS patron bills
[Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / Application / Trigger / Reactor / Circ / AutoRenew.pm
1 package OpenILS::Application::Trigger::Reactor::Circ::AutoRenew;
2 use strict; use warnings;
3 use Error qw/:try/;
4 use Data::Dumper;
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';
11
12 use Encode;
13 $Data::Dumper::Indent = 0;
14
15 use base 'OpenILS::Application::Trigger::Reactor';
16
17 my $log = 'OpenSRF::Utils::Logger';
18
19 sub ABOUT {
20     return <<ABOUT;
21 This Autorenew reactor will auto renew a circulation on the day it is due.
22 ABOUT
23 }
24
25 sub handler {
26     my $self = shift;
27     my $env = shift;
28
29     # 1. get a session token for circ user
30
31     my $circs = $env->{target};
32     my $svc = "open-ils.auth_internal";
33     my $api = $svc . '.session.create';
34
35     my $auth_internal_svc = OpenSRF::AppSession->create($svc);
36
37     my $userid = $circs->[0]->usr();
38     # fetch user
39     my $userObj = new_editor()->retrieve_actor_user($userid);
40     my %args = 
41         ( 
42             user_id => $userid,
43             org_unit => $userObj->home_ou(), # all autorenewals occur from patron's Home OU.
44             login_type => "opac"
45         );
46
47     my $token = $auth_internal_svc->request($api, \%args)->gather(1)->{payload}->{authtoken};
48
49     # 2. carry out renewal:
50     for (@$circs){
51
52         $logger->info( "AUTORENEW: circ.target_copy: " . Dumper($_->target_copy()) );
53         my $evt = $AppUtils->simplereq(
54             'open-ils.circ',
55             'open-ils.circ.renew',
56             $token,
57             {
58                 patron_id => $_->usr(),
59                 copy_id => $_->target_copy(),
60                 auto_renewal => 1
61             }
62         );
63
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;
66
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....
73
74         my %user_data = (
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,
83         );
84
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.
88         $AppUtils->simplereq(
89             'open-ils.trigger',
90             'open-ils.trigger.event.autocreate',
91             'autorenewal', $_, $_->circ_lib, undef, \%user_data
92         );
93     }
94
95     return 1;
96 }
97
98 1;