]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Utils/Penalty.pm
pull name from the SP object, not the user_standing_penalty
[working/Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Utils / Penalty.pm
1 package OpenILS::Utils::Penalty;
2 use strict; use warnings;
3 use DateTime;
4 use Data::Dumper;
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";
14
15
16 # calculate and update the well-known penalties
17 sub calculate_penalties {
18     my($class, $e, $user_id, $context_org) = @_;
19
20     my $penalties = $e->json_query({from => ['actor.calculate_system_penalties',$user_id, $context_org]});
21
22     for my $pen_obj (@$penalties) {
23
24         next if grep { # leave duplicate penalties in place
25             $_->{org_unit} == $pen_obj->{org_unit} and
26             $_->{standing_penalty} == $pen_obj->{standing_penalty} and
27             ($_->{id} || '') ne ($pen_obj->{id} || '') } @$penalties;
28
29         my $pen = Fieldmapper::actor::user_standing_penalty->new;
30         $pen->$_($pen_obj->{$_}) for keys %$pen_obj;
31
32         if(defined $pen_obj->{id}) {
33             $e->delete_actor_user_standing_penalty($pen) or return $e->die_event;
34
35         } else {
36             $e->create_actor_user_standing_penalty($pen) or return $e->die_event;
37         }
38     }
39
40     return undef;
41 }
42
43 # any penalties whose block_list has an item from @fatal_mask will be sorted
44 # into the fatal_penalties set.  Others will be sorted into the info_penalties set
45 sub retrieve_penalties {
46     my($class, $e, $user_id, $context_org, @fatal_mask) = @_;
47
48     my $penalties = $e->search_actor_user_standing_penalty([
49         {usr => $user_id, org_unit => $U->get_org_ancestors($context_org)},
50         {flesh => 1, flesh_fields => {ausp => ['standing_penalty']}}
51     ]);
52
53     my(@info, @fatal);
54     for my $p (@$penalties) {
55         my $pushed = 0;
56         if($p->standing_penalty->block_list) {
57             for my $m (@fatal_mask) {
58                 if($p->standing_penalty->block_list =~ /$m/) {
59                     push(@fatal, $p->standing_penalty->name);
60                     $pushed = 1;
61                     last;
62                 }
63             }
64         }
65         push(@info, $p->name) unless $pushed;
66     }
67
68     return {fatal_penalties => \@fatal, info_penalties => \@info};
69 }
70
71 1;