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

if (isset($payout_amount))
	{
	// read in last ran and afhid if applicable
	list($last_ran, $afhid)=split("[|]", read_affiliate_period_log($below_public, $afuid));

	// get the period_of
	$rsog=mysql_fetch_row(mysql_query("select period_of from affiliate_history where afhid='".addslashes(trim($afhid))."'"));

	// update the history record
	mysql_query("update affiliate_history set period_of='".addslashes(trim($rsog[0]))."', status='1' where afhid='".addslashes(trim($afhid))."'");

	// get the ogcreate
	$rowx=mysql_query("select afdid, ogcreate from affiliate_details where afuid='".addslashes(trim($afuid))."'");
	while ($rx=mysql_fetch_row($rowx))
		{
		// update the history details table for affiliate
		mysql_query("update affiliate_details set ogcreate='".addslashes(trim($rx[1]))."', status='99' where afdid='".addslashes(trim($rx[0]))."'");
		}

	// notify of payment
	notify_of_payout($uid, $email_admin, $payout_amount, $currency, $currency_type);
	}

// update the history table
update_affiliate_history_table($below_public);

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'>Accounting Details</font></b></td>
					<td colspan='2' align='left' valign='middle' bgcolor='#40a3de'><b>&nbsp;<font color='#FFFFFF'>Balance Required for Payment: ".$currency."".$balance_required_for_payment."".$currency_type."</font></b></td>
				</tr>
	");
