<?PHP
include "../inc/var.php";
include "../inc/connect.php";
include $server_inc."/client_functions.php";
include $server_inc."/affiliate_functions.php";
include $server_inc."/login_functions.php";

include $server_admin_inc."/header.php";

echo("
	<table width='725' cellpadding='0' cellspacing='0' border='0'>
		<tr>
			<td width='1%' align='left' valign='top'><img src='".$http_images."/space.gif' width='10' height='1'></td>
			<td width='99%' align='left' valign='top'>
			<table width='725' cellpadding='2' cellspacing='1' border='0'>
				<tr>
					<td colspan='2' align='left' valign='middle' bgcolor='#40a3de'><b>&nbsp;<font color='#FFFFFF'>Account Details</font></b></td>
					<td colspan='2' align='left' valign='middle' bgcolor='#40a3de'><b>&nbsp;<font color='#FFFFFF'>General Statistics</font></b></td>
				</tr>
			");
		$row=mysql_query("select user.first_name, user.last_name, user.username, user.email, affiliate_user.afuid from user, affiliate_user where affiliate_user.uid=user.uid order by affiliate_user.afuid asc");
		while ($rs=mysql_fetch_row($row))
			{
			$first_name=stripslashes(trim($rs[0]));
			$last_name=stripslashes(trim($rs[1]));
			$username=stripslashes(trim($rs[2]));
			$email=stripslashes(trim($rs[3]));
			$afuid=stripslashes(trim($rs[4]));

			// find out how many affiliate has refered
			$ct=mysql_fetch_row(mysql_query("select count(*) from affiliate_details where afuid='".addslashes(trim($afuid))."'"));

			// figure payouts to date
			$rs0=mysql_fetch_row(mysql_query("select SUM(owed_to_client) from affiliate_history where afuid='".addslashes(trim($afuid))."'"));

			// calc balance forward
			$rs1=mysql_fetch_row(mysql_query("select SUM(total_due_to_client) from affiliate_details where afuid='".addslashes(trim($afuid))."' and status='1'"));

			// find last paid date
			# $rs2=mysql_fetch_row(mysql_query("select UNIX_TIMESTAMP(period_of) from affiliate_history where afuid='".addslashes(trim($afuid))."' and status='1' order by period_of desc limit 0, 1"));
			$rs2=mysql_fetch_row(mysql_query("select period_of from affiliate_history where afuid='".addslashes(trim($afuid))."' and status='1' order by period_of desc limit 0, 1"));

			if (strlen(trim($rs2[0]))==0)
				{
				$last_paid="Never";
				}
			else
				{
				$last_paid=date("m/d/Y h:i:s a", $rs2[0]);
				}


			echo("
					<tr>
						<td colspan='4' align='left' valign='middle' bgcolor='#asdaf9'><img src='images/space.gif' width='1' height='12'></td>
					</tr>
					<tr>
						<td width='15%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;Username:</td>
						<td width='35%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;".$username."</td>
						<td width='20%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;Last Paid:</td>
						<td width='30%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;".$last_paid."</td>
					</tr>
					<tr>
						<td width='15%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;Client Name:</td>
						<td width='35%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;".$first_name." ".$last_name."</td>
						<td width='20%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;Balance Forward:</td>
						<td width='30%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;".$currency."".sprintf("%01.2f", $rs1[0])."".$currency_type."</td>
					</tr>
					<tr>
						<td width='15%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;Email:</td>
						<td width='35%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;<a href='mailto:$email'>".$email."</a></td>
						<td width='20%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;Payouts to Date:</td>
						<td width='30%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;".$currency."".sprintf("%01.2f", $rs0[0])."".$currency_type."</td>
					</tr>
					<tr>
						<td width='15%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;Total Referrals:</td>
						<td width='35%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;".$ct[0]." [<a href='".$http_admin."/affiliate_details_view.php?sid=".trim($sid)."&afuid=".trim($afuid)."'>view details</a>]</td>
						<td width='20%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;<b>Grand Total Sales</b></td>
						<td width='30%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;<b>".$currency."".sprintf("%01.2f", ($rs0[0]+$rs1[0]))."".$currency_type."</b></td>
					</tr>
				");
			}
echo("
			</table>
			</td>
		</tr>
		<tr>
			<td align='left' valign='top'><img src='".$http_images."/space.gif' width='1' height='15'></td>
		</tr>
	</table>
	");

// End Main Process
mysql_close($dblink);
include $server_admin_inc."/footer.php";
?>