#!/usr/bin/perl

require "config.idx";
print "Content-type: text/html\n\n";
$long = $ENV{'QUERY_STRING'};

if ($long) {
	@pairs=split(/&/,$long);
	foreach $item(@pairs) {
		($name,$content)=split (/=/,$item,2);
		$content=~tr/+/ /;
		$content=~ s/%(..)/pack("c",hex($1))/ge;
		if ($INPUT{$name}) { $INPUT{$name} = $INPUT{$name}.",".$content; }
		else { $INPUT{$name} = $content; }
	}
}
else {
	read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
	@pairs = split(/&/, $buffer);
	foreach $pair (@pairs) {
		($name, $value) = split(/=/, $pair);
		$value =~ tr/+/ /;
		$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
		if ($INPUT{$name}) { $INPUT{$name} = $INPUT{$name}.",".$value; }
		else { $INPUT{$name} = $value; }
	}
}

$Cook{agentid} = "600";

$print_header =qq~
				<tr>
				<td valign="top" bgcolor="navy"><b><font color="white" size="1">Task Type</font></b></td>
				<td valign="top" bgcolor="navy"><b><font color="white" size="1">Activity</font></b></td>
				<td valign="top" bgcolor="navy"><b><font color="white" size="1">Description</font></b></td>
				<td valign="top" bgcolor="navy"><b><font color="white" size="1">Due Date</font></b></td>
				<td valign="top" bgcolor="navy"><b><font color="white" size="1">Actions</font></b></td>
				</tr>
~;

use DBI;
my $dsn = "DBI:mysql:database=$d_name;host=localhost";
my $dbh = DBI->connect($dsn, $d_username, $d_pass, {RaiseError => 1}) || die $DBI::errstr;

my $agsth = $dbh->prepare("select firstname,lastname from agents where mlsid = '$Cook{agentid}' order by agentid ASC LIMIT 0,1");
$agsth->execute;
while (($agent_firstname,$agent_lastname) = $agsth->fetchrow_array()) {


my $agsth = $dbh->prepare("select DATE_FORMAT(now(), '%b %D, %Y at %h\:%i%p')");
$agsth->execute;
while (($datenow) = $agsth->fetchrow_array()) {
$todaystasks=qq~
<html>
<head>
<title>Task Report for $agent_firstname $agent_lastname - Connect.EagleSold.com</title>
<link rel="stylesheet" TYPE="text/css" href="includes/style.css">
</head>
<body bgcolor="white" leftMargin="0" topMargin="0" marginwidth="0" marginheight="0">
<center>
<table border="0" cellpadding="1" cellspacing="1" width="825">
<tr>
<td valign="top" colspan="5">
<a href="tasks.idx?action=report"><img src="http://connect.eaglesold.com/images/task_manager.gif" height="70" width="825" border="0" alt="Task Report for $agent_firstname $agent_lastname as of $datenow"></a><br>
<center><font size="5" face="Arial Narrow"><b>Task Report for $agent_firstname $agent_lastname as of $datenow</b><br></font></center>
</td>
</tr>
~;
}
}


