]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Utils/Penalty.pm
thinko on rollback vs. commit when creating a local cstoreEditor object for user...
[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 $commit = 0;
21     unless($e) {
22         $e = new_editor(xact =>1);
23         $commit = 1;
24     }
25
26     my $penalties = $e->json_query({from => ['actor.calculate_system_penalties',$user_id, $context_org]});
27
28     my $user = $e->retrieve_actor_user( $user_id );
29     my $ses = OpenSRF::AppSession->create('open-ils.trigger') if (@$penalties);
30
31     my %csp;
32     for my $pen_obj (@$penalties) {
33
34         next if grep { # leave duplicate penalties in place
35             $_->{org_unit} == $pen_obj->{org_unit} and
36             $_->{standing_penalty} == $pen_obj->{standing_penalty} and
37             ($_->{id} || '') ne ($pen_obj->{id} || '') } @$penalties;
38
39         my $pen = Fieldmapper::actor::user_standing_penalty->new;
40         $pen->$_($pen_obj->{$_}) for keys %$pen_obj;
41
42         if(defined $pen_obj->{id}) {
43             $e->delete_actor_user_standing_penalty($pen) or return $e->die_event;
44
45         } else {
46             $e->create_actor_user_standing_penalty($pen) or return $e->die_event;
47
48             my $csp_obj = $csp{$pen->standing_penalty} ||
49                 $e->retrieve_config_standing_penalty( $pen->standing_penalty );
50
51             # cache for later
52             $csp{$pen->standing_penalty} = $csp_obj;
53
54             $ses->request(
55                 'open-ils.trigger.event.autocreate',
56                 'penalty.' . $csp_obj->name,
57                 $user,
58                 $pen->org_unit
59             );
60         }
61     }
62
63     $e->commit if $commit;
64     return undef;
65 }
66
67 # any penalties whose block_list has an item from @fatal_mask will be sorted
68 # into the fatal_penalties set.  Others will be sorted into the info_penalties set
69 sub retrieve_penalties {
70     my($class, $e, $user_id, $context_org, @fatal_mask) = @_;
71
72     my(@info, @fatal);
73     my $penalties = $class->retrieve_usr_penalties($e, $user_id, $context_org);
74
75     for my $p (@$penalties) {
76         my $pushed = 0;
77         if($p->standing_penalty->block_list) {
78             for my $m (@fatal_mask) {
79                 if($p->standing_penalty->block_list =~ /$m/) {
80                     push(@fatal, $p->standing_penalty);
81                     $pushed = 1;
82                     last;
83                 }
84             }
85         }
86         push(@info, $p->standing_penalty) unless $pushed;
87     }
88
89     return {fatal_penalties => \@fatal, info_penalties => \@info};
90 }
91
92
93 # Returns a list of actor_user_standing_penalty objects
94 sub retrieve_usr_penalties {
95     my($class, $e, $user_id, $context_org) = @_;
96
97     return $e->search_actor_user_standing_penalty([
98         {
99             usr => $user_id, 
100             org_unit => $U->get_org_ancestors($context_org),
101             '-or' => [
102                 {stop_date => undef},
103                 {stop_date => {'>' => 'now'}}
104             ],
105         },
106         {flesh => 1, flesh_fields => {ausp => ['standing_penalty']}}
107     ]);
108 }
109
110 1;
111
112