<?php
include("includes/site_info.php");
include('session_handler.inc');

$WUserID = $_SESSION['Sess_UserId'];

	switch ($_REQUEST["mode"]) {
	case "clients"; 
		clients($WUserID);
		break;
	case "group_clients"; 
		group_clients($WUserID);
		break;
	case "mclients"; 
		monthly_clients($WUserID);
		break;
	case "mgroup_clients"; 
		monthly_group_clients($WUserID);
		break;
	default; 
		echo "";
		break;
	}

function clients($WUserID) {
$member_query=mysql_query("SELECT DATE_FORMAT(mem_joindate, '%M %d, %Y') as mem_joindate FROM adverts LEFT JOIN members ON (adv_userid=mem_userid) WHERE adv_userid = '$WUserID' AND adv_approved='1'") or die(mysql_error());
$sql_array = mysql_fetch_object($member_query);

$referrer_query=mysql_query("SELECT B.mem_userid, B.mem_username, DATE_FORMAT(B.mem_joindate, '%M %d, %Y') as mem_joindate, B.mem_fullname FROM adverts AS A, members AS B, members AS C WHERE C.mem_userid = '$WUserID' AND A.adv_approved='1' And A.adv_userid=C.mem_userid And B.mem_referrer = C.mem_userid AND B.mem_joindate < date_sub(current_date, interval dayofmonth(current_date)-1 day)") or die(mysql_error());
$referrer_TOTAL = mysql_num_rows($referrer_query);
?>

<table width="100%" border="0" cellspacing="0" cellpadding="0" class="white_back_t_xb p5 m_tb_15">
		<tr class="grey_12 orange_row_back solid_line_separator">
			<td><div style="background-color:#0000FF; width:10px; height:10px; float:left; margin-top:3px; margin-right:5px;"><img src="images/spacer.gif" width="10" height="10" /></div> All Clients Accumulated Since <?=$sql_array->mem_joindate?> </td>
		</tr>
	<?php if($referrer_TOTAL>0) {?>
	<tr>
		<td><table width="100%" border="0" cellpadding="5" cellspacing="1" id="structureTable2">
				<tr>
					<th width="33%" align="left" nowrap="nowrap">My Client Development (<?=$referrer_TOTAL?>)</th>
					<th width="33%" align="center" nowrap="nowrap">Direct Group Client Development</th>
					<th width="33%" align="left"> Join Date  </th>
				</tr>
				<?php
						while ($sql = mysql_fetch_object($referrer_query) ) {
						$referrer_group_query=mysql_query("SELECT B.mem_userid, B.mem_username, DATE_FORMAT(B.mem_joindate, '%M %d, %Y') as mem_joindate FROM adverts AS A, members AS B, members AS C WHERE C.mem_userid = '$sql->mem_userid' AND A.adv_approved='1' And A.adv_userid=C.mem_userid And B.mem_referrer = C.mem_userid AND B.mem_joindate < date_sub(current_date, interval dayofmonth(current_date)-1 day)") or die(mysql_error());
						$referrer_group_TOTAL = mysql_num_rows($referrer_group_query);
						?>
				<tr>
					<td><?=ucwords($sql->mem_username." (".$sql->mem_fullname.")");?></td>
					<td align="center"><?=$referrer_group_TOTAL?></td>
					<td><?=$sql->mem_joindate?></td>
				</tr>
				<?php } ?>
			</table></td>
	</tr>
	<?php } else {?>
	<tr>
		<td><?php  print(NO_RECORDS_SECTION_NAME); ?>
			!</td>
	</tr>
	<?php }?>
</table>
<?php
}

