Maintenance.pm 4.24 KB
# --
# Kernel/System/Maintenanca.pm - maintenance instance
#
# Copyright (C) 2014-2018 - SeTIC - UFSC - http://setic.ufsc.br/
# Version 2015-08-01 - Adjustments for OTRS 4
# Version 2018-01-05 - Adjustments for OTRS 6
#
# 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::Maintenance;

use strict;
use warnings;
use Data::Dumper;


our @ObjectDependencies = (
"Kernel::System::DB",
"Kernel::Output::HTML::Layout",	# OLD Layout object

"Kernel::System::Maintenance",
"Kernel::System::DateUtils",

"Kernel::System::Log",
"Kernel::Config"
);


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

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

	return $Self;
}

=item CreateMaintenance()

Creates a maintenance

    $ocurrenceObject->CreateMaintenance(    	
        Description => string
        StartDate => string (format yyyy-mm-dd hh:nn:ss)
        ScheduledEndDate => string (format yyyy-mm-dd hh:nn:ss)
        UserID => integer        
    );

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

	for (qw(Description StartDate ScheduledEndDate UserID)) {
		if ( !$Param{$_} ) {
			$Kernel::OM->Get("Kernel::Output::HTML::Layout")->FatalError( Message => Dumper( \%Param ) . "got no $_!" );
		}
	}
	
	my $status = $Self->SCHEDULED_STATUS();

	$Kernel::OM->Get("Kernel::System::DB")->Do(
		SQL  => "insert into maintenance (description, start_date, scheduled_end_date, user_id, status) values (?, ?, ?, ?, ?)",
		Bind => [ \$Param{Description}, \$Param{StartDate}, \$Param{ScheduledEndDate}, \$Param{UserID}, \$status ]
	);
}

sub ListMaintenances {
	my ( $Self, %Param ) = @_;
	my @retorno = ();
	
	my $dateUtils = $Kernel::OM->Get("Kernel::System::DateUtils");	
	my $type = $Param{Type} || "current_scheduled";
	my $sql = "SELECT id,description,start_date,scheduled_end_date,COALESCE(status,0),end_date,CURRENT_TIMESTAMP FROM maintenance WHERE ORDER BY start_date DESC, id asc";
	my $where = "";
	
	if ($Param{Type} eq "current") {
		$where = "WHERE COALESCE(status,0) = " . $Self->SCHEDULED_STATUS() . " AND start_date <= CURRENT_TIMESTAMP";
	} elsif ($Param{Type} eq "all") {
		$where = "";
	} elsif ($Param{Type} eq "scheduled") {
		$where = "WHERE COALESCE(status,0) = " . $Self->SCHEDULED_STATUS() . "  AND start_date > CURRENT_TIMESTAMP";
	} elsif ($Param{Type} eq "current_scheduled") {
		$where = "WHERE COALESCE(status,0) = " . $Self->SCHEDULED_STATUS();
	} elsif ($Param{Type} eq "canceled") {
		$where = "WHERE COALESCE(status,0) = " . $Self->CANCELED_STATUS();
	}
	
	$sql =~ s/WHERE/$where/;
	
	$Kernel::OM->Get("Kernel::System::DB")->Prepare(
		SQL => $sql,
		Bind => []
	);

	while ( my @row = $Kernel::OM->Get("Kernel::System::DB")->FetchrowArray() ) {
		my %ret = ();
		$ret{'ID'}          = $row[0];
		$ret{'Description'} = $row[1];
		$ret{'StartDate'}   = $row[2];
		$ret{'ScheduledEndDate'} = $row[3];
		$ret{'Status'} = $row[4];
		$ret{'EndDate'} = $row[5];
		
		my $startDate = $dateUtils->FromSQL(StringDate => $row[2]);
		my $currentDate = $dateUtils->FromSQL(StringDate => $row[6]);
		$ret{'Active'} = ($row[4] == $Self->SCHEDULED_STATUS()) && ($startDate < $currentDate);		
		push @retorno, \%ret;
	}

	return @retorno;
}

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

	$Kernel::OM->Get("Kernel::System::DB")->Prepare(
		SQL  => "UPDATE maintenance SET end_date=CURRENT_TIMESTAMP,status=1 WHERE id=?",
		Bind => [ \$Param{ID} ]
	);

}

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

	$Kernel::OM->Get("Kernel::System::DB")->Prepare(
		SQL  => "UPDATE maintenance SET status=" . $Self->CANCELED_STATUS() . " WHERE id=?",
		Bind => [ \$Param{ID} ]
	);

}

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

	$Kernel::OM->Get("Kernel::System::DB")->Prepare(
		SQL  => "SELECT count(id), max(id) FROM maintenance where start_date < CURRENT_TIMESTAMP and COALESCE(end_date,0) = 0 and status <> 2",
		Bind => []
	);

	my @row = $Kernel::OM->Get("Kernel::System::DB")->FetchrowArray();
	if ($row[0] != 0) {
		return ( $row[0], $row[1] );
	}
	else {
		return ( 0, 0 );
	}
}

sub SCHEDULED_STATUS {
	return 0;
}

sub FINISHED_STATUS {
	return 1;
}

sub CANCELED_STATUS {
	return 2;
}

1;