1 package OpenILS::Application::Penalty;
2 use strict; use warnings;
5 use OpenSRF::EX qw(:try);
6 use OpenSRF::Utils::Cache;
7 use OpenSRF::Utils qw/:datetime/;
8 use OpenILS::Utils::ScriptRunner;
9 use OpenSRF::Utils::SettingsClient;
10 use OpenILS::Application::AppUtils;
11 use OpenSRF::Utils::Logger qw(:logger);
12 use base 'OpenSRF::Application';
14 my $U = "OpenILS::Application::AppUtils";
19 my %groups; # - user groups
21 my $fatal_key = 'result.fatalEvents';
22 my $info_key = 'result.infoEvents';
25 # --------------------------------------------------------------
26 # Loads the config info
27 # --------------------------------------------------------------
30 my $conf = OpenSRF::Utils::SettingsClient->new;
31 my @pfx = ( "apps", "open-ils.penalty","app_settings" );
32 $path = $conf->config_value( @pfx, 'script_path');
33 $script = $conf->config_value( @pfx, 'patron_penalty' );
35 if(!($path and $script)) {
36 $logger->error("Penalty server config missing script and/or script path");
40 $logger->info("penalty: Loading patron penalty script $script with path $path");
44 # --------------------------------------------------------------
45 # Builds the script runner and shoves data into the script
47 # --------------------------------------------------------------
51 my $patron = $args{patron};
52 my $patron_summary = $args{patron_summary};
54 my $pgroup = find_profile($patron);
55 $patron->profile( $pgroup );
58 $runner->refresh_context if $runner;
61 $runner = OpenILS::Utils::ScriptRunner->new unless $runner;
62 $runner->add_path( $_ );
65 $runner->insert( 'environment.patron', $patron, 1);
66 $runner->insert( $fatal_key, [] );
67 $runner->insert( $info_key, [] );
68 $runner->insert( 'environment.patronItemsOut', $patron_summary->[0] );
69 $runner->insert( 'environment.patronFines', $patron_summary->[1] );
79 my $groups = $U->storagereq(
80 'open-ils.storage.direct.permission.grp_tree.retrieve.all.atomic');
81 %groups = map { $_->id => $_ } @$groups;
84 return $groups{$patron->profile};
89 __PACKAGE__->register_method (
90 method => 'patron_penalty',
91 api_name => 'open-ils.penalty.patron_penalty.calculate',
93 Calculates the patron's standing penalties
94 @param authtoken The login session key
95 @params args An object of named params including:
96 patronid The id of the patron
97 update True if this call should update the database
98 background True if this call should return immediately,
99 then go on to process the penalties. This flag
100 works only in conjunction with the 'update' flag.
101 @return An object with keys 'fatal_penalties' and
102 'info_penalties' who are themeselves arrays of 0 or
103 more penalties. Returns event on error.
107 # --------------------------------------------------------------
110 # - background : modifier to 'update' which says to return
111 # immediately then continue processing. If this flag is set
112 # then the caller will get no penalty info and will never
113 # know for sure if the call even succeeded.
114 # --------------------------------------------------------------
116 my( $self, $conn, $authtoken, $args ) = @_;
118 my( $requestor, $patron, $evt );
120 $conn->respond_complete(1) if $$args{background};
122 $patron = $$args{patron};
125 ( $patron, $evt ) = $U->fetch_user($$args{patronid});
129 ( $requestor, $evt ) = $U->checkses($authtoken);
132 $evt = $U->check_perms( $requestor->id, $patron->home_ou, 'VIEW_USER');
135 # - fetch the circulation summary info for the user
136 my $summary = $U->fetch_patron_circ_summary($patron->id);
138 # - build the script runner
139 my $runner = build_runner(
141 patron_summary => $summary
144 # - Load up the script and run it
145 $runner->add_path($path);
146 $runner->load($script);
147 $runner->run or throw OpenSRF::EX::ERROR ("Patron Penalty Script Died: $@");
149 # array items are returned as a comma-separated list of strings
150 my @fatals = split( /,/, $runner->retrieve($fatal_key) );
151 my @infos = split( /,/, $runner->retrieve($info_key) );
152 my $all = [ @fatals, @infos ];
154 $logger->info("penalty: script returned fatal events [@fatals] and info events [@infos]");
156 $conn->respond_complete(
157 { fatal_penalties => \@fatals, info_penalties => \@infos });
159 # - update the penalty info in the db if necessary
160 $evt = update_patron_penalties(
163 requestor => $requestor ) if $$args{update};
165 # - The caller won't know it failed, so log it
166 $logger->error("penalty: Error updating the patron ".
167 "penalties in the database: ".Dumper($evt)) if $evt;
172 # --------------------------------------------------------------
173 # Removes existing penalties for the patron that are not passed
174 # into this function. Creates new penalty entries for the
175 # provided penalties that don't already exist;
176 # --------------------------------------------------------------
177 sub update_patron_penalties {
180 my $patron = $args{patron};
181 my $penalties = $args{penalties};
182 my $requestor = $args{requestor};
184 my $session = $U->start_db_session();
186 # - fetch the current penalties
187 my $existing = $session->request(
188 'open-ils.storage.direct.actor.'.
189 'user_standing_penalty.search.usr.atomic', $patron->id )->gather(1);
192 my $reqid = $requestor->id;
193 my $patronid = $patron->id;
195 # If an existing penalty is not in the newly generated
196 # list of penalties, remove it from the DB
197 for my $e (@$existing) {
198 if( ! grep { $_ eq $e->penalty_type } @$penalties ) {
200 $logger->activity("user $reqid removing user penalty ".
201 $e->penalty_type . " from user $patronid");
203 my $s = $session->request(
204 'open-ils.storage.direct.actor.user_standing_penalty.delete', $e->id )->gather(1);
205 return $U->DB_UPDATE_FAILED($e) unless defined($s);
209 # Add penalties that previously didn't exist
210 for my $p (@$penalties) {
211 if( ! grep { $_->penalty_type eq $p } @$existing ) {
213 $logger->activity("user $reqid adding user penalty $p to user $patronid");
215 my $newp = Fieldmapper::actor::user_standing_penalty->new;
216 $newp->penalty_type( $p );
217 $newp->usr( $patronid );
219 my $s = $session->request(
220 'open-ils.storage.direct.actor.user_standing_penalty.create', $newp )->gather(1);
221 return $U->DB_UPDATE_FAILED($p) unless $s;
225 $U->commit_db_session($session);