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 OpenILS::Utils::CStoreEditor qw/:funcs/;
13 use base 'OpenSRF::Application';
15 my $U = "OpenILS::Application::AppUtils";
20 my %groups; # - user groups
22 my $fatal_key = 'result.fatalEvents';
23 my $info_key = 'result.infoEvents';
26 # --------------------------------------------------------------
27 # Loads the config info
28 # --------------------------------------------------------------
31 my $conf = OpenSRF::Utils::SettingsClient->new;
32 my @pfx = ( "apps", "open-ils.penalty","app_settings" );
33 $path = $conf->config_value( @pfx, 'script_path');
34 $script = $conf->config_value( @pfx, 'patron_penalty' );
36 if(!($path and $script)) {
37 $logger->error("penalty: server config missing script and/or script path");
41 $logger->info("penalty: Loading patron penalty script $script with path $path");
46 __PACKAGE__->register_method (
47 method => 'patron_penalty',
48 api_name => 'open-ils.penalty.patron_penalty.calculate',
50 Calculates the patron's standing penalties
51 @param args An object of named params including:
52 patronid The id of the patron
53 update True if this call should update the database
54 background True if this call should return immediately,
55 then go on to process the penalties. This flag
56 works only in conjunction with the 'update' flag.
57 @return An object with keys 'fatal_penalties' and
58 'info_penalties' who are themeselves arrays of 0 or
59 more penalties. Returns event on error.
63 # --------------------------------------------------------------
66 # - background : modifier to 'update' which says to return
67 # immediately then continue processing. If this flag is set
68 # then the caller will get no penalty info and will never
69 # know for sure if the call even succeeded.
70 # --------------------------------------------------------------
72 my( $self, $conn, $args ) = @_;
76 $conn->respond_complete(1) if $$args{background};
78 return { fatal_penalties => [], info_penalties => [] }
79 unless ($args->{patron} || $args->{patronid});
81 $args->{patron_id} = $args->{patronid};
82 $args->{fetch_patron_circ_info} = 1;
83 my $runner = OpenILS::Application::Circ::ScriptBuilder->build($args);
85 # - Load up the script and run it
86 $runner->add_path($path);
88 $runner->load($script);
89 my $result = $runner->run or throw OpenSRF::EX::ERROR ("Patron Penalty Script Died: $@");
91 my @fatals = @{$result->{fatalEvents}};
92 my @infos = @{$result->{infoEvents}};
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;
127 $logger->debug("updating penalties for patron $pid => @$penalties");
128 my $editor = new_editor(xact =>1);
131 # - fetch the current penalties
132 my $existing = $editor->search_actor_user_standing_penalty({usr=>$pid});
135 push( @types, $_->penalty_type ) for @$existing;
136 $logger->info("penalty: user has existing penalties [@types]");
140 # If an existing penalty is not in the newly generated
141 # list of penalties, remove it from the DB
142 for my $e (@$existing) {
143 if( ! grep { $_ eq $e->penalty_type } @$penalties ) {
145 $logger->activity("penalty: removing user penalty ".
146 $e->penalty_type . " from user $pid");
148 $editor->delete_actor_user_standing_penalty($e)
149 or return $editor->event;
153 # Add penalties that previously didn't exist
154 for my $p (@$penalties) {
155 if( ! grep { $_->penalty_type eq $p } @$existing ) {
157 $logger->activity("penalty: adding user penalty $p to user $pid");
159 my $newp = Fieldmapper::actor::user_standing_penalty->new;
160 $newp->penalty_type( $p );
163 $editor->create_actor_user_standing_penalty($newp)
164 or return $editor->event;