#!/usr/bin/perl

#######################################################
#  Name:    weekly_timesheet.pl
#  version: 1.0
#  Date:    May 15, 2007
#  Author:  Mathew Snyder
#######################################################

use warnings;
use strict;
use lib '/usr/local/rt-3.6.1/lib';
use lib '/usr/local/rt-3.6.1/local/lib';
use RT;
use RT::Tickets;
use RT::Users;
use MIME::Lite;

RT::LoadConfig();
RT::Init();

# Declare our global variables
my (@days, @months, @years, @searchDate);
my $time = time();

for (1 .. 7) {
  $time -= 24*60*60;
  my @date = (localtime($time))[3 .. 5];
  push @days, (sprintf '%02d', $date[0]);
  push @months,(sprintf '%02d',$date[1] + 1);
  push @years, $date[2] + 1900;
  push @searchDate, join "-", ($date[2] + 1900), (sprintf '%02d',$date[1] + 1), (sprintf '%02d', $date[0]);
}

my $startDate	= join "-", $months[$#months], $days[$#days], $years[$#years];
my $endDate	= join "-", $months[0], $days[0], $years[0];
my $searchStart = $searchDate[6];
#my $searchStart = join "-", $years[$#years], $months[$#months], $days[$#days];
my $searchEnd   = $searchDate[0];
#my $searchEnd   = join "-", $years[0], $months[0], $days[0];

my $tix = new RT::Tickets(RT::SystemUser);
$tix->FromSQL('Queue = "CustomerCare" AND ((LastUpdated > "' . $searchStart . '" AND LastUpdated < "' . $searchEnd . '") OR (LastUpdated = "' . $searchStart . '" OR LastUpdated = "' . $searchEnd. '"))');

# The nitty gritty of the script
open TIMESHEET, ">/work_reports/weekly/debug/timesheet_weekof_$endDate.txt";

print TIMESHEET "Timesheet for $startDate to $endDate\n";

foreach my $day (@searchDate) {
	my %environment;
	my %ticCount;
        my $displayDate = join "-", (shift @months), (shift @days), (shift @years);

        # Print the header for our data
        printf TIMESHEET "\n\n" . $displayDate . "\n";
        printf TIMESHEET "%32s%11s\n", "Time", "Avg Time";
        printf TIMESHEET "%18s%7s%7s%11s\n", "Profile", "Tkts", "hh:mm", "hh:mm";
        print  TIMESHEET ("-" x 44);
        print  TIMESHEET "\n";

	while (my $ticket = $tix->Next) {
		my $env = $ticket->FirstCustomFieldValue('Environment');
		(my $updt = $ticket->LastUpdated) =~ s/\s.*$//;
		if ($updt eq $day) {
			$ticCount{$day}{$env}++;
        		my $transactions = $ticket->Transactions;
			while (my $transaction  = $transactions->Next) {
				(my $date = $transaction->Created) =~ s/\s.*$//;
                		next unless ($transaction->TimeTaken);
				next unless ($date eq $day);
				$environment{$day}{$env} += $transaction->TimeTaken;
        		}
		}else{
			next;
		}
	}

	foreach my $date1 (keys %environment) {
		foreach my $enviro (sort keys %{ $environment{$date1} }) {
			my @endTime;
			my @endtime2;
			my $temp	  = $environment{$date1}{$enviro};
			my $temp2	  = $temp / 60;
			my @temp	  = split /\./, $temp2;
			$endTime[0]	  = $temp[0];
			$endTime[1]	  = $temp % 60;
			my @meantemp = split /\./, ($temp / $ticCount{$date1}{$enviro});
			# If the time divided by the number of tickets has a decimal
	                # value round up if that value is greater than
	                # 0.5.  This will give an even number of minutes to figure
	                # out the mean time with.
	               if ($meantemp[1]) {
	                        if ($meantemp[1] >= 5) {
	                                $meantemp[0]++;
	                        }
	                }
	                my @meantime  = split /\./, ($meantemp[0] / 60);
	                $endtime2[0]  = $meantime[0];
	                $endtime2[1]  = $meantemp[0] % 60;
			my $endTime   = sprintf '%d:%02d', @endTime[0,1];
			my $mean      = sprintf '%d:%02d', @endtime2[0,1];
			printf TIMESHEET "%18s%6s%8s%11s\n", $enviro, $ticCount{$date1}{$enviro}, $endTime, $mean;
		}
	}
}

close TIMESHEET;

exit;