###  FIND TASKS DUE TODAY OR OVERDUE FOR TRANSACTIONS!
$todaystasks.=qq~
<tr>
<td colspan="5"><font size="4" face="Arial Black" color="#6666ff"><i>Due or Overdue Transaction Tasks!</i></font><br></td>
</tr>
~;
### 1. Check The List Of Properties ###
my $ssth = $dbh->prepare("select id,addressnumber,addressdirection,addressstreet,city,zip,mlsnumber,status from trans_board where agent = '$Cook{agentid}' order by id DESC");
$ssth->execute;
while (($transid,$addressnumber,$addressdirection,$addresstreet,$city,$zip,$mlsnumber,$status) = $ssth->fetchrow_array()) {

my $this_address_bar = qq~
                <tr>
                <td colspan="5" bgcolor="white">
                <font size="3" color="navy"><b><u>$status | $addressnumber $addressdirection $addresstreet, $city IN $zip (MLS Number $mlsnumber)</u></b></font><br>
                </td>
                </tr>
                ~;
                
my $this_address_bar_printed = "no";

    ### 2. Find The Template ###
	my $xasth = $dbh->prepare("SELECT template_id FROM trans_boardtask_triggers WHERE idlink = '$transid' GROUP BY template_id");
	$xasth->execute;
	while (($g_temp_id) = $xasth->fetchrow_array()) {

        my $this_template_bar;

	     #IF TEMPLATE IS FOUND DISPLAY PROPERTY ADDRESS
			### 4. Convert The Template ID To Name ###
			my $basth = $dbh->prepare("select template_name from trans_boardtask_templates where id = '$g_temp_id'");
			$basth->execute;
			while (($template_id_name) = $basth->fetchrow_array()) {
			$this_template_bar = qq~
				<tr>
					<td colspan="5" bgcolor="green">
						<font color="white" size="2"><i><b>Actions to Perform for $template_id_name</b></i></font></b></font><br>
					</td>
				</tr>
				$print_header
			~;
			}

		### 3. Find Overdue Tasks ###
		my $casth = $dbh->prepare("select id,idlink,template_id,task_type,title,activity,agent_task_id,DATE_FORMAT(date_trigger, '%b %D, %Y'),notes from trans_boardtask_triggers where task_type = 'Transaction' and idlink = '$transid' AND template_id = '$g_temp_id' AND date_trigger <= now() AND date_completed = '0000-00-00 00:00:00' order by date_trigger ASC");
		$casth->execute;

		$trcolor = "1";
		$trcolor2 = "2";
		
		my $task_bar_data;
		while (($task_triggers_id,$task_triggers_idlink,$task_triggers_template_id,$task_triggers_task_type,$task_triggers_title,$task_triggers_activity,$task_triggers_agent_task_id,$task_triggers_date_trigger,$task_triggers_notes) = $casth->fetchrow_array()) {

				unless ($task_triggers_notes eq "") {$display_note = "<br>(Note: $task_triggers_notes)";}

				if ($task_triggers_activity eq "Letter") {
				$task_triggers_activityl = "<a target=\"_blank\" href=\"http://connect.eaglesold.com/transactions.idx?action=overview_tasks&do=ActionLetter&type=$task_triggers_task_type&agent_task_id=$task_triggers_agent_task_id&idlink=$task_triggers_idlink\">[ Print Letter ]</a>";
				}
				elsif ($task_triggers_activity eq "E-Mail") {
				$task_triggers_activityl = "<a target=\"_blank\" href=\"http://connect.eaglesold.com/transactions.idx?action=overview_tasks&do=ActionEMail&type=$task_triggers_task_type&agent_task_id=$task_triggers_agent_task_id&idlink=$task_triggers_idlink\">[ Send E-Mail ]</a>";
				}
				else {
				$task_triggers_activityl = "";
				}

					if ($trcolor eq $trcolor2) {
					$trcolor = "1";
					$setcolor = "#ccccff";
					}
					else {
					$setcolor = "#FFFFFF";
					$trcolor++;
					}

				#$display_task_list .=qq~
				$task_bar_data .= qq~
				<tr>
				<td valign="middle" bgcolor="$setcolor" align="center"><b>$task_triggers_task_type</b></td>
				<td valign="middle" bgcolor="$setcolor" align="center"><b>$task_triggers_activity</b></td>
				<td valign="middle" bgcolor="$setcolor" nowrap><font size="2"><b><img src="http://connect.eaglesold.com/images/top_icon.gif"> <a class="tasktitle" target="_blank" href="http://connect.eaglesold.com/transactions.idx?action=overview&transid=$transid&agent=$Cook{agentid}">$task_triggers_title</a></font>$display_note</b></td>
				<td valign="middle" bgcolor="$setcolor" align="center" nowrap><b>$task_triggers_date_trigger</b></td>
				<td valign="middle" bgcolor="$setcolor" nowrap><font size="2" color="navy"><b><u>Actions:</u></b></font> <a target="_blank" class="action_plan" href="http://connect.eaglesold.com/transactions.idx?action=overview_tasks&id=$task_triggers_id&idlink=$INPUT{idlink}&do=MarkDone"><img src="http://connect.eaglesold.com/images/trans_sys/icons/edit-page-green.gif" border="0" alt="Mark as Completed"></a>  <a target="_blank" href="http://connect.eaglesold.com/transactions.idx?action=overview_tasks&id=$task_triggers_id&idlink=$INPUT{idlink}&do=Notes"><img src="http://connect.eaglesold.com/images/trans_sys/icons/add-comment-green.gif" border="0" alt="Enter Notes"></a><br>$task_triggers_activityl</td>
				</tr>
				~;
			$found = "yes";
			$display_note = "";
$todaystasks.="$address_displayed";
			$address_displayed = "";
		}

        if ($task_bar_data ne "") {
            unless ($this_address_bar_printed eq "yes") {
                $todaystasks.=$this_address_bar;
                $this_address_bar_printed = "yes";
            }
            $todaystasks.=$this_template_bar;
            $todaystasks.=$task_bar_data;
        }
	}
}
$found = "no";

