1 package OpenILS::Utils::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::AppUtils;
9 use OpenSRF::Utils::Logger qw(:logger);
10 use OpenILS::Utils::CStoreEditor qw/:funcs/;
11 use OpenILS::Utils::Fieldmapper;
12 use OpenILS::Const qw/:const/;
13 my $U = "OpenILS::Application::AppUtils";
16 # calculate and update the well-known penalties
17 sub calculate_penalties {
18 my($class, $e, $user_id, $context_org) = @_;
22 $e = new_editor(xact =>1);
26 my $penalties = $e->json_query({from => ['actor.calculate_system_penalties',$user_id, $context_org]});
28 for my $pen_obj (@$penalties) {
30 next if grep { # leave duplicate penalties in place
31 $_->{org_unit} == $pen_obj->{org_unit} and
32 $_->{standing_penalty} == $pen_obj->{standing_penalty} and
33 ($_->{id} || '') ne ($pen_obj->{id} || '') } @$penalties;
35 my $pen = Fieldmapper::actor::user_standing_penalty->new;
36 $pen->$_($pen_obj->{$_}) for keys %$pen_obj;
38 if(defined $pen_obj->{id}) {
39 $e->delete_actor_user_standing_penalty($pen) or return $e->die_event;
42 $e->create_actor_user_standing_penalty($pen) or return $e->die_event;
46 $e->rollback if $rollback;
50 # any penalties whose block_list has an item from @fatal_mask will be sorted
51 # into the fatal_penalties set. Others will be sorted into the info_penalties set
52 sub retrieve_penalties {
53 my($class, $e, $user_id, $context_org, @fatal_mask) = @_;
55 my $penalties = $e->search_actor_user_standing_penalty([
56 {usr => $user_id, org_unit => $U->get_org_ancestors($context_org)},
57 {flesh => 1, flesh_fields => {ausp => ['standing_penalty']}}
61 for my $p (@$penalties) {
63 if($p->standing_penalty->block_list) {
64 for my $m (@fatal_mask) {
65 if($p->standing_penalty->block_list =~ /$m/) {
66 push(@fatal, $p->standing_penalty);
72 push(@info, $p->standing_penalty) unless $pushed;
75 return {fatal_penalties => \@fatal, info_penalties => \@info};