| 1 | #!/usr/local/bin/perl -w | ||
| 2 | # -- | ||
| 3 | # otrs.AutoAllocation.pl - Clean the ticket archive flag | ||
| 4 | # Copyright (C) 2001-2010 OTRS AG, http://otrs.org/ | ||
| 5 | # -- | ||
| 6 | # $Id: otrs.AutoAllocation.pl,v 1.3 2013/02/06 17:49:20 cr 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 | use vars qw($VERSION); | ||
| 33 | $VERSION = qw($Revision: 1.3 $) [1]; | ||
| 34 | |||
| 35 | # | ||
| 36 | # use required modules | ||
| 37 | # | ||
| 38 | use Kernel::Config; | ||
| 39 | use Kernel::System::Encode; | ||
| 40 | use Kernel::System::Log; | ||
| 41 | use Kernel::System::Main; | ||
| 42 | use Kernel::System::DB; | ||
| 43 | use Kernel::System::Time; | ||
| 44 | use Kernel::System::Ticket; | ||
| 45 | use Kernel::System::AuthSession; | ||
| 46 | use Kernel::Modules::TicketAutoAssignment; | ||
| 47 | |||
| 48 | # | ||
| 49 | # create common objects | ||
| 50 | # | ||
| 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.AutoAllocation.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{GroupObject} = Kernel::System::Group->new(%CommonObject); | ||
| 63 | $CommonObject{SessionObject} = Kernel::System::AuthSession->new(%CommonObject); | ||
| 64 | $CommonObject{AutoAllocation} = Kernel::Modules::TicketAutoAssignment->new(); | ||
| 65 | |||
| 66 | |||
| 67 | # | ||
| 68 | # declar variables | ||
| 69 | # | ||
| 70 | my $TicketSearchLimit; | ||
| 71 | my @TicketIDs; | ||
| 72 | my $MatchedUserRef; | ||
| 73 | my @TicketAssignedSummary = (); | ||
| 74 | my @TicketAssignedFailSummary = (); | ||
| 75 | my $ArticleType = 'note-internal'; | ||
| 76 | my $SenderType = 'system'; | ||
| 77 | my $Subject = 'Owner Update'; | ||
| 78 | my $Body = 'Owner changed - Auto ticket assign'; | ||
| 79 | my $CharSet = 'ISO-8859-15'; | ||
| 80 | my $MimeType = 'text/plain'; | ||
| 81 | my $HistoryType = 'OwnerUpdate'; | ||
| 82 | my $HistoryComment = '%%Note'; | ||
| 83 | my $UserID = 1; | ||
| 84 | my $From = 'Admin OTRS'; | ||
| 85 | |||
| 86 | # | ||
| 87 | # get the required field from configuration item | ||
| 88 | # | ||
| 89 | $TicketSearchLimit = $CommonObject{ConfigObject}->{'Core::Ticket::AutoAssign'}->{ConfigurationItems}->{TicketSearchLimit}; | ||
| 90 | |||
| 91 | |||
| 92 | # check no of ticket search limit is not exceed the upper bound | ||
| 93 | if( ($TicketSearchLimit eq '') || ( $TicketSearchLimit > 1000) ) { | ||
| 94 | $TicketSearchLimit = 1000; | ||
| 95 | } | ||
| 96 | |||
| 97 | # | ||
| 98 | # get all tickets with an open statetype | ||
| 99 | # | ||
| 100 | @TicketIDs = $CommonObject{TicketObject}->TicketSearch( | ||
| 101 | StateType => ['new'], | ||
| 102 | States => ['new'], | ||
| 103 | Result => 'ARRAY', | ||
| 104 | Limit => $TicketSearchLimit || 1000, | ||
| 105 | UserID => 1, | ||
| 106 | OwnerIDs => [1], | ||
| 107 | Permission => 'ro', | ||
| 108 | ); | ||
| 109 | |||
| 110 | if ( !@TicketIDs ) { | ||
| 111 | $CommonObject{LogObject}->Log( | ||
| 112 | Priority => 'notice', | ||
| 113 | Message => "No new ticket found to assign support desk agent.", | ||
| 114 | ); | ||
| 115 | exit; | ||
| 116 | } | ||
| 117 | |||
| 118 | # | ||
| 119 | # get online users which is matched with given Group & Roles | ||
| 120 | # | ||
| 121 | $MatchedUserRef = &_GetOnlineAgent( \%CommonObject ); | ||
| 122 | |||
| 123 | # | ||
| 124 | # check each ticket | ||
| 125 | # | ||
| 126 | foreach my $TicketID ( @TicketIDs ) { | ||
| 127 | |||
| 128 | # declar variables | ||
| 129 | my $SuitableAgentID; | ||
| 130 | |||
| 131 | # get ticket details | ||
| 132 | my %TicketDetails = $CommonObject{TicketObject}->TicketGet( | ||
| 133 | TicketID => $TicketID, | ||
| 134 | UserID => 1, | ||
| 135 | ); | ||
| 136 | |||
| 137 | # if on-line user matched with given role & Group get suitable agent. | ||
| 138 | if( ref $MatchedUserRef ne 'ARRAY' ) { | ||
| 139 | $CommonObject{LogObject}->Log( | ||
| 140 | Priority => 'notice', | ||
| 141 | Message => "on-line users is not matched with given Role/Group OR no on-line user is found.", | ||
| 142 | ); | ||
| 143 | } | ||
| 144 | if( ref $MatchedUserRef eq 'ARRAY' and scalar @{$MatchedUserRef} >= 1 ) { | ||
| 145 | $SuitableAgentID = $CommonObject{AutoAllocation}->Run( UserIds => $MatchedUserRef, ); | ||
| 146 | } | ||
| 147 | |||
| 148 | # if no suitable agent found throw a message | ||
| 149 | if( !$SuitableAgentID ) { | ||
| 150 | $CommonObject{LogObject}->Log( | ||
| 151 | Priority => 'notice', | ||
| 152 | Message => "No suitable user found to assign ticket.", | ||
| 153 | ); | ||
| 154 | } | ||
| 155 | |||
| 156 | #Auto allocate the ticket if some suitable agent is identified | ||
| 157 | if( $SuitableAgentID ) { | ||
| 158 | |||
| 159 | my $SampleComment = "Ticket auto allocated using batch process."; | ||
| 160 | |||
| 161 | #set ticket state to lock. | ||
| 162 | $CommonObject{TicketObject}->TicketLockSet( | ||
| 163 | TicketID => $TicketID, | ||
| 164 | Lock => 'lock', | ||
| 165 | UserID => 1, | ||
| 166 | ); | ||
| 167 | |||
| 168 | #change ticket owner. | ||
| 169 | my $Success = $CommonObject{TicketObject}->TicketOwnerSet( | ||
| 170 | TicketID => $TicketID, | ||
| 171 | UserID => 1, | ||
| 172 | NewUserID => $SuitableAgentID, | ||
| 173 | Comment => $SampleComment, | ||
| 174 | ); | ||
| 175 | |||
| 176 | # set ticket state to open | ||
| 177 | $CommonObject{TicketObject}->TicketStateSet( | ||
| 178 | State => 'open', | ||
| 179 | TicketID => $TicketID, | ||
| 180 | UserID => 1, | ||
| 181 | ); | ||
| 182 | |||
| 183 | # article create | ||
| 184 | $CommonObject{TicketObject}->ArticleCreate( | ||
| 185 | TicketID => $TicketID, | ||
| 186 | ArticleType => $ArticleType, | ||
| 187 | SenderType => $SenderType, | ||
| 188 | From => $From, | ||
| 189 | Subject => $Subject, | ||
| 190 | Body => $Body, | ||
| 191 | Charset => $CharSet, | ||
| 192 | MimeType => $MimeType, | ||
| 193 | HistoryType => $HistoryType, | ||
| 194 | HistoryComment => $HistoryComment, | ||
| 195 | UserID => $UserID, | ||
| 196 | %TicketDetails, | ||
| 197 | ); | ||
| 198 | |||
| 199 | if ( $Success && $Success eq 1 ) { | ||
| 200 | push @TicketAssignedSummary, $TicketDetails{TicketNumber}; | ||
| 201 | } | ||
| 202 | else { | ||
| 203 | push @TicketAssignedFailSummary, $TicketDetails{TicketNumber}; | ||
| 204 | } | ||
| 205 | |||
| 206 | } | ||
| 207 | } | ||
| 208 | |||
| 209 | # | ||
| 210 | # ticket summary write in log | ||
| 211 | # | ||
| 212 | if( @TicketAssignedSummary && scalar @TicketAssignedSummary >=1 ) { | ||
| 213 | my $TotalTicket = scalar @TicketAssignedSummary; | ||
| 214 | $CommonObject{LogObject}->Log( | ||
| 215 | Priority => 'notice', | ||
| 216 | Message => "Total no of tickets assigned=$TotalTicket", | ||
| 217 | ); | ||
| 218 | } | ||
| 219 | if( @TicketAssignedFailSummary && scalar @TicketAssignedFailSummary >=1 ) { | ||
| 220 | my $TotalTicket = scalar @TicketAssignedFailSummary; | ||
| 221 | $CommonObject{LogObject}->Log( | ||
| 222 | Priority => 'notice', | ||
| 223 | Message => "Total no of tickets failed to assign=$TotalTicket", | ||
| 224 | ); | ||
| 225 | } | ||
| 226 | |||
| 227 | #---------------------------------------------------------- | ||
| 228 | # Get online agent -- START | ||
| 229 | #----------------------------------------------------------- | ||
| 230 | sub _GetOnlineAgent { | ||
| 231 | |||
| 232 | my $ObjectRef = $_[0]; | ||
| 233 | my %CommonObject = %{$ObjectRef}; | ||
| 234 | |||
| 235 | # | ||
| 236 | # declare variables | ||
| 237 | # | ||
| 238 | my %Online = (); | ||
| 239 | my @OnlineUsers; | ||
| 240 | my @SutableOnlineUsers; | ||
| 241 | my $MaxInactiveInterval; | ||
| 242 | my $RoleList; | ||
| 243 | my $GroupList; | ||
| 244 | my @Roles; | ||
| 245 | my @Groups; | ||
| 246 | my @UserBelongsToGroup; | ||
| 247 | my @UserBelongsToRole; | ||
| 248 | my $SuitableAgent; | ||
| 249 | |||
| 250 | # | ||
| 251 | # get required fields | ||
| 252 | # | ||
| 253 | $MaxInactiveInterval = $CommonObject{ConfigObject}->{'Core::Ticket::AutoAssign'}->{ConfigurationItems}->{MaxInactiveInterval}; | ||
| 254 | $RoleList = $CommonObject{ConfigObject}->{'Core::Ticket::AutoAssign'}->{ConfigurationItems}->{RoleList}; | ||
| 255 | $GroupList = $CommonObject{ConfigObject}->{'Core::Ticket::AutoAssign'}->{ConfigurationItems}->{GroupList}; | ||
| 256 | |||
| 257 | # clean rolelist & group list | ||
| 258 | if( $RoleList ) { | ||
| 259 | $RoleList =~ s/^\s*$//g; | ||
| 260 | $RoleList =~ s/^\s*//g; | ||
| 261 | $RoleList =~ s/\s*$//g; | ||
| 262 | $RoleList =~ s/\s*,\s*/,/g; | ||
| 263 | } | ||
| 264 | if( $GroupList ) { | ||
| 265 | $GroupList =~ s/^\s*$//g; | ||
| 266 | $GroupList =~ s/^\s*//g; | ||
| 267 | $GroupList =~ s/\s*$//g; | ||
| 268 | $GroupList =~ s/\s*,\s*/,/g; | ||
| 269 | } | ||
| 270 | |||
| 271 | my ( $Sec, $Min, $Hour, $Day, $Month, $Year, $WeekDay ) = $CommonObject{TimeObject}->SystemTime2Date( | ||
| 272 | SystemTime => $CommonObject{TimeObject}->SystemTime(), | ||
| 273 | ); | ||
| 274 | |||
| 275 | my $CurrentTime = $CommonObject{TimeObject}->Date2SystemTime( | ||
| 276 | Year => $Year, | ||
| 277 | Month => $Month, | ||
| 278 | Day => $Day, | ||
| 279 | Hour => $Hour, | ||
| 280 | Minute => $Min, | ||
| 281 | Second => $Sec, | ||
| 282 | ); | ||
| 283 | |||
| 284 | # if Group and Role list is not mentioned in config item | ||
| 285 | if( ( !$GroupList) and ( !$RoleList ) ) { | ||
| 286 | $CommonObject{LogObject}->Log( | ||
| 287 | Priority => 'notice', | ||
| 288 | Message => "Group or Role list is not mentioned in config item.", | ||
| 289 | ); | ||
| 290 | return; | ||
| 291 | } | ||
| 292 | |||
| 293 | # split roles and groups by , | ||
| 294 | if( $RoleList ) { | ||
| 295 | @Roles = split(',',$RoleList); | ||
| 296 | } | ||
| 297 | if( $GroupList ) { | ||
| 298 | @Groups = split(',',$GroupList); | ||
| 299 | } | ||
| 300 | |||
| 301 | # check and convert MaxInactiveInterval time into second | ||
| 302 | if( $MaxInactiveInterval ) { | ||
| 303 | # convert into seconds | ||
| 304 | $MaxInactiveInterval = $MaxInactiveInterval * 60; | ||
| 305 | } | ||
| 306 | else { | ||
| 307 | #set default value 15 Min. | ||
| 308 | $MaxInactiveInterval = 900; | ||
| 309 | } | ||
| 310 | |||
| 311 | # check for online Agent | ||
| 312 | my @Sessions = $CommonObject{SessionObject}->GetAllSessionIDs(); | ||
| 313 | for (@Sessions) { | ||
| 314 | my %Data = $CommonObject{SessionObject}->GetSessionIDData( SessionID => $_, ); | ||
| 315 | |||
| 316 | if (($Data{UserType} eq 'User') && (($CurrentTime - $Data{UserLastRequest} ) < $MaxInactiveInterval ) ) { | ||
| 317 | $Online{"UserLogin" } = $Data{UserLogin}; | ||
| 318 | $Online{"lastRequest"} = $Data{UserLastRequest}; | ||
| 319 | $Online{"userID"} = $Data{UserID}; | ||
| 320 | $Online{"userEmail"} = $Data{UserEmail}; | ||
| 321 | $Online{"userFirstname"}= $Data{UserFirstname}; | ||
| 322 | $Online{"userLastname"} = $Data{UserLastname}; | ||
| 323 | |||
| 324 | push(@OnlineUsers,$Data{UserID}); | ||
| 325 | } | ||
| 326 | } | ||
| 327 | |||
| 328 | # when no user is online | ||
| 329 | if ( !@OnlineUsers ) { | ||
| 330 | $CommonObject{LogObject}->Log( | ||
| 331 | Priority => 'notice', | ||
| 332 | Message => "No user is online.", | ||
| 333 | ); | ||
| 334 | exit; | ||
| 335 | } | ||
| 336 | |||
| 337 | # check each online user and match with given roles & groups | ||
| 338 | foreach my $OnlineUser (@OnlineUsers) { | ||
| 339 | my $GroupSQLQuery; | ||
| 340 | my $GroupUserSQLQuery; | ||
| 341 | my $RoleSQLQuery; | ||
| 342 | my $RoleUserSQLQuery; | ||
| 343 | my $GroupID; | ||
| 344 | my $RoleID; | ||
| 345 | |||
| 346 | # check user is part of given groups | ||
| 347 | foreach my $Group ( @Groups ) { | ||
| 348 | $GroupID = $CommonObject{GroupObject}->GroupLookup( Group => $Group ); | ||
| 349 | |||
| 350 | my @Users = $CommonObject{GroupObject}->GroupMemberList( | ||
| 351 | GroupID => $GroupID, | ||
| 352 | Type => 'rw', | ||
| 353 | Result => 'ID', | ||
| 354 | ); | ||
| 355 | |||
| 356 | foreach ( @Users ) { | ||
| 357 | if ( $_ == $OnlineUser) { | ||
| 358 | push @UserBelongsToGroup, $_; | ||
| 359 | } | ||
| 360 | } | ||
| 361 | } | ||
| 362 | |||
| 363 | # check user is part of given roles | ||
| 364 | foreach my $Role ( @Roles ) { | ||
| 365 | $RoleID = $CommonObject{GroupObject}->RoleLookup( Role => $Role ); | ||
| 366 | |||
| 367 | my @Users = $CommonObject{GroupObject}->GroupUserRoleMemberList( | ||
| 368 | RoleID => $RoleID, | ||
| 369 | Result => 'ID', | ||
| 370 | ); | ||
| 371 | |||
| 372 | foreach ( @Users ) { | ||
| 373 | if ( $_ == $OnlineUser ) { | ||
| 374 | push @UserBelongsToRole, $_; | ||
| 375 | } | ||
| 376 | } | ||
| 377 | } | ||
| 378 | } | ||
| 379 | |||
| 380 | # get the unique online user list from matched group list | ||
| 381 | my %TempHash1; | ||
| 382 | if( scalar @UserBelongsToGroup > 1 ) { | ||
| 383 | @UserBelongsToGroup = grep { ! $TempHash1{ $_ }++ } @UserBelongsToGroup; | ||
| 384 | } | ||
| 385 | |||
| 386 | # get the unique online user list from matched role list | ||
| 387 | my %TempHash2; | ||
| 388 | if( scalar @UserBelongsToRole > 1 ) { | ||
| 389 | @UserBelongsToRole = grep { ! $TempHash2{ $_ }++ } @UserBelongsToRole; | ||
| 390 | } | ||
| 391 | |||
| 392 | # when both group and Role is mentioned in config item | ||
| 393 | if( ($GroupList and $GroupList ne "") and ($RoleList and $RoleList ne "") ) { | ||
| 394 | |||
| 395 | # return matched users belongs to Group and Roles | ||
| 396 | if( scalar @UserBelongsToGroup >= 1 && scalar @UserBelongsToRole >= 1 ) { | ||
| 397 | |||
| 398 | # get common users from two list | ||
| 399 | my @CommonUser = (); | ||
| 400 | my %Count = map { $_ => 1 } @UserBelongsToGroup; | ||
| 401 | @CommonUser = grep { $Count{$_} } @UserBelongsToRole; | ||
| 402 | if( @CommonUser && scalar @CommonUser >= 1 ) { | ||
| 403 | return \@CommonUser; | ||
| 404 | } | ||
| 405 | else { | ||
| 406 | return; | ||
| 407 | } | ||
| 408 | } | ||
| 409 | else { | ||
| 410 | return; | ||
| 411 | } | ||
| 412 | } | ||
| 413 | |||
| 414 | # when only group is mentioned in config item | ||
| 415 | if( ($GroupList and $GroupList ne "") and (!$RoleList) ) { | ||
| 416 | if ( scalar @UserBelongsToGroup >= 1 ) { | ||
| 417 | return \@UserBelongsToGroup; | ||
| 418 | } | ||
| 419 | else { | ||
| 420 | return; | ||
| 421 | } | ||
| 422 | } | ||
| 423 | |||
| 424 | # when only role is mentioned in config item | ||
| 425 | if( ($RoleList and $RoleList ne "") and (!$GroupList) ) { | ||
| 426 | if ( scalar @UserBelongsToRole >= 1 ) { | ||
| 427 | return \@UserBelongsToRole; | ||
| 428 | } | ||
| 429 | else { | ||
| 430 | return; | ||
| 431 | } | ||
| 432 | } | ||
| 433 | |||
| 434 | return ; | ||
| 435 | } | ||
| 436 | #---------------------------------------------------------- | ||
| 437 | # Get online agent -- END | ||
| 438 | #----------------------------------------------------------- | ||
| 439 |