###  FIND TASKS DUE TODAY OR OVERDUE FOR CONTACTS!
$todaystasks.=qq~
<tr>
<td colspan="5">&nbsp;</td>
</tr>
<tr>
<td colspan="5"><font size="4" face="Arial Black" color="#33cc33"><i>Due or Overdue Contact Tasks!</i></font><br></td>
</tr>
$print_header
~;
my $ssth = $dbh->prepare("select id,agent,type,firstname,lastname,address,spouse,city,state,zip,phone1type,phone1number,phone2type,phone2number,phone3type,phone3number,faxnumber,email,emailtwo,notes from trans_boardcontact where agent = '$Cook{agentid}' order by id DESC");
$ssth->execute;
while (my($id,$agent,$type,$firstname,$lastname,$address,$spouse,$city,$state,$zip,$phone1type,$phone1number,$phone2type,$phone2number,$phone3type,$phone3number,$faxnumber,$email,$emailtwo,$notes) = $ssth->fetchrow_array()) {

		my $basth = $dbh->prepare("select id,idlink,template_id,task_type,title,activity,agent_task_id,DATE_FORMAT(date_trigger, '%b %D, %Y') from trans_boardtask_triggers where task_type = 'Contact' and idlink = '$id' AND date_trigger <= now() AND date_completed = '0000-00-00 00:00:00' order by date_trigger ASC");
		$basth->execute;

$trcolor = "1";
$trcolor2 = "2";

		while (my($task_triggers_id,$task_triggers_idlink,$task_triggers_template_id,$task_triggers_task_type,$task_triggers_title,$task_triggers_activity,$task_triggers_agent_task_id,$task_triggers_date_trigger) = $basth->fetchrow_array()) {
		$contact_found = "yes";

			my $basth = $dbh->prepare("select template_name from trans_boardtask_templates where id = '$id'");
			$basth->execute;
			while (($template_id_name) = $basth->fetchrow_array()) {
			$print_tempname = "$template_id_name";
			}

				if ($task_triggers_activity eq "Letter") {
				$task_triggers_activityl = "<a target=\"_blank\" href=\"http://connect.eaglesold.com/transactions.idx?action=overview_tasks&do=ActionLetter&type=$task_triggers_task_type&agent_task_id=$task_triggers_agent_task_id&idlink=$task_triggers_idlink\">[ Print Letter ]</a>";
				}
				elsif ($task_triggers_activity eq "E-Mail") {
				$task_triggers_activityl = "<a target=\"_blank\" href=\"http://connect.eaglesold.com/transactions.idx?action=overview_tasks&do=ActionEMail&type=$task_triggers_task_type&agent_task_id=$task_triggers_agent_task_id&idlink=$task_triggers_idlink\">[ Send E-Mail ]</a>";
				}
				else {
				$task_triggers_activityl = "";
				}

			if ($trcolor eq $trcolor2) {
			$trcolor = "1";
			$setcolor = "#ccffcc";
			}
			else {
			$setcolor = "#FFFFFF";
			$trcolor++;
			}

				$display_ctask_list .=qq~
				<tr>
				<td valign="middle" bgcolor="$setcolor" align="center"><b>$task_triggers_task_type</b></td>
				<td valign="middle" bgcolor="$setcolor" align="center"><b>$task_triggers_activity</b></td>
				<td valign="middle" bgcolor="$setcolor" nowrap><font size="2"><b><img src="http://connect.eaglesold.com/images/top_icon.gif"> <a class="tasktitle" target="_blank" href="contacts.idx?action=overview_contact&agent=$Cook{agentid}&contactid=$id">$task_triggers_title</a></font>$display_note</b></td>
				<td valign="middle" bgcolor="$setcolor" align="center" nowrap><b>$task_triggers_date_trigger</b></td>
				<td valign="middle" bgcolor="$setcolor" nowrap><font size="2" color="navy"><b><u>Actions:</u></b></font> <a target="_blank" class="action_plan" href="http://connect.eaglesold.com/transactions.idx?action=overview_tasks&id=$task_triggers_id&idlink=$INPUT{idlink}&do=MarkDone"><img src="http://connect.eaglesold.com/images/trans_sys/icons/edit-page-green.gif" border="0" alt="Mark as Completed"></a>  <a target="_blank" href="http://connect.eaglesold.com/transactions.idx?action=overview_tasks&id=$task_triggers_id&idlink=$INPUT{idlink}&do=Notes"><img src="http://connect.eaglesold.com/images/trans_sys/icons/add-comment-green.gif" border="0" alt="Enter Notes"></a><br>$task_triggers_activityl</td>
				</tr>
			~;
			$found = "yes";
		}

	if ($found eq "yes") {

		if ($spouse ne "") {
		$spouse = " & $spouse ";
		}
		else {
		$spouse = "";
		}

	$todaystasks.=qq~
	<tr>
	<td colspan="5" bgcolor="#99ff99">
	<font size="2"><b>Contact $id $firstname$spouse $lastname<br><font color="green"><i>Running: $print_tempname Template</i></font></b></font><br>
	</td>
	</tr>
	$display_ctask_list
	~;
	}

$display_ctask_list = "";
$found = "no";
$contact_found = "no";
}
unless ($contact_found eq "yes") {
$todaystasks.=qq~
<tr>
<td colspan="5" align="center"><font size="2" color="red"><b>You Must Create/Assign An Action Plan To Your Contacts!</b></font></td>
</tr>
~;
}



