#!/usr/bin/perl
use XML::Simple;
use Data::Dumper;
# slurp in the xml file
$ref = XMLin('offline_links.planner', keyattr => {task => 'id', resource => 'name'});
# dump it to the screen for debugging only
#print Dumper($ref);
# read in the old costs and comments
init_old_costs_comments();
$cost_old = 0;
$comment_change = '';

$scientist_id = $ref->{resources}->{resource}->{Scientist}->{id};
$scientist_rate = $ref->{resources}->{resource}->{Scientist}->{'std-rate'};
$contributed_id = $ref->{resources}->{resource}->{Contributed}->{id};
@allocray = @{$ref->{allocations}->{allocation}};
# get scientist units out of allocation array all tasks
foreach $allocref (@allocray) {
    if ($allocref->{'resource-id'} == $scientist_id) {
	#print "task = $allocref->{'task-id'}, units = $allocref->{units}\n";
	$scientist_units[$allocref->{'task-id'}] = $allocref->{units};
    }
    $task_id = $allocref->{'task-id'};
    $units = $allocref->{units};
    $taskunits[$task_id] += $units;
}
print "<html><head></head><body>\n";
print "<table border>\n";
print "<tr><th>Task</th><th>Cost</th><th>Original Cost</th><th>Difference</th><th>Comment</th></tr>\n";
$cost_total = 0.0;
%taskhashtop = %{$ref->{tasks}->{task}};
foreach $key (keys %taskhashtop) {
    #print "key = $key name = $taskhashtop{$key}->{name}\n";
    $taskhash{$key} = $taskhashtop{$key};
    if (exists $taskhashtop{$key}->{task}) {
	#print "subtasks exist\n";
	%taskhashsub = %{$taskhashtop{$key}->{task}};
	foreach $keysub (keys %taskhashsub) {
	    #print "keysub = $keysub name = $taskhashsub{$keysub}->{name}\n";
	    $taskhash{$keysub} = $taskhashsub{$keysub};
	}
    }
}
# loop over all of the tasks, sorted by task id
foreach $key (sort numerically keys %taskhash) {
    #print "working on task $key = $taskhash{$key}->{name}\n";
    if ($scientist_units[$key]) {
	#print "$scientist_rate, $scientist_units[$key]\n";
	$duration = $taskhash{$key}->{work}*100.0/$taskunits[$key]/3600.0;
	#print "duration = $duration\n";
	$cost = $duration*$scientist_rate*$scientist_units[$key]/100;
    } else {
	$cost = 0.0;
    }
    $name = $taskhash{$key}->{name};
    @tmpray = split(/\[/, $name);
    @tmpray2 = split(/\]/, $tmpray[1]);
    $project_id = $tmpray2[0];
    get_old_cost_comment($project_id);
    if ($cost_old ne 'not found') {
	$difference = $cost - $cost_old;
	$cost_print = sprintf("%.2f", $cost);
	$cost_old_print = sprintf("%.2f", $cost_old);
	$difference_print = sprintf("%.2f", $difference);
	print "<tr><td>$name</td><td align=\"right\">$cost_print</td><td align=\"right\">$cost_old_print</td><td align=\"right\">$difference_print</td><td>$comment_change</td></tr>\n";
	$cost_total += $cost;
	$cost_old_total += $cost_old;
    }
}
$difference_total = $cost_total - $cost_old_total;
$cost_total_print = sprintf("%.2f", $cost_total);
$cost_old_total_print = sprintf("%.2f", $cost_old_total);
$difference_total_print = sprintf("%.2f", $difference_total);
print "<tr><th>Total</th><th align=\"right\">$cost_total_print</th><th align=\"right\">$cost_old_total_print</th><th align=\"right\">$difference_total_print</th></tr>\n";
print "</tbody></table>\n";
print "</body></html>\n";

make_effort_report();

exit;

sub numerically {
    $a <=> $b;
}

sub make_effort_report{
    foreach $allocref (@allocray) {
	$task_id = $allocref->{'task-id'};
	$units = $allocref->{units};
	$start = $taskhash{$task_id}->{start};
	$end = $taskhash{$task_id}->{end};
	#print "task_id = $task_id, units = $units, start = $start, end = $end \n";
	if ($allocref->{'resource-id'} == $scientist_id) {
	    #print "scientist\n";
	    $scientist_effort{$start . "_" . $task_id} = $units;
	    $scientist_effort{$end . "_" . $task_id} = -$units;
	} elsif ($allocref->{'resource-id'} == $contributed_id) {
	    #print "contributed\n";
	    $contributed_effort{$start . "_" . $task_id} = $units;
	    $contributed_effort{$end . "_" . $task_id} = -$units;
	} else {
	    die "error: allocation not scientist nor contributed";
	}
    }
    print "<h3>Scientist Effort</h3>\n";
    print "<table border>\n";
    print "<tr><th>Date</th><th>% FTE</th></tr>\n";
    $sum_scientist = 0;
    $first = 1;
    foreach $key (sort keys %scientist_effort) {
	$date = extract_date($key);
	$sum_scientist += $scientist_effort{$key};
	if ($date ne $date_last && !$first) {
	    print "<tr><td>$date_last</td><td>$sum_scientist_last</td></tr>\n";
	}
	$first = 0;
	$date_last = $date;
	$sum_scientist_last = $sum_scientist;
    }
    print "<tr><td>$date_last</td><td>$sum_scientist_last</td></tr>\n";
    print "</table>\n";
    print "<h3>Contributed Effort</h3>\n";
    print "<table border>\n";
    print "<tr><th>Date</th><th>% FTE</th></tr>\n";
    $sum_contributed = 0;
    $first = 1;
    foreach $key (sort keys %contributed_effort) {
	$date = extract_date($key);
	$sum_contributed += $contributed_effort{$key};
	if ($date ne $date_last && !$first) {
	    print "<tr><td>$date_last</td><td>$sum_contributed_last</td></tr>\n";
	}
	$first = 0;
	$date_last = $date;
	$sum_contributed_last = $sum_contributed;
    }
    print "<tr><td>$date_last</td><td>$sum_contributed_last</td></tr>\n";
    print "</table>\n";
}

sub extract_date {
    my ($date_string) = @_;
    $year = substr($date_string, 0, 4);
    $month = substr($date_string, 4, 2);
    $day = substr($date_string, 6, 2);
    $extracted = $year . '-' . $month . '-' . $day;
    return $extracted;
}

sub init_old_costs_comments {
    open(OLDCSV, "original_costs_comments.csv");
    while (<OLDCSV>) {
	#print;
	chomp;
	@column = split /,/;
	$cost_old{$column[0]} = $column[2];
	$comment_old_to_new{$column[0]} = remove_double_quotes($column[3]);
	#print "$_[1]\n";
    }
    close(OLDCSV);
}

sub remove_double_quotes {
    my ($string_in) = @_;
    @field = split(/"/, $string_in);
    return $field[1];
}

sub get_old_cost_comment {
    my ($id_in) = @_;
    $cost_old = 'not found';
    $comment_change = 'no comment';
    foreach $id (keys %cost_old) {
	if ($id eq $id_in) {
	    $cost_old = $cost_old{$id};
	    $comment_change = $comment_old_to_new{$id};
	}
    }
    return;
}
