# --
# Kernel/System/GenericAgent/DeleteOldClosedTickets.pm - generic agent to delete old closed tickets
##_ by Alexander Scholler
# Needed Parameters:
#   AgeInDays (all closed tickets with closure-age older than DayInterval days are marked to be deleted)
#   DeleteReally (all marked tickets will be deleted(!) if this parameter is set to 1)
# Closure-age is the age of the last state-update (towards a closed-state)
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see http://www.gnu.org/licenses/gpl.txt.
# --

package Kernel::System::GenericAgent::DeleteOldClosedTickets;

use strict;

use vars qw(@ISA $VERSION);
$VERSION = '$Revision: 1.1 $';
$VERSION =~ s/^\$.*:\W(.*)\W.+?$/$1/;

# --
sub new {
    my $Type = shift;
    my %Param = @_;

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

    # check needed objects
    foreach (qw(DBObject ConfigObject LogObject TicketObject TimeObject)) {
        $Self->{$_} = $Param{$_} || die "Got no $_!";
    }

    # 0=off; 1=on;
    $Self->{Debug} = $Param{Debug} || 0;

    return $Self;
}
# --
sub Run {
    my $Self = shift;
    my %Param = @_;
    my $Age = 0; # Age of ticket in seconds
    my $Interval = 3600 * 24 * 365.25 * 10;
    my $LatestClosure = 0;
    # check needed param
    if (!$Param{New}->{"AgeInDays"}) {
        $Self->{LogObject}->Log(
            Priority => 'error',
            Message => "Need AgeInDays param for GenericAgent module!",
        );
        return;
    }
    else {
        $Interval = $Param{New}->{"AgeInDays"} * 3600 * 24;
    }
    my $DeleteReally = ($Param{New}->{"DeleteReally"})?$Param{New}->{"DeleteReally"}:0; 
    # get ticket data
    my %Ticket = $Self->{TicketObject}->TicketGet(%Param);
 
    # abort if the ticket is not closed
    return if ($Ticket{StateType} ne 'closed');

    my @HistoryLines = $Self->{TicketObject}->HistoryGet(%Param, UserID => 1);

    # find the time of (last) ticket closure
    foreach my $History (@HistoryLines) {
        $LatestClosure = $History->{CreateTime}
          if ( $History->{HistoryType} eq 'StateUpdate' && $History->{Name} =~ /^\%\%.+\%\%closed.*\%\%$/ );
    }
    # closed tickets without state-update were created with closed-state
    $LatestClosure = $Ticket{Created} if (!$LatestClosure);
    $LatestClosure = $Self->{TimeObject}->TimeStamp2SystemTime( String => $LatestClosure );
    # get seconds after last ticket closure
    $Age = time() - $LatestClosure;

    # is this ticket old enough to be deleted
    return 1 unless ($Age > $Interval);

    # for safety reasons: parameter DeleteReally = 1 must be given for executing the deletion
    if ($DeleteReally == 1) {
        $Self->{LogObject}->Log(
            Priority => 'Notice',
            Message => sprintf "Delete ticket %s/%s due to closure-age %d > %d seconds",
                               $Ticket{TicketNumber}, $Ticket{TicketID}, $Age, $Interval
        );
        $Self->{TicketObject}->TicketDelete(
            UserID => $Ticket{TicketNumber},
            TicketID => $Ticket{TicketID},
        );
    }
    else {
        $Self->{LogObject}->Log(
            Priority => 'Notice',
            Message => sprintf "No delete on %s/%s with closure-age %d > %d seconds done due to missing module-param DeleteReally = 1",
                               $Ticket{TicketNumber}, $Ticket{TicketID}, $Age, $Interval
        );
    }
    return 1;
}
# --
1;
