e24e0541a09c93118b3a5b98486876723cd817d1
[freeside.git] / rt / lib / RT / Action / EscalatePriority.pm
1 # BEGIN LICENSE BLOCK
2
3 # Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
4
5 # (Except where explictly superceded by other copyright notices)
6
7 # This work is made available to you under the terms of Version 2 of
8 # the GNU General Public License. A copy of that license should have
9 # been provided with this software, but in any event can be snarfed
10 # from www.gnu.org.
11
12 # This work is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # General Public License for more details.
16
17 # Unless otherwise specified, all modifications, corrections or
18 # extensions to this work which alter its source code become the
19 # property of Best Practical Solutions, LLC when submitted for
20 # inclusion in the work.
21
22
23 # END LICENSE BLOCK
24 =head1 NAME
25
26   RT::Action::EscalatePriority
27
28 =head1 DESCRIPTION
29
30 EscalatePriority is a ScripAction which is NOT intended to be called
31 per transaction. It's intended to be called by an RT escalation tool.
32 One such tool is called rt-crontool and is located in $RTHOME/bin (see
33 C<rt-crontool -h> for more details)
34
35 EsclatePriority uses the following formula to change a ticket's priority:
36
37     Priority = Priority +  (( FinalPriority - Priority ) / ( DueDate-Today))
38
39 Unless the duedate is past, in which case priority gets bumped straight
40 to final priority.
41
42 In this way, priority is either increased or decreased toward the final priority
43 as the ticket heads toward its due date.
44
45
46 =cut
47
48
49 package RT::Action::EscalatePriority;
50 require RT::Action::Generic;
51
52 use strict;
53 use vars qw/@ISA/;
54 @ISA=qw(RT::Action::Generic);
55
56 #Do what we need to do and send it out.
57
58 #What does this type of Action does
59
60 # {{{ sub Describe 
61 sub Describe  {
62   my $self = shift;
63   return (ref $self . " will move a ticket's priority toward its final priority.");
64 }
65 # }}}
66         
67
68 # {{{ sub Prepare 
69 sub Prepare  {
70     my $self = shift;
71     
72     if ($self->TicketObj->Priority() == $self->TicketObj->FinalPriority()) {
73         # no update necessary.
74         return 0;
75     }
76    
77     #compute the number of days until the ticket is due
78     my $due = $self->TicketObj->DueObj();
79     
80
81     # If we don't have a due date, adjust the priority by one
82     # until we hit the final priority
83     if ($due->Unix() < 1) {
84         if ( $self->TicketObj->Priority > $self->TicketObj->FinalPriority ){
85             $self->{'prio'} = ($self->TicketObj->Priority - 1);
86             return 1;
87         }
88         elsif ( $self->TicketObj->Priority < $self->TicketObj->FinalPriority ){
89             $self->{'prio'} = ($self->TicketObj->Priority + 1);
90             return 1;
91         }
92         # otherwise the priority is at the final priority. we don't need to
93         # Continue
94         else {
95             return 0;
96         }
97     }
98
99     # we've got a due date. now there are other things we should do
100     else { 
101         my $diff_in_seconds = $due->Diff(time());    
102         my $diff_in_days = int( $diff_in_seconds / 86400);    
103         
104         #if we haven't hit the due date yet
105         if ($diff_in_days > 0 ) {       
106             
107             # compute the difference between the current priority and the
108             # final priority
109             
110             my $prio_delta = 
111               $self->TicketObj->FinalPriority() - $self->TicketObj->Priority;
112             
113             my $inc_priority_by = int( $prio_delta / $diff_in_days );
114             
115             #set the ticket's priority to that amount
116             $self->{'prio'} = $self->TicketObj->Priority + $inc_priority_by;
117             
118         }
119         #if $days is less than 1, set priority to final_priority
120         else {  
121             $self->{'prio'} = $self->TicketObj->FinalPriority();
122         }
123
124     }
125     return 1;
126 }
127 # }}}
128
129 sub Commit {
130     my $self = shift;
131    my ($val, $msg) = $self->TicketObj->SetPriority($self->{'prio'});
132
133    unless ($val) {
134         $RT::Logger->debug($self . " $msg\n"); 
135    }
136 }
137
138 eval "require RT::Action::EscalatePriority_Vendor";
139 die $@ if ($@ && $@ !~ qr{^Can't locate RT/Action/EscalatePriority_Vendor.pm});
140 eval "require RT::Action::EscalatePriority_Local";
141 die $@ if ($@ && $@ !~ qr{^Can't locate RT/Action/EscalatePriority_Local.pm});
142
143 1;