function group_clients($WUserID) {
$member_query=mysql_query("SELECT DATE_FORMAT(mem_joindate, '%M %d, %Y') as mem_joindate FROM adverts LEFT JOIN members ON (adv_userid=mem_userid) WHERE adv_userid = '$WUserID' AND adv_approved='1'") or die(mysql_error());
$sql_array = mysql_fetch_object($member_query);

$referrer_query = mysql_query("SET @refer=0, @prev=null, @curr=null;");
$referrer_query=mysql_query("SELECT * ,
CASE WHEN @curr = REFER
THEN NULL
ELSE REFER
END AS REFERRER, @curr := REFER
FROM (

SELECT * , @refer :=
CASE WHEN @prev <> REFER
THEN @refer :=1
ELSE @refer := @refer +1
END AS Countt, @prev := REFER
FROM (

SELECT B.mem_userid, B.mem_username, DATE_FORMAT( B.mem_joindate, '%M %d, %Y' ) AS mem_joindate, B.mem_fullname, C.mem_username AS REFER
FROM adverts AS A, members AS B, members AS C, members AS D
WHERE D.mem_userid = '1'
AND A.adv_approved = '1'
AND A.adv_userid = D.mem_userid
AND B.mem_referrer = C.mem_userid
AND C.mem_referrer = D.mem_userid
AND B.mem_joindate < date_sub( current_date, INTERVAL dayofmonth( current_date ) -1
DAY )
ORDER BY C.mem_username ASC
)DATA
ORDER BY refer, Countt DESC
)DATA2") or die(mysql_error());
$referrer_TOTAL = mysql_num_rows($referrer_query);
?>

<table width="100%" border="0" cellspacing="0" cellpadding="0" class="white_back_t_xb p5 m_tb_15">
		<tr class="grey_12 orange_row_back solid_line_separator">
			<td><div style="background-color:#0000FF; width:10px; height:10px; float:left; margin-top:3px; margin-right:5px;"><img src="images/spacer.gif" width="10" height="10" /></div> All Direct Group Client Accumulated Since <?=$sql_array->mem_joindate?> </td>
		</tr>
	<?php if($referrer_TOTAL>0) {?>
	<tr>
		<td><table width="100%" border="0" cellpadding="5" cellspacing="1" id="structureTable2">
				<tr>
					<th width="33%" align="left" nowrap="nowrap">Direct Group Client Development (<?=$referrer_TOTAL?>)</th>
					<th width="33%" align="left" nowrap="nowrap">Associates</th>
					<th width="33%" align="left"> Join Date  </th>
				</tr>
				<?php	while ($sql = mysql_fetch_object($referrer_query) ) {?>
				<tr>
					<td><?php if ($sql->REFERRER<>NULL) echo ucwords($sql->REFERRER)." (".$sql->Countt.")"?></td>
					<td><?=ucwords($sql->mem_username." (".$sql->mem_fullname.")");?></td>
					<td><?=$sql->mem_joindate?></td>
				</tr>
				<?php } ?>
			</table></td>
	</tr>
	<?php } else {?>
	<tr>
		<td><?php  print(NO_RECORDS_SECTION_NAME); ?>
			!</td>
	</tr>
	<?php }?>
</table>
<?php
}

function monthly_clients($WUserID) {
$referrer_query=mysql_query("SELECT B.mem_userid, B.mem_username, DATE_FORMAT(B.mem_joindate, '%M %d, %Y') as mem_joindate, B.mem_fullname FROM adverts AS A, members AS B, members AS C WHERE C.mem_userid = '$WUserID' AND A.adv_approved='1' And A.adv_userid=C.mem_userid And B.mem_referrer = C.mem_userid AND B.mem_joindate >= date_sub(current_date, interval dayofmonth(current_date)-1 day) AND B.mem_joindate <= date_add(date_sub(current_date, interval dayofmonth(current_date) day), interval 1 month)") or die(mysql_error());
$referrer_TOTAL = mysql_num_rows($referrer_query);
?>

<table width="100%" border="0" cellspacing="0" cellpadding="0" class="white_back_t_xb p5 m_tb_15">
		<tr class="grey_12 orange_row_back solid_line_separator">
			<td><div style="background-color:#0000FF; width:10px; height:10px; float:left; margin-top:3px; margin-right:5px;"><img src="images/spacer.gif" width="10" height="10" /></div> All New Clients for <?=date('F, Y')?> </td>
		</tr>
	<?php if($referrer_TOTAL>0) {?>
	<tr>
		<td><table width="100%" border="0" cellpadding="5" cellspacing="1" id="structureTable2">
				<tr>
					<th width="33%" align="left" nowrap="nowrap">My Client Development (<?=$referrer_TOTAL?>)</th>
					<th width="33%" align="center" nowrap="nowrap">Direct Group Client Development</th>
					<th width="33%" align="left"> Join Date  </th>
				</tr>
				<?php
						while ($sql = mysql_fetch_object($referrer_query) ) {
						$referrer_group_query=mysql_query("SELECT B.mem_userid, B.mem_username, DATE_FORMAT(B.mem_joindate, '%M %d, %Y') as mem_joindate FROM adverts AS A, members AS B, members AS C WHERE C.mem_userid = '$sql->mem_userid' AND A.adv_approved='1' And A.adv_userid=C.mem_userid And B.mem_referrer = C.mem_userid") or die(mysql_error());
						$referrer_group_TOTAL = mysql_num_rows($referrer_group_query);
						?>
				<tr>
					<td><?=ucwords($sql->mem_username." (".$sql->mem_fullname.")");?></td>
					<td align="center"><?=$referrer_group_TOTAL?></td>
					<td><?=$sql->mem_joindate?></td>
				</tr>
				<?php } ?>
			</table></td>
	</tr>
	<?php } else {?>
	<tr>
		<td><?php  print(NO_RECORDS_SECTION_NAME); ?>
			!</td>
	</tr>
	<?php }?>
</table>
<?php
}

function monthly_group_clients($WUserID) {
$referrer_query = mysql_query("SET @refer=0, @prev=null, @curr=null;");
$referrer_query=mysql_query("SELECT * ,
CASE WHEN @curr = REFER
THEN NULL
ELSE REFER
END AS REFERRER, @curr := REFER
FROM (

SELECT * , @refer :=
CASE WHEN @prev <> REFER
THEN @refer :=1
ELSE @refer := @refer +1
END AS Countt, @prev := REFER
FROM (

SELECT B.mem_userid, B.mem_username, DATE_FORMAT( B.mem_joindate, '%M %d, %Y' ) AS mem_joindate, B.mem_fullname, C.mem_username AS REFER
FROM adverts AS A, members AS B, members AS C, members AS D
WHERE D.mem_userid = '1'
AND A.adv_approved = '1'
AND A.adv_userid = D.mem_userid
AND B.mem_referrer = C.mem_userid
AND C.mem_referrer = D.mem_userid
AND B.mem_joindate >= date_sub(current_date, interval dayofmonth(current_date)-1 day) 
AND B.mem_joindate <= date_add(date_sub(current_date, interval dayofmonth(current_date) day), interval 1 month)
ORDER BY C.mem_username ASC
)DATA
ORDER BY refer, Countt DESC
)DATA2") or die(mysql_error());
$referrer_TOTAL = mysql_num_rows($referrer_query);
?>

<table width="100%" border="0" cellspacing="0" cellpadding="0" class="white_back_t_xb p5 m_tb_15">
		<tr class="grey_12 orange_row_back solid_line_separator">
			<td><div style="background-color:#0000FF; width:10px; height:10px; float:left; margin-top:3px; margin-right:5px;"><img src="images/spacer.gif" width="10" height="10" /></div> All Direct Group New Clients for <?=date('F, Y')?> </td>
		</tr>
	<?php if($referrer_TOTAL>0) {?>
	<tr>
		<td><table width="100%" border="0" cellpadding="5" cellspacing="1" id="structureTable2">
				<tr>
					<th width="33%" align="left" nowrap="nowrap">Direct Group Client Development (<?=$referrer_TOTAL?>)</th>
					<th width="33%" align="left" nowrap="nowrap">Associates</th>
					<th width="33%" align="left"> Join Date  </th>
				</tr>
				<?php
						while ($sql = mysql_fetch_object($referrer_query) ) {
						?>
				<tr>
					<td><?php if ($sql->REFERRER<>NULL) echo ucwords($sql->REFERRER)." (".$sql->Countt.")"?></td>
					<td><?=ucwords($sql->mem_username." (".$sql->mem_fullname.")");?></td>
					<td><?=$sql->mem_joindate?></td>
				</tr>
				<?php } ?>
			</table></td>
	</tr>
	<?php } else {?>
	<tr>
		<td><?php  print(NO_RECORDS_SECTION_NAME); ?>
			!</td>
	</tr>
	<?php }?>
</table>
<?php
}


?>