$row=mysql_query("select uid, ssn_tax_id, afuid from affiliate_user order by afuid asc");
while ($rs=mysql_fetch_row($row))
	{
	// calc balance forward
	$rs1=mysql_fetch_row(mysql_query("select SUM(total_due_to_client) from affiliate_details where afuid='".addslashes(trim($rs[2]))."' and status='1'"));

	// build query
	$query="select ";
	$query.="first_name, ";			// 0
	$query.="last_name, ";			// 1
	$query.="email, ";				// 2
	$query.="street_address_1, ";	// 3
	$query.="street_address_2, ";	// 4
	$query.="phone, ";				// 5
	$query.="fax, ";				// 6
	$query.="city, ";				// 7
	$query.="state, ";				// 8
	$query.="zip_code, ";			// 9
	$query.="country ";				// 10
	$query.="from user ";
	$query.="where uid='".addslashes(trim($rs[0]))."'";

	// execute query
	$rs0=mysql_fetch_row(mysql_query($query));

	// define vars
	$first_name=stripslashes(trim($rs0[0]));
	$last_name=stripslashes(trim($rs0[1]));
	$email=stripslashes(trim($rs0[2]));
	$street_address_1=stripslashes(trim($rs0[3]));
	$street_address_2=stripslashes(trim($rs0[4]));
	$phone=stripslashes(trim($rs0[5]));
	$fax=stripslashes(trim($rs0[6]));
	$city=stripslashes(trim($rs0[7]));
	$state=stripslashes(trim($rs0[8]));
	$zip_code=stripslashes(trim($rs0[9]));
	$country=stripslashes(trim($rs0[10]));
	$ssn=stripslashes(trim($rs[1]));
	$payout=stripslashes(trim($rs1[0]));
	$afuid=stripslashes(trim($rs[2]));
	$uid=$rs[0];

	$rs2=mysql_fetch_row(mysql_query("select payable_to from affiliate_user where afuid='".addslashes(trim($afuid))."'"));
	$payable_to=stripslashes(trim($rs2[0]));
	
	if ($payout<$balance_required_for_payment)
		{
		@$under.=("
			<tr>
				<td colspan='4' align='left' valign='middle' bgcolor='#asdaf9'><img src='".$http_images."/space.gif' width='1' height='12'></td>
			</tr>
			<tr>
				<td colspan='1' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;Name:</td>
				<td colspan='3' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;<b>".$first_name." ".$last_name."</b>&nbsp;&nbsp;&nbsp;[Affiliate ID: ".$rs[2]."]</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;".$email."</td>
				<td width='20%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;Street Address:</td>
				<td width='30%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;".$street_address_1."".((strlen(trim($street_address_2))!=0)?"<br>".$street_address_2."":"")."</td>
			</tr>
			<tr>
				<td width='15%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;Phone / Fax:</td>
				<td width='35%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;".$phone."".((strlen(trim($fax))!=0)?" / ".$fax."":"")."</td>
				<td width='20%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;Payable to:</td>
				<td width='30%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;".$payable_to."</td>
			</tr>
			<tr>
				<td width='15%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;City / State:</td>
				<td width='35%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;".$city.", ".$state."</td>
				<td width='20%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;Tax ID / SSN:</td>
				<td width='30%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;".$ssn."</td>
			</tr>
			<tr>
				<td width='15%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;Zip / Country:</td>
				<td width='35%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;".$zip." ".$country."</td>
				<td width='20%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;<b>Balance&nbsp;&nbsp;&nbsp;<font color='#990000'>".$currency."".sprintf("%01.2f", $payout)."".$currency_type."</font></b></td>
				<td width='30%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;Under Minimum Payout</td>
			</tr>
			");
		}
	else
		{
		@$over.=("
			<tr>
				<td colspan='4' align='left' valign='middle' bgcolor='#asdaf9'><img src='".$http_images."/space.gif' width='1' height='12'></td>
			</tr>
			<tr>
				<td colspan='1' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;Name:</td>
				<td colspan='3' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;<b>".$first_name." ".$last_name."</b>&nbsp;&nbsp;&nbsp;[Affiliate ID: ".$rs[2]."]</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;".$email."</td>
				<td width='20%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;Street Address:</td>
				<td width='30%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;".$street_address_1."".((strlen(trim($street_address_2))!=0)?"<br>".$street_address_2."":"")."</td>
			</tr>
			<tr>
				<td width='15%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;Phone / Fax:</td>
				<td width='35%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;".$phone."".((strlen(trim($fax))!=0)?" / ".$fax."":"")."</td>
				<td width='20%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;Payable to:</td>
				<td width='30%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;".$first_name." ".$last_name."</td>
			</tr>
			<tr>
				<td width='15%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;City / State:</td>
				<td width='35%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;".$city.", ".$state."</td>
				<td width='20%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;Tax ID / SSN:</td>
				<td width='30%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;".$ssn."</td>
			</tr>
			<tr>
				<td width='15%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;Zip / Country:</td>
				<td width='35%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;".$zip." ".$country."</td>
				<td width='20%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;<b>Balance&nbsp;&nbsp;&nbsp;<font color='#990000'>".$currency."".sprintf("%01.2f", $payout)."".$currency_type."</font></b></td>
				<td width='30%' align='left' valign='middle' bgcolor='#dbedf9'>&nbsp;<a href='".$http_admin."/affiliate_accounting.php?sid=".trim($sid)."&afuid=".trim($afuid)."&uid=".trim($uid)."&payout_amount=".sprintf("%01.2f", $payout)."'><b>Pay This Affiliate</b></a></td>
			</tr>
			");
		}
	}
echo("
		<tr>
			<td colspan='4' align='left' valign='middle'><img src='".$http_images."/space.gif' width='1' height='22'><img src='".$http_images."/menu_arrow.gif'><b>Over Minimum Payout:</b></td>
		</tr>
	");
if (strlen(trim($over))!=0)
	{
	echo $over."\n";
	}
else
	{
	echo("
		<tr>
			<td colspan='4' align='left' valign='middle'><img src='".$http_images."/space.gif' width='15' height='9'><img src='".$http_images."/drop_arrow.gif' width='15' height='9'>No Records Found.</td>
		</tr>
		");
	}
echo("
		<tr>
			<td colspan='4' align='left' valign='middle'><img src='".$http_images."/space.gif' width='1' height='22'><img src='".$http_images."/menu_arrow.gif'><b>Under Minimum Payout:</b></td>
		</tr>
	");
if (strlen(trim($under))!=0)
	{
	echo $under."\n";
	}
else
	{
	echo("
		<tr>
			<td colspan='4' align='left' valign='middle'><img src='".$http_images."/space.gif' width='15' height='9'><img src='".$http_images."/drop_arrow.gif' width='15' height='9'>No Records Found.</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";
?>