# --
# Kernel/System/Stats/State_Changes.pm - stats module
# --
# 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::Stats::State_Changes;

use strict;
use Date::Pcalc qw(Days_in_Month);
use Kernel::System::CustomerUser;

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

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

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

    # get common opjects
    foreach (keys %Param) {
        $Self->{$_} = $Param{$_};
    }

    # check all needed objects
    foreach (qw(DBObject ConfigObject LogObject)) {
        die "Got no $_" if (!$Self->{$_});
    }

    return $Self;
}
# --
sub Param {
    my $Self = shift;
    my @Params = ();
    # get current time
    my ($s,$m,$h, $D,$M,$Y) = $Self->{TimeObject}->SystemTime2Date(
        SystemTime => $Self->{TimeObject}->SystemTime(),
    );
    # get one month bevore
    if ($M == 1) {
        $M = 12;
        $Y = $Y - 1;
    }
    else {
        $M = $M -1;
    }
    # create possible time selections
    my %Year = ();
    foreach ($Y-10..$Y+1) {
        $Year{$_} = $_;
    }
    my %Month = ();
    foreach (1..12) {
        my $Tmp = sprintf("%02d", $_);
        $Month{$_} = $Tmp;
    }

    push (@Params, {
            Frontend => 'Year',
            Name => 'Year',
            Multiple => 0,
            Size => 0,
            SelectedID => $Y,
            Data => {
                %Year,
            },
        },
    );
    push (@Params, {
            Frontend => 'Month',
            Name => 'Month',
            Multiple => 0,
            Size => 0,
            SelectedID => $M,
            Data => {
                %Month,
            },
        },
    );
    return @Params;
}
# --
sub Run {
    my $Self = shift;
    my %Param = @_;
    $Param{Month} = sprintf("%02d", $Param{Month});
    my $Title = "$Param{Year}-$Param{Month}";
    my @HeadData = ('Status', 'Anzahl', '24hdiff', 'Servicediff');
    my @Data = ();
    # get accounted time
    my @Tickets = ();

    my %StatesNumber = $Self->_GetStateTypes();
    my %States24h = %StatesNumber;
    my %StatesService = %StatesNumber;

    my $SQL = <<ENDE;
SELECT
 SUBSTRING_INDEX(TRIM(BOTH '%%' FROM paare.name), '%%', 1) AS Status,
 /*SUBSTRING_INDEX(TRIM(BOTH '%%' FROM paare.name), '%%', -1) AS Folgestatus,*/
 th.create_time AS Von, paare.time2 AS Bis,
 (UNIX_TIMESTAMP(paare.time2)-UNIX_TIMESTAMP(th.create_time))/3600 AS Stunden,
 0.1 AS dummy
FROM
( /* History-Paare der aufeinanderfolgenden Statusaenderungen */
 SELECT
 th2.name,
 (SELECT MAX(id) FROM ticket_history WHERE ticket_id = th2.ticket_id AND history_type_id IN (1, 27) AND id < th2.id) AS id1,
 th2.id AS id2,
 th2.create_time AS time2
FROM ticket_history th2
WHERE th2.history_type_id = 27 /*  Tickets oder State-Updates */
 AND YEAR(th2.create_time) = $Param{Year}
 AND MONTH(th2.create_time) = $Param{Month}
) AS paare
INNER JOIN ticket_history th
ON paare.id1 = th.id
/*ORDER BY th.ticket_id, paare.id2;*/
ENDE

    $Self->{DBObject}->Prepare(SQL => $SQL);
    while (my @Row = $Self->{DBObject}->FetchrowArray()) {
        # Zeitspanne innerhalb der Service-Zeit berechnen
        $Row[4] = $Self->{TimeObject}->WorkingTime(
            StartTime => $Self->{TimeObject}->TimeStamp2SystemTime(String => $Row[1]),
            StopTime => $Self->{TimeObject}->TimeStamp2SystemTime(String => $Row[2]),
        ) / 3600;
        $StatesNumber{$Row[0]}++;
        $States24h{$Row[0]} += $Row[3];
        $StatesService{$Row[0]} += $Row[4];
        push (@Data, \@Row);
    }

    my @Data = ();
    foreach my $State (sort {$a cmp $b} keys %StatesNumber) {
        next unless $StatesNumber{$State}; # Stati die nicht vorgekommen sind ueberspringen
        my @Row = ($State, $StatesNumber{$State});
        push (@Row, sprintf('%0.2f', $States24h{$State}/$StatesNumber{$State}));
        push (@Row, sprintf('%0.2f', $StatesService{$State}/$StatesNumber{$State}));
        push(@Data, \@Row);
    }

    return ([$Title],[@HeadData], @Data);
}
# --
sub _GetStateTypes {
    my $Self = shift;
    my %Param = @_;
    my %Stats;
    my $SQL = "SELECT id, name FROM ticket_state";
    $Self->{DBObject}->Prepare(SQL => $SQL);
    while (my @RowTmp = $Self->{DBObject}->FetchrowArray()) {
        $Stats{$RowTmp[1]} = 0;
    }
    return %Stats;
}
1;