###  FIND TASKS DUE TODAY OR OVERDUE FOR PROSPECTS!
$todaystasks.=qq~
<tr>
<td colspan="5">&nbsp;</td>
</tr>
<tr>
<td colspan="5"><font size="4" face="Arial Black" color="#cccc66"><i>Due or Overdue Prospect Tasks!</i></font><br></td>
</tr>
$print_header
~;
my $ssth = $dbh->prepare("select id,agent,type,firstname,lastname,address,spouse,city,state,zip,phone1type,phone1number,phone2type,phone2number,phone3type,phone3number,faxnumber,email,emailtwo,notes from trans_boardprospect where agent = '$Cook{agentid}' order by id DESC");
$ssth->execute;
while (($id,$agent,$type,$firstname,$lastname,$address,$spouse,$city,$state,$zip,$phone1type,$phone1number,$phone2type,$phone2number,$phone3type,$phone3number,$faxnumber,$email,$emailtwo,$notes) = $ssth->fetchrow_array()) {

		my $basth = $dbh->prepare("select id,idlink,template_id,task_type,title,activity,agent_task_id,DATE_FORMAT(date_trigger, '%b %D, %Y') from trans_boardtask_triggers where task_type = 'Prospect' AND idlink = '$id' AND date_trigger <= now() AND date_completed = '0000-00-00 00:00:00' order by date_trigger ASC");
		$basth->execute;

$trcolor = "1";
$trcolor2 = "2";

		while (($task_triggers_id,$task_triggers_idlink,$task_triggers_template_id,$task_triggers_task_type,$task_triggers_title,$task_triggers_activity,$task_triggers_agent_task_id,$task_triggers_date_trigger) = $basth->fetchrow_array()) {
		$prospect_found = "yes";
		my $basth = $dbh->prepare("select template_name from trans_boardtask_templates where id = '$task_triggers_template_id'");
		$basth->execute;
		while (($template_id_name) = $basth->fetchrow_array()) {
		$print_tempname = "$template_id_name";
		}

				if ($task_triggers_activity eq "Letter") {
				$task_triggers_activityl = "<a target=\"_blank\" href=\"http://connect.eaglesold.com/transactions.idx?action=overview_tasks&do=ActionLetter&type=$task_triggers_task_type&agent_task_id=$task_triggers_agent_task_id&idlink=$task_triggers_idlink\">[ Print Letter ]</a>";
				}
				elsif ($task_triggers_activity eq "E-Mail") {
				$task_triggers_activityl = "<a target=\"_blank\" href=\"http://connect.eaglesold.com/transactions.idx?action=overview_tasks&do=ActionEMail&type=$task_triggers_task_type&agent_task_id=$task_triggers_agent_task_id&idlink=$task_triggers_idlink\">[ Send E-Mail ]</a>";
				}
				else {
				$task_triggers_activityl = "";
				}

			if ($trcolor eq $trcolor2) {
			$trcolor = "1";
			$setcolor = "#ffffcc";
			}
			else {
			$setcolor = "#FFFFFF";
			$trcolor++;
			}
				$display_ptask_list .=qq~
				<tr>
				<td valign="middle" bgcolor="$setcolor" align="center"><b>$task_triggers_task_type</b></td>
				<td valign="middle" bgcolor="$setcolor" align="center"><b>$task_triggers_activity</b></td>
				<td valign="middle" bgcolor="$setcolor" nowrap><font size="2"><b><img src="http://connect.eaglesold.com/images/top_icon.gif"> <a class="tasktitle" target="_blank" href="prospects.idx?action=overview_prospect&agent=$Cook{agentid}&prospectid=$id">$task_triggers_title</a></font>$display_note</b></td>
				<td valign="middle" bgcolor="$setcolor" align="center" nowrap><b>$task_triggers_date_trigger</b></td>
				<td valign="middle" bgcolor="$setcolor" nowrap><font size="2" color="navy"><b><u>Actions:</u></b></font> <a target="_blank" class="action_plan" href="http://connect.eaglesold.com/transactions.idx?action=overview_tasks&id=$task_triggers_id&idlink=$INPUT{idlink}&do=MarkDone"><img src="http://connect.eaglesold.com/images/trans_sys/icons/edit-page-green.gif" border="0" alt="Mark as Completed"></a>  <a target="_blank" href="http://connect.eaglesold.com/transactions.idx?action=overview_tasks&id=$task_triggers_id&idlink=$INPUT{idlink}&do=Notes"><img src="http://connect.eaglesold.com/images/trans_sys/icons/add-comment-green.gif" border="0" alt="Enter Notes"></a><br>$task_triggers_activityl</td>
				</tr>
			~;
			$found = "yes";
		}

	if ($found eq "yes") {

		if ($spouse ne "") {
		$spouse = " & $spouse ";
		}
		else {
		$spouse = "";
		}

	$todaystasks.=qq~
	<tr>
	<td colspan="5" bgcolor="#ffff66">
	<font size="2"><b>Prospect $firstname$spouse $lastname<br><font color="green"><i>Running: $print_tempname Template</i></font></b></font><br>
	</td>
	</tr>
	$display_ptask_list
	~;
	}

$display_ptask_list = "";
$found = "no";
$prospect_found = "no";
}
unless ($prospect_found eq "yes") {
$todaystasks.=qq~
<tr>
<td colspan="5" align="center"><font size="2" color="red"><b>You Must Create/Assign An Action Plan To Your Prospects!</b></font></td>
</tr>
~;
}
$todaystasks.=qq~
</table>
</center>
~;

my $agsth = $dbh->prepare("select DATE_FORMAT(now(), '%b %D, %Y')");
$agsth->execute;
while (($datenow2) = $agsth->fetchrow_array()) {

					open(MAIL,"|/usr/sbin/sendmail -t");
					print MAIL "To: Info\@ORRHomes.com\n";
					print MAIL "From: Tasks\@ORRHomes.com\n";
					print MAIL "Subject: ORRTEAM Task Report For $datenow2\n";
					print MAIL "Content-type: text/html\n\n";
					print MAIL qq~$todaystasks~;
					close(MAIL);
}

print "E-Mailed Updated Task Report";
exit 0;