# --
# Kernel/System/CustomerTimeAccounting.pm - get some timing infos
# Copyright (C) 2004 Stephan Lauffer <lauffer@ph-freiburg.de>
# --
# $Id: CustomerTimeAccounting.pm,v 0.01 2004/03/23 18:00:00 lauffer Exp $
# --
# 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::CustomerTimeAccounting;

use strict;
use Kernel::System::CheckItem;

use vars qw(@ISA $VERSION);
$VERSION = '$Revision: 0.01 $';
$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)) {
        $Self->{$_} = $Param{$_} || die "Got no $_!";
    }
    
    # load generator customer preferences module
    my $GeneratorModule = $Self->{ConfigObject}->Get('CustomerPreferences')->{Module}
      || 'Kernel::System::CustomerUser::Preferences::DB';
    eval "require $GeneratorModule";
    $Self->{PreferencesObject} = $GeneratorModule->new(%Param);

    # load master backend customer user module
    $GeneratorModule = $Self->{ConfigObject}->Get('CustomerUser')->{Module}
      || 'Kernel::System::CustomerUser::DB';
    eval "require $GeneratorModule";
    $Self->{CustomerUser} = $GeneratorModule->new(
        %Param,  
        PreferencesObject => $Self->{PreferencesObject},
        CustomerUserMap => $Self->{ConfigObject}->Get("CustomerUser"),
    );
 
    # load slave backend customer user module
    foreach (1..10) {
        if ($Self->{ConfigObject}->Get("CustomerUser$_")) {
            $GeneratorModule = $Self->{ConfigObject}->Get("CustomerUser$_")->{Module};
            eval "require $GeneratorModule";
            $Self->{"CustomerUser$_"} = $GeneratorModule->new(
                %Param,  
                PreferencesObject => $Self->{PreferencesObject},
                CustomerUserMap => $Self->{ConfigObject}->Get("CustomerUser$_"),
            );
        }
    }

    return $Self;
}

# --
sub run {
    my $Self = shift;
    my %Param = @_;
    my %CustomerTimeUnits;
    my %Period;
    
    # check needed stuff
    if (!$Param{UserCustomerID}) {
        $Self->{LogObject}->Log(Priority => 'error', Message => "Need UserCustomerID!");
	return;
    }

    %Period = (
    	ThisWeek  => "week(time_accounting.create_time)  = week(curdate())",
	ThisMonth => "month(time_accounting.create_time) = month(curdate())",
	ThisYear  => "year(time_accounting.create_time)  = year(curdate())",
	LastYear  => "year(time_accounting.create_time)  = year(curdate())-1",
    );

    foreach my $p (keys %Period) {
        $CustomerTimeUnits{"CustomerTimeAccounting$p"} = '0';
        $Self->{DBObject}->Prepare(SQL => "
            SELECT   sum(time_accounting.time_unit)
            FROM     time_accounting, ticket
            WHERE    time_accounting.ticket_id=ticket.id
            AND      ticket.customer_id='$Param{UserCustomerID}'
	    AND      $Period{$p}
            GROUP BY ticket.customer_user_id
        ");
	while (my @Row = $Self->{DBObject}->FetchrowArray()) {    
            $CustomerTimeUnits{"CustomerTimeAccounting$p"} = $Row[$0];
        }
    }
    
    return (%CustomerTimeUnits);
}
# --
1;
