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::Application::Circ::ScriptBuilder;
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");
45 __PACKAGE__->register_method (
46 method => 'patron_penalty',
47 api_name => 'open-ils.penalty.patron_penalty.calculate',
49 Calculates the patron's standing penalties
50 @param args An object of named params including:
51 patronid The id of the patron
52 update True if this call should update the database
53 background True if this call should return immediately,
54 then go on to process the penalties. This flag
55 works only in conjunction with the 'update' flag.
56 @return An object with keys 'fatal_penalties' and
57 'info_penalties' who are themeselves arrays of 0 or
58 more penalties. Returns event on error.
62 # --------------------------------------------------------------
65 # - background : modifier to 'update' which says to return
66 # immediately then continue processing. If this flag is set
67 # then the caller will get no penalty info and will never
68 # know for sure if the call even succeeded.
69 # --------------------------------------------------------------
71 my( $self, $conn, $args ) = @_;
75 $conn->respond_complete(1) if $$args{background};
77 return { fatal_penalties => [], info_penalties => [] }
78 unless ($args->{patron} || $args->{patronid});
80 $args->{patron_id} = $args->{patronid};
81 $args->{fetch_patron_circ_info} = 1;
82 my $runner = OpenILS::Application::Circ::ScriptBuilder->build($args);
84 # - Load up the script and run it
85 $runner->add_path($path);
87 $runner->run($script) or
88 throw OpenSRF::EX::ERROR ("Patron Penalty Script Died: $@");
90 # array items are returned as a comma-separated list of strings
91 my @fatals = split( /,/, $runner->retrieve($fatal_key) );
92 my @infos = split( /,/, $runner->retrieve($info_key) );
93 my $all = [ @fatals, @infos ];
95 $logger->info("penalty: script returned fatal events [@fatals] and info events [@infos]");
97 $conn->respond_complete(
98 { fatal_penalties => \@fatals, info_penalties => \@infos });
100 # - update the penalty info in the db if necessary
101 $logger->debug("update penalty settings = " . $$args{update});
103 $evt = update_patron_penalties(
104 patron => $args->{patron},
105 penalties => $all ) if $$args{update};
107 # - The caller won't know it failed, so log it
108 $logger->error("penalty: Error updating the patron ".
109 "penalties in the database: ".Dumper($evt)) if $evt;
114 # --------------------------------------------------------------
115 # Removes existing penalties for the patron that are not passed
116 # into this function. Creates new penalty entries for the
117 # provided penalties that don't already exist;
118 # --------------------------------------------------------------
119 sub update_patron_penalties {
122 my $patron = $args{patron};
123 my $penalties = $args{penalties};
124 my $pid = $patron->id;
126 $logger->debug("updating penalties for patron $pid => @$penalties");
128 my $session = $U->start_db_session();
130 # - fetch the current penalties
131 my $existing = $session->request(
132 'open-ils.storage.direct.actor.'.
133 'user_standing_penalty.search.usr.atomic', $pid )->gather(1);
137 # If an existing penalty is not in the newly generated
138 # list of penalties, remove it from the DB
139 for my $e (@$existing) {
140 if( ! grep { $_ eq $e->penalty_type } @$penalties ) {
142 $logger->activity("penalty: removing user penalty ".
143 $e->penalty_type . " from user $pid");
145 my $s = $session->request(
146 'open-ils.storage.direct.actor.user_standing_penalty.delete', $e->id )->gather(1);
147 return $U->DB_UPDATE_FAILED($e) unless defined($s);
151 # Add penalties that previously didn't exist
152 for my $p (@$penalties) {
153 if( ! grep { $_->penalty_type eq $p } @$existing ) {
155 $logger->activity("penalty: adding user penalty $p to user $pid");
157 my $newp = Fieldmapper::actor::user_standing_penalty->new;
158 $newp->penalty_type( $p );
161 my $s = $session->request(
162 'open-ils.storage.direct.actor.user_standing_penalty.create', $newp )->gather(1);
163 return $U->DB_UPDATE_FAILED($p) unless $s;
167 $U->commit_db_session($session);