]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Reactor/ApplyPatronPenalty.pm
Merge branch 'master' of git.evergreen-ils.org:Evergreen-DocBook into doc_consolidati...
[Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / Application / Trigger / Reactor / ApplyPatronPenalty.pm
1 package OpenILS::Application::Trigger::Reactor::ApplyPatronPenalty;
2 use base 'OpenILS::Application::Trigger::Reactor';
3 use strict; use warnings;
4 use Error qw/:try/;
5 use OpenILS::Const qw/:const/;
6 use OpenILS::Utils::Fieldmapper;
7 use OpenSRF::Utils::Logger qw/$logger/;
8 use OpenILS::Utils::CStoreEditor q/:funcs/;
9 use OpenILS::Application::AppUtils;
10 my $U = "OpenILS::Application::AppUtils";
11
12
13 sub ABOUT {
14     return <<ABOUT;
15     
16     Applies a standing penalty to a patron.  If there is a template, the template is 
17     used as the value for the note
18
19     Required named (with labels) environment variables:
20         "user" -- User object fleshed into the environment
21         "context_org" -- Org unit object fleshed into the environment
22
23     Note: Using named env variables with a grouped event definition where the 
24         env vars may be different depending on the target produces undefined behavior.
25         Don't use this reactor if more than one User or Org Unit object may be 
26         referenced accross the set of target objects.
27
28 ABOUT
29 }
30
31 sub handler {
32     my $self = shift;
33     my $env = shift;
34
35     my $pname = $$env{params}{standing_penalty};
36     my $user = $$env{environment}{user};
37     my $context_org = $$env{environment}{context_org};
38
39     unless($pname and ref $user and ref $context_org) {
40         $logger->error("ApplyPatronPenalty: missing parameters");
41         return 0;
42     }
43
44     my $e = new_editor(xact => 1);
45
46     my $ptype = $e->search_config_standing_penalty({name => $pname})->[0];
47
48     unless($ptype) {
49         $logger->error("ApplyPatronPenalty: invalid penalty name '$pname'");
50         $e->rollback;
51         return 0;
52     }
53
54     $context_org = (defined $ptype->org_depth) ?
55         $U->org_unit_ancestor_at_depth($context_org->id, $ptype->org_depth) :
56         $context_org->id;
57
58     # apply the penalty
59     my $penalty = Fieldmapper::actor::usr_standing_penalty->new;
60     $penalty->usr($user->id);
61     $penalty->org_unit($context_org);
62     $penalty->standing_penalty($ptype->id);
63     $penalty->note($self->run_TT($env));
64
65     unless($e->create_actor_user_standing_penalty($penalty)) {
66         $e->rollback;
67         return 0;
68     }
69
70     $e->commit;
71     return 1;
72 }
73
74 1;