<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># --
# Kernel/System/ProcessManagement/TransitionAction/TicketStateSet.pm - A Module to set the ticket state
# Copyright (C) 2001-2014 OTRS AG, http://otrs.com/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (AGPL). If you
# did not receive this file, see http://www.gnu.org/licenses/agpl.txt.
# --

package Kernel::System::ProcessManagement::TransitionAction::TicketStateSet;

use strict;
use warnings;
use Kernel::System::VariableCheck qw(:all);

use utf8;
use Kernel::System::State;

=head1 NAME

Kernel::System::ProcessManagement::TransitionAction::TicketStateSet - A module to set the ticket state

=head1 SYNOPSIS

All TicketStateSet functions.

=head1 PUBLIC INTERFACE

=over 4

=cut

=item new()

create an object

    use Kernel::Config;
    use Kernel::System::Encode;
    use Kernel::System::Log;
    use Kernel::System::Time;
    use Kernel::System::Main;
    use Kernel::System::DB;
    use Kernel::System::Ticket;
    use Kernel::System::ProcessManagement::TransitionAction::TicketStateSet;

    my $ConfigObject = Kernel::Config-&gt;new();
    my $EncodeObject = Kernel::System::Encode-&gt;new(
        ConfigObject =&gt; $ConfigObject,
    );
    my $LogObject = Kernel::System::Log-&gt;new(
        ConfigObject =&gt; $ConfigObject,
        EncodeObject =&gt; $EncodeObject,
    );
    my $TimeObject = Kernel::System::Time-&gt;new(
        ConfigObject =&gt; $ConfigObject,
        LogObject    =&gt; $LogObject,
    );
    my $MainObject = Kernel::System::Main-&gt;new(
        ConfigObject =&gt; $ConfigObject,
        EncodeObject =&gt; $EncodeObject,
        LogObject    =&gt; $LogObject,
    );
    my $DBObject = Kernel::System::DB-&gt;new(
        ConfigObject =&gt; $ConfigObject,
        EncodeObject =&gt; $EncodeObject,
        LogObject    =&gt; $LogObject,
        MainObject   =&gt; $MainObject,
    );
    my $TicketObject = Kernel::System::Ticket-&gt;new(
        ConfigObject       =&gt; $ConfigObject,
        LogObject          =&gt; $LogObject,
        DBObject           =&gt; $DBObject,
        MainObject         =&gt; $MainObject,
        TimeObject         =&gt; $TimeObject,
        EncodeObject       =&gt; $EncodeObject,
    );
    my $TicketStateSetActionObject = Kernel::System::ProcessManagement::TransitionAction::TicketStateSet-&gt;new(
        ConfigObject       =&gt; $ConfigObject,
        LogObject          =&gt; $LogObject,
        EncodeObject       =&gt; $EncodeObject,
        DBObject           =&gt; $DBObject,
        MainObject         =&gt; $MainObject,
        TimeObject         =&gt; $TimeObject,
        TicketObject       =&gt; $TicketObject,
    );

=cut

sub new {
    my ( $Type, %Param ) = @_;

    # allocate new hash for object
    my $Self = {};
    bless( $Self, $Type );

    # get needed objects
    for my $Needed (
        qw(ConfigObject LogObject EncodeObject DBObject MainObject TimeObject TicketObject)
        )
    {
        die "Got no $Needed!" if !$Param{$Needed};

        $Self-&gt;{$Needed} = $Param{$Needed};
    }

    $Self-&gt;{StateObject} = Kernel::System::State-&gt;new(
        %Param,
        DBObject   =&gt; $Self-&gt;{DBObject},
        MainObject =&gt; $Self-&gt;{MainObject},
        TimeObject =&gt; $Self-&gt;{TimeObject},
    );

    return $Self;
}

=item Run()

    Run Data

    my $TicketStateSetResult = $TicketStateSetActionObject-&gt;Run(
        UserID                   =&gt; 123,
        Ticket                   =&gt; \%Ticket,   # required
        ProcessEntityID          =&gt; 'P123',
        ActivityEntityID         =&gt; 'A123',
        TransitionEntityID       =&gt; 'T123',
        TransitionActionEntityID =&gt; 'TA123',
        Config                   =&gt; {
            State   =&gt; 'open',
            # or
            StateID =&gt; 3,

            PendingTimeDiff =&gt; 123,             # optional, used for pending states, difference in seconds from
                                                #   current time to desired penting time (e.g. a value of 3600 means
                                                #   that the pending time will be 1hr after the Transition Action is
                                                #   executed)
            UserID  =&gt; 123,                     # optional, to override the UserID from the logged user
        }
    );
    Ticket contains the result of TicketGet including DynamicFields
    Config is the Config Hash stored in a Process::TransitionAction's  Config key
    Returns:

    $TicketStateSetResult = 1; # 0

=cut

sub Run {
    my ( $Self, %Param ) = @_;

    for my $Needed (
        qw(UserID Ticket ProcessEntityID ActivityEntityID TransitionEntityID
        TransitionActionEntityID Config
        )
        )
    {
        if ( !defined $Param{$Needed} ) {
            $Self-&gt;{LogObject}-&gt;Log(
                Priority =&gt; 'error',
                Message  =&gt; "Need $Needed!",
            );
            return;
        }
    }

    # define a common message to output in case of any error
    my $CommonMessage = "Process: $Param{ProcessEntityID} Activity: $Param{ActivityEntityID}"
        . " Transition: $Param{TransitionEntityID}"
        . " TransitionAction: $Param{TransitionActionEntityID} - ";

    # Check if we have Ticket to deal with
    if ( !IsHashRefWithData( $Param{Ticket} ) ) {
        $Self-&gt;{LogObject}-&gt;Log(
            Priority =&gt; 'error',
            Message  =&gt; $CommonMessage . "Ticket has no values!",
        );
        return;
    }

    # Check if we have a ConfigHash
    if ( !IsHashRefWithData( $Param{Config} ) ) {
        $Self-&gt;{LogObject}-&gt;Log(
            Priority =&gt; 'error',
            Message  =&gt; $CommonMessage . "Config has no values!",
        );
        return;
    }

    # override UserID if specified as a parameter in the TA config
    if ( IsNumber( $Param{Config}-&gt;{UserID} ) ) {
        $Param{UserID} = $Param{Config}-&gt;{UserID};
        delete $Param{Config}-&gt;{UserID};
    }

    if ( !$Param{Config}-&gt;{StateID} &amp;&amp; !$Param{Config}-&gt;{State} ) {
        $Self-&gt;{LogObject}-&gt;Log(
            Priority =&gt; 'error',
            Message  =&gt; $CommonMessage . "No State or StateID configured!",
        );
        return;
    }

    my $Success;
    my %StateData;

    # If Ticket's StateID is already the same as the Value we
    # should set it to, we got nothing to do and return success
    if (
        defined $Param{Config}-&gt;{StateID}
        &amp;&amp; $Param{Config}-&gt;{StateID} eq $Param{Ticket}-&gt;{StateID}
        )
    {
        return 1;
    }

    # If Ticket's StateID is not the same as the Value we
    # should set it to, set the StateID
    elsif (
        defined $Param{Config}-&gt;{StateID}
        &amp;&amp; $Param{Config}-&gt;{StateID} ne $Param{Ticket}-&gt;{StateID}
        )
    {
        %StateData = $Self-&gt;{StateObject}-&gt;StateGet(
            ID =&gt; $Param{Config}-&gt;{StateID},
        );
        $Success = $Self-&gt;{TicketObject}-&gt;TicketStateSet(
            TicketID =&gt; $Param{Ticket}-&gt;{TicketID},
            StateID  =&gt; $Param{Config}-&gt;{StateID},
            UserID   =&gt; $Param{UserID},
        );

        if ( !$Success ) {
            $Self-&gt;{LogObject}-&gt;Log(
                Priority =&gt; 'error',
                Message  =&gt; $CommonMessage
                    . 'Ticket StateID '
                    . $Param{Config}-&gt;{StateID}
                    . ' could not be updated for Ticket: '
                    . $Param{Ticket}-&gt;{TicketID} . '!',
            );
        }
    }

    # If Ticket's State is already the same as the Value we
    # should set it to, we got nothing to do and return success
    elsif (
        defined $Param{Config}-&gt;{State}
        &amp;&amp; $Param{Config}-&gt;{State} eq $Param{Ticket}-&gt;{State}
        )
    {
        return 1;
    }

    # If Ticket's State is not the same as the Value we
    # should set it to, set the State
    elsif (
        defined $Param{Config}-&gt;{State}
        &amp;&amp; $Param{Config}-&gt;{State} ne $Param{Ticket}-&gt;{State}
        )
    {
        %StateData = $Self-&gt;{StateObject}-&gt;StateGet(
            Name =&gt; $Param{Config}-&gt;{State},
        );
        $Success = $Self-&gt;{TicketObject}-&gt;TicketStateSet(
            TicketID =&gt; $Param{Ticket}-&gt;{TicketID},
            State    =&gt; $Param{Config}-&gt;{State},
            UserID   =&gt; $Param{UserID},
        );

        if ( !$Success ) {
            $Self-&gt;{LogObject}-&gt;Log(
                Priority =&gt; 'error',
                Message  =&gt; $CommonMessage
                    . 'Ticket State '
                    . $Param{Config}-&gt;{State}
                    . ' could not be updated for Ticket: '
                    . $Param{Ticket}-&gt;{TicketID} . '!',
            );
        }
    }
    else {
        $Self-&gt;{LogObject}-&gt;Log(
            Priority =&gt; 'error',
            Message  =&gt; $CommonMessage
                . "Couldn't update Ticket State - can't find valid State parameter!",
        );
        return;
    }

    # set pending time
    if (
        $Success
        &amp;&amp; IsHashRefWithData( \%StateData )
        &amp;&amp; $StateData{TypeName} =~ m{\A pending}msxi
        &amp;&amp; IsNumber( $Param{Config}-&gt;{PendingTimeDiff} )
        )
    {

        # get current time
        my $PendingTime = $Self-&gt;{TimeObject}-&gt;SystemTime();

        # add PendingTimeDiff
        $PendingTime += $Param{Config}-&gt;{PendingTimeDiff};

        # convert pending time to time stamp
        my $PendingTimeString = $Self-&gt;{TimeObject}-&gt;SystemTime2TimeStamp(
            SystemTime =&gt; $PendingTime,
        );

        # set pending time
        $Self-&gt;{TicketObject}-&gt;TicketPendingTimeSet(
            UserID   =&gt; $Param{UserID},
            TicketID =&gt; $Param{Ticket}-&gt;{TicketID},
            String   =&gt; $PendingTimeString,
        );
    }

    return $Success;
}

1;

=back

=head1 TERMS AND CONDITIONS

This software is part of the OTRS project (L&lt;http://otrs.org/&gt;).

This software comes with ABSOLUTELY NO WARRANTY. For details, see
the enclosed file COPYING for license information (AGPL). If you
did not receive this file, see L&lt;http://www.gnu.org/licenses/agpl.txt&gt;.

=cut
</pre></body></html>