script/otrs.Escalation.pl untested

filter: Statementstmt 0.0% 0 / 315 Branchbran 0.0% 0 / 76 Conditioncond 0.0% 0 / 85 MC/DCmcdc 0.0% 0 / 53 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 552 CC 7 SCAR 18.3
CC
7
Cov
0%
_Getusrid
20.0
_SendNotification
12.0
_Unique
6.0
CRAP 6.2
1 #!/usr/local/bin/perl -w
2 # --
3 # otrs.Escalation.pl - Clean the ticket archive flag
4 # Copyright (C) 2001-2010 OTRS AG, http://otrs.org/
5 # --
6 # $Id: otrs.Escalation.pl,v 1.0 2013/02/06 17:49:20 en Exp $
7 # --
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU AFFERO General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 # or see http://www.gnu.org/licenses/agpl.txt.
22 # --
23
24 #use strict;
25 use warnings;
26
27 # use ../ as lib location
28 use File::Basename;
29 use FindBin qw($RealBin);
30 use lib dirname($RealBin);
31
32
33
34 use Kernel::Config;
35 use Kernel::System::Encode;
36 use Kernel::System::Log;
37 use Kernel::System::Time;
38 use Kernel::System::DB;
39 use Kernel::System::Main;
40 use Kernel::System::Ticket;
41 use Kernel::System::Queue;
42 use Kernel::System::Group;
43 use Kernel::System::SLA;
44 use Kernel::System::Email;
45 use Kernel::System::User;
46 use Kernel::System::Escalation;
47
48
49
50 # create common objects
51 my %CommonObject = ();
52 $CommonObject{ConfigObject} = Kernel::Config->new();
53 $CommonObject{EncodeObject} = Kernel::System::Encode->new(%CommonObject);
54 $CommonObject{LogObject} = Kernel::System::Log->new(
55 LogPrefix => 'OTRS-otrs.EscalationMatrix.pl',
56 %CommonObject,
57 );
58 $CommonObject{MainObject} = Kernel::System::Main->new(%CommonObject);
59 $CommonObject{TimeObject} = Kernel::System::Time->new(%CommonObject);
60 $CommonObject{DBObject} = Kernel::System::DB->new(%CommonObject);
61 $CommonObject{TicketObject} = Kernel::System::Ticket->new(%CommonObject);
62 $CommonObject{QueueObject} = Kernel::System::Queue->new(%CommonObject);
63 $CommonObject{GroupObject} = Kernel::System::Group->new(%CommonObject);
64 $CommonObject{SLAObject} = Kernel::System::SLA->new(%CommonObject);
65 $CommonObject{ESCLObject} = Kernel::System::Escalation->new(%CommonObject);
66 $CommonObject{EmailObject} = Kernel::System::Email->new(%CommonObject);
67 $CommonObject{UserObject} = Kernel::System::User->new(%CommonObject);
68
69
70 # declar variable to hold escalation and role parameter
71 my %ESCLData;
72 my %Roles = $CommonObject{GroupObject}->RoleList( Valid => 1 );
73 my $RolesRef = \%Roles;
74 my %RoleAct;
75
76
77 #get ticket IDs. Filter needs to be modified. @TicketIDs is the list of IDs which will be checked.
78 my @TicketIDs = $CommonObject{TicketObject}->TicketSearch(
79 Result => 'ARRAY',
80 SortBy => 'Age',
81 OrderBy => 'Down',
82 States => ['new', 'open'],
83 UserID => 1,
84 Limit => 600,
85 );
86
87
88 foreach my $TID (@TicketIDs)
89 {
90 #get ticket info.
91 my %Ticket = $CommonObject{TicketObject}->TicketGet(
92 TicketID => $TID,
93 );
94
95 #find ticket SLA type.
96 my $ticketSLAID = $Ticket{SLAID};
97 my $ticketGRPID = $Ticket{GroupID};
98 my $TktOwner = $Ticket{OwnerID};
99 my $TktNumber = $Ticket{TicketNumber};
100 my $TktQueueID = $Ticket{QueueID};
101
102 #################################
103
104
105 if ($ticketSLAID)
106 {
107
108 # Define some global variable to initialised value from DB
109
110 our $FR_NotifyTo_1;
111 our $FR_NotifyPerc_1;
112 our $FR_NotifyTo_2;
113 our $FR_NotifyPerc_2;
114 our $FR_NotifyTo_3;
115 our $FR_NotifyPerc_3;
116
117 our $UT_NotifyTo_1;
118 our $UT_NotifyPerc_1;
119 our $UT_NotifyTo_2;
120 our $UT_NotifyPerc_2;
121 our $UT_NotifyTo_3;
122 our $UT_NotifyPerc_3;
123
124 our $ST_NotifyTo_1;
125 our $ST_NotifyPerc_1;
126 our $ST_NotifyTo_2;
127 our $ST_NotifyPerc_2;
128 our $ST_NotifyTo_3;
129 our $ST_NotifyPerc_3;
130
131 # declar variable to hold actual roleid related to the ticket
132 my %Roleid;
133
134 ###############################################
135 # get escalation matrix
136 ###############################################
137 %ESCLData = $CommonObject{ESCLObject}->ESCLGet(
138 SLAID => $ticketSLAID,
139 UserID => 1,
140 );
141
142 my @Typearray = qw(FR UT ST);
143
144 foreach my $Type (@Typearray)
145 {
146 # can increase level of escalation
147 foreach my $Level (1..3)
148 {
149 #my $Ref = $Type . "_NotifyTo_" . $Level;
150 #my $RefPerc = $Type . "_NotifyPerc_" . $Level;
151 #$$Ref = $ESCLData{$ticketSLAID}{$Type}{$Level}{Notify_To};
152 #$$RefPerc = $ESCLData{$ticketSLAID}{$Type}{$Level}{Notify_Perc};
153 ${$Type . "_NotifyTo_" . $Level} = $ESCLData{$ticketSLAID}{$Type}{$Level}{Notify_To};
154 ${$Type . "_NotifyPerc_" . $Level} = $ESCLData{$ticketSLAID}{$Type}{$Level}{Notify_Perc};
155
156 }
157 }
158
159 ##########################################################
160 #list of all users in a group associated with ticket-group
161 ##########################################################
162 my %GroupUser = $CommonObject{GroupObject}->GroupGroupMemberList(
163 GroupID => $ticketGRPID,
164 Type => 'move_into',
165 Result => 'HASH',
166 );
167
168 #########################################
169 # finding role for each group user
170 #########################################
171
172 foreach my $key (keys %GroupUser)
173 {
174 my @UserRoleArray;
175
176 # get role ids in which the user is a member roleid=>userid
177 my %MemberRoleid = $CommonObject{GroupObject}->GroupUserRoleMemberList(
178 UserID => $key,
179 Result => 'HASH',
180 );
181 if (%MemberRoleid) {
182 foreach my $id (keys %MemberRoleid)
183 {
184 push(@UserRoleArray,$id);
185 }
186 $Roleid{$key} = \@UserRoleArray;
187 }
188 }
189
190 ##############################################
191
192 ##############################################
193 # populate the mail list
194 ##############################################
195 my @FR_NotifyMail_1;
196 my @FR_NotifyMail_2;
197 my @FR_NotifyMail_3;
198
199 my @UT_NotifyMail_1;
200 my @UT_NotifyMail_2;
201 my @UT_NotifyMail_3;
202
203 my @ST_NotifyMail_1;
204 my @ST_NotifyMail_2;
205 my @ST_NotifyMail_3;
206
207 # for FR find the mail list
208 #FR1
209 @FR_NotifyMail_1 = _Getusrid($FR_NotifyTo_1);
210 #FR2
211 @FR_NotifyMail_2 = _Getusrid($FR_NotifyTo_2);
212 push(@FR_NotifyMail_2, @FR_NotifyMail_1);
213 @FR_NotifyMail_2 = _Unique(\@FR_NotifyMail_2);
214
215
216 #FR3
217 @FR_NotifyMail_3 = _Getusrid($FR_NotifyTo_3);
218 push(@FR_NotifyMail_3, @FR_NotifyMail_2);
219 @FR_NotifyMail_3 = _Unique(\@FR_NotifyMail_3);
220
221 # for UT find the mail list
222 #UT1
223 @UT_NotifyMail_1 = _Getusrid($UT_NotifyTo_1);
224 #UT2
225 @UT_NotifyMail_2 = _Getusrid($UT_NotifyTo_2);
226 push(@UT_NotifyMail_2, @UT_NotifyMail_1);
227 @UT_NotifyMail_2 = _Unique(\@UT_NotifyMail_2);
228 #UT3
229 @UT_NotifyMail_3 = _Getusrid($UT_NotifyTo_3);
230 push(@UT_NotifyMail_3, @UT_NotifyMail_2);
231 @UT_NotifyMail_3 = _Unique(\@UT_NotifyMail_3);
232
233 # for ST find the mail list
234 #ST1
235 @ST_NotifyMail_1 = _Getusrid($ST_NotifyTo_1);
236 #ST2
237 @ST_NotifyMail_2 = _Getusrid($ST_NotifyTo_2);
238 push(@ST_NotifyMail_2, @ST_NotifyMail_1);
239 @ST_NotifyMail_2 = _Unique(\@ST_NotifyMail_2);
240 #ST3
241 @ST_NotifyMail_3 = _Getusrid($ST_NotifyTo_3);
242 push(@ST_NotifyMail_3, @ST_NotifyMail_2);
243 @ST_NotifyMail_3 = _Unique(\@ST_NotifyMail_3);
244 ######################################################
245
246 #####################################################
247 # find SLA details for a particulat ticket
248 #####################################################
249 my %SLAData = $CommonObject{SLAObject}->SLAGet(
250 SLAID => $ticketSLAID,
251 UserID => 1,
252 );
253 ###########################################
254 #percentage calculation of FR UT ST
255 ###########################################
256
257
258 ##########
259 #FR
260 ##########
261 my $FirstRespTime = $SLAData{FirstResponseTime}; #in minutes
262 my $FirstRespRemaining = $Ticket{FirstResponseTime}; #in seconds
263 my $percentElapsedFR;
264 if($FirstRespRemaining && $FirstRespTime){
265 #Calculate percent time elapsed
266 $percentElapsedFR = 100 - ((($FirstRespRemaining)/($FirstRespTime * 60))*100);
267 }
268 else{
269 $percentElapsedFR = -1;
270 }
271
272 if($FR_NotifyTo_1 && $FR_NotifyPerc_1) {
273 my $RoleName;
274 if($FR_NotifyTo_1 == -99) {
275 $RoleName = 'owner';
276 }
277 else {
278 $RoleName = $Roles{$FR_NotifyTo_1};
279 }
280 if(($FR_NotifyPerc_1 <= $percentElapsedFR)&&($percentElapsedFR >= 0)){
281 _SendNotification(\@FR_NotifyMail_1,$FR_NotifyPerc_1,$TID,'First Response -Level1',$ticketSLAID,$RoleName,$TktOwner,$TktNumber,$percentElapsedFR,'FRNotify1',$TktQueueID);
282 }
283 }
284
285 ##FR2
286 if($FR_NotifyTo_2 && $FR_NotifyPerc_2) {
287 my $RoleName;
288 if($FR_NotifyTo_2 == -99) {
289 $RoleName = 'owner';
290 }
291 else {
292 $RoleName = $Roles{$FR_NotifyTo_2};
293 }
294 if(($FR_NotifyPerc_2 <= $percentElapsedFR)&&($percentElapsedFR >= 0)){
295
296 _SendNotification(\@FR_NotifyMail_2,$FR_NotifyPerc_2,$TID,'First Response -Level2',$ticketSLAID,$RoleName,$TktOwner,$TktNumber,$percentElapsedFR,'FRNotify2',$TktQueueID);
297
298 }
299 }
300
301 ##FR3
302 if($FR_NotifyTo_3 && $FR_NotifyPerc_3) {
303 my $RoleName;
304 if($FR_NotifyTo_3 == -99)
305 {
306 $RoleName = 'owner';
307 }
308 else {
309 $RoleName = $Roles{$FR_NotifyTo_3};
310 }
311 if(($FR_NotifyPerc_3 <= $percentElapsedFR)&&($percentElapsedFR >= 0)){
312
313
314 _SendNotification(\@FR_NotifyMail_3,$FR_NotifyPerc_3,$TID,'First Response -Level3',$ticketSLAID,$RoleName,$TktOwner,$TktNumber,$percentElapsedFR,'FRNotify3',$TktQueueID);
315
316 }
317 }
318
319 ##########
320 #UT
321 ##########
322
323 my $UpdateTime = $SLAData{UpdateTime}; #in minutes
324 my $UpdateRemaining = $Ticket{UpdateTime}; #in seconds
325 my $percentElapsedUT;
326 if($UpdateRemaining && $UpdateTime) {
327 #Calculate percent time elapsed
328 $percentElapsedUT = 100 - ((($UpdateRemaining)/($UpdateTime * 60))*100);
329
330
331 }
332 else {
333 $percentElapsedUT = -1;
334 }
335
336 ##UT1
337
338 if($UT_NotifyTo_1 && $UT_NotifyPerc_1) {
339 my $RoleName;
340 if($UT_NotifyTo_1 == -99)
341 {
342 $RoleName = 'owner';
343 }
344 else
345 {
346 $RoleName = $Roles{$UT_NotifyTo_1};
347 }
348 if(($UT_NotifyPerc_1 <= $percentElapsedUT)&&($percentElapsedUT >= 0)){
349 _SendNotification(\@UT_NotifyMail_1,$UT_NotifyPerc_1,$TID,'Update Time -Level1',$ticketSLAID,$RoleName,$TktOwner,$TktNumber,$percentElapsedUT,'UTNotify1',$TktQueueID);
350
351 }
352 }
353
354 ##UT2
355 if($UT_NotifyTo_2 && $UT_NotifyPerc_2) {
356 my $RoleName;
357 if($UT_NotifyTo_2 == -99) {
358 $RoleName = 'owner';
359 }
360 else {
361 $RoleName = $Roles{$UT_NotifyTo_2};
362 }
363 if(($UT_NotifyPerc_2 <= $percentElapsedUT)&&($percentElapsedUT >= 0)){
364
365 _SendNotification(\@UT_NotifyMail_2,$UT_NotifyPerc_2,$TID,'Update Time -Level2',$ticketSLAID,$RoleName,$TktOwner,$TktNumber,$percentElapsedUT,'UTNotify2',$TktQueueID);
366
367 }
368 }
369
370 ##UT3
371 if($UT_NotifyTo_3 && $UT_NotifyPerc_3) {
372 my $RoleName;
373 if($UT_NotifyTo_3 == -99) {
374 $RoleName = 'owner';
375 }
376 else {
377 $RoleName = $Roles{$UT_NotifyTo_3};
378 }
379 if(($UT_NotifyPerc_3 <= $percentElapsedUT)&&($percentElapsedUT >= 0)){
380
381 _SendNotification(\@UT_NotifyMail_3,$UT_NotifyPerc_3,$TID,'Update Time -Level3',$ticketSLAID,$RoleName,$TktOwner,$TktNumber,$percentElapsedUT,'UTNotify3',$TktQueueID);
382
383 }
384 }
385
386 ##########
387 #ST
388 ##########
389
390 my $SolutionTime = $SLAData{SolutionTime}; #in minutes
391 my $SolutionRemaining = $Ticket{SolutionTime}; #in seconds
392 my $percentElapsedST;
393 if($SolutionRemaining && $SolutionTime){
394 #Calculate percent time elapsed
395 $percentElapsedST = 100 - ((($SolutionRemaining)/($SolutionTime * 60))*100);
396
397
398 }
399 else {
400 $percentElapsedST = -1;
401 }
402
403 ##ST1
404 if($ST_NotifyTo_1 && $ST_NotifyPerc_1) {
405 my $RoleName;
406 if($ST_NotifyTo_1 == -99)
407 {
408 $RoleName = 'owner';
409 }
410 else {
411 $RoleName = $Roles{$ST_NotifyTo_1};
412 }
413 if(($ST_NotifyPerc_1 <= $percentElapsedST)&&($percentElapsedST >= 0)){
414 _SendNotification(\@ST_NotifyMail_1,$ST_NotifyPerc_1,$TID,'Solution Time -Level1',$ticketSLAID,$RoleName,$TktOwner,$TktNumber,$percentElapsedST,'STNotify1',$TktQueueID);
415
416 }
417 }
418
419 ##ST2
420 if($ST_NotifyTo_2 && $ST_NotifyPerc_2) {
421 my $RoleName;
422 if($ST_NotifyTo_2 == -99) {
423 $RoleName = 'owner';
424 }
425 else {
426 $RoleName = $Roles{$ST_NotifyTo_2};
427 }
428 if(($ST_NotifyPerc_2 <= $percentElapsedST)&&($percentElapsedST >= 0)){
429
430 _SendNotification(\@ST_NotifyMail_2,$ST_NotifyPerc_2,$TID,'Solution Time -Level2',$ticketSLAID,$RoleName,$TktOwner,$TktNumber,$percentElapsedST,'STNotify2',$TktQueueID);
431
432 }
433 }
434
435 ##ST3
436 if($ST_NotifyTo_3 && $ST_NotifyPerc_3) {
437 my $RoleName;
438 if($ST_NotifyTo_3 == -99) {
439 $RoleName = 'owner';
440 }
441 else {
442 $RoleName = $Roles{$ST_NotifyTo_3};
443 }
444 if(($ST_NotifyPerc_3 <= $percentElapsedST)&&($percentElapsedST >= 0)){
445
446
447 _SendNotification(\@ST_NotifyMail_3,$ST_NotifyPerc_3,$TID,'Solution Time -Level3',$ticketSLAID,$RoleName,$TktOwner,$TktNumber,$percentElapsedST,'STNotify3',$TktQueueID);
448
449 }
450 }
451
452
453
454 #############################################################
455
456
457 $FR_NotifyTo_1 = '';
458 $FR_NotifyPerc_1 = '';
459 $FR_NotifyTo_2 = '';
460 $FR_NotifyPerc_2 = '';
461 $FR_NotifyTo_3 = '';
462 $FR_NotifyPerc_3 = '';
463 $UT_NotifyTo_1 = '';
464 $UT_NotifyPerc_1 = '';
465 $UT_NotifyTo_2 = '';
466 $UT_NotifyPerc_2 = '';
467 $UT_NotifyTo_3 = '';
468 $UT_NotifyPerc_3 = '';
469 $ST_NotifyTo_1 = '';
470 $ST_NotifyPerc_1 = '';
471 $ST_NotifyTo_2 = '';
472 $ST_NotifyPerc_2 = '';
473 $ST_NotifyTo_3 = '';
474 $ST_NotifyPerc_3 = '';
475
476
477 #########################################
478
479 sub _Unique{
480
481 #(@{$destArray})
482 my $Temparray = $_[0];
483 my @UniqueArray = ();
484 my %TempHash = ();
485
486 foreach my $Key (@{$Temparray}) {
487 if ($Key) {
488 $TempHash{$Key} = 1;
489 }
490
491 }
492
493 @UniqueArray = keys %TempHash;
494
495 return (@UniqueArray);
496
497 }
498
499 sub _Getusrid{
500
501 my $NotifyTo =$_[0];
502 my @NotifyMail;
503 if($NotifyTo) {
504 if($NotifyTo == -99) {
505 push (@NotifyMail,$Ticket{OwnerID});
506 return (@NotifyMail);
507 }
508 else {
509 foreach my $uid (keys %Roleid)
510 {
511
512 my $TempArrayAddress = \$Roleid{$uid};
513
514 foreach my $rid (@$$TempArrayAddress) {
515 if($NotifyTo == $rid) {
516 push(@NotifyMail,$uid);
517 }
518 }
519 }
520 return (@NotifyMail);
521 }
522 }
523 }
524 #############################################
525
526 } ## End of if ($ticketSLAID)
527
528 } ## End of foreach my $TID (@TicketIDs)
529
530
531
532 sub _SendNotification{
533
534 ##_SendNotification(\@FR_NotifyMail_1,$FR_NotifyPerc_3,$TID,'First Response -Level3',$ticketSLAID,$RoleName,$TktOwner,$TktNumber,$percentElapsedFR, 'FRNotify3',$TktQueueID);
535
536 # declear and initialised variable
537
538 my $destArray = $_[0];
539 my $Percent = $_[1] || '';
540 my $TicketID = $_[2] || '';
541 my $EscType = $_[3] || '';
542 my $SLAID = $_[4] || '';
543 my $level = $_[5] || '';
544 my $TktOwner = $_[6] || '';
545 my $TktNumber = $_[7] || '';
546 my $ActualPerc = $_[8] || '';
547 my $UKey = $_[9] || '';
548 my $TktQueueID = $_[10] || '';
549
550
551 my %TicketOwnerData = $CommonObject{UserObject}->GetUserData(
552 UserID => $TktOwner,
553 Valid => 1,
554 );
555
556 #[Ticket#2012101110000044] Ticket Escalation Before! (10% of First Response - Level1)
557
558 my $UniqueName = $TicketID . "-" . $SLAID . "-" . $UKey . "-" . $level;
559 my $Subject = "[Ticket#" . $TktNumber . "] Ticket Escalation Before! (" . $Percent . " % of " . $EscType . ")";
560 my $Body = "Ticket " . $TktNumber . " has breached " . $Percent . " % of " . $EscType . "\n";
561 $Body = $Body . "Ellapsed Percentage is : " . $ActualPerc . "\n";
562 $Body = $Body . "Ticket Owner ID : " . $TicketOwnerData{UserEmail} . "\n";
563 $Body = $Body . "SLA : " . $SLAID . "\n";
564 $Body = $Body . "\n \n";
565 $Body = $Body . "Regards \n";
566 $Body = $Body . "OTRS Admin \n";
567 $Body = $Body . "\n\nPlease Note:" . "\n" . "FR: First Response\n" . "UT: Update Time\n". "ST: Solution Time\n";
568 my $Msg = $Percent . " % of " . $EscType ;
569
570
571 my @Lines = $CommonObject{TicketObject}->HistoryGet(
572 TicketID => $TicketID,
573 UserID => 1,
574 );
575 my $Sent = 1;
576
577
578
579 for my $Line (@Lines)
580 {
581 #if ($Line->{Name} =~ /\%\%$UniqueName\%\%/ )
582 if ($Line->{Name} eq $UniqueName)
583 {
584 $Sent = 0;
585 }
586 }
587
588
589 #$Sent = 1;
590
591 if($Sent)
592 {
593 my $Queue = $CommonObject{QueueObject}->QueueLookup( QueueID => $TktQueueID );
594
595 $CommonObject{TicketObject}->HistoryAdd(
596 TicketID => $TicketID,
597 CreateUserID => 1,
598 HistoryType => 'Misc',
599 Name => $UniqueName,
600 );
601
602 foreach my $Recipient (@{$destArray}) {
603
604
605 my $Result = $CommonObject{TicketObject}->SendAgentNotification(
606 TicketID => $TicketID,
607 CustomerMessageParams => {
608 Queue => $Queue,
609 Body => $Msg || '',
610 },
611 Type => 'EscalationMatrix',
612 RecipientID => $Recipient,
613 UserID => 1,
614 );
615
616
617 #if ($Result) {
618 # open(FF,">>/tmp/tick.txt");
619 # print FF "\n sent -> $recipient, $UserData{UserEmail}, $Subject ---- \n";
620 # close(FF);
621 # print "Email sent! -> $recipient, $UserData{UserEmail}, $Subject ----\n";
622 #}
623 #else {
624 # print "Email not sent!\n";
625 #}
626 }
627 }
628 $Sent = 1;
629
630 }