<?php

	print "<table border='1' width='50%' align='center'>";

	print "<tr>
				<td>Sr No</td>
				<td>Member Name</td>
				<td>Member ID</td>
				<td>Direct Volume</td>
				<td>Left Volume</td>
				<td>Right Volume</td>
				<td>Total Leg Volume</td>
				<td>Status</td>
		  </tr>";

	$db = new PDO('mysql:host=localhost;dbname=mybooster;', 'root', 'root');
//	$db = new PDO('mysql:host=localhost;dbname=mybooster;', 'root', '');
	$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

	$data='';
	$i=1;


	$data = getMemberName($db);

	foreach($data as $row)
	{
		$directVolume = getDirectPayout($row['refer_id'], $db);
		$treeVolume   = getChildVolume($row['refer_id'], $db);

		echo "<tr>";
		echo "<td>" . $i++  ."</td>";
		echo "<td>" . $row['name']  ."</td>";
		echo "<td>" . $row['refer_id']  ."</td>";
		echo "<td><a href='Bangkok-DirectDownline.php?refer_id=". $row['refer_id'] . "' target='_blank'>" . $directVolume ."</a></td>";

		$totalVolume=0;
		
		if(count($treeVolume)==0)
		{
			
			echo "<td>0</td>";
			echo "<td>0</td>";
		}

		else if (count($treeVolume) == 1)
		{
			if($treeVolume[0]['node']='l')
			{
				echo "<td>" . $treeVolume[0]['pv']   ."</td>";
				echo "<td>0</td>";
				$totalVolume += $treeVolume[0]['pv'];

			}

			else
			{
				echo "<td>0</td>";
				echo "<td>" . $treeVolume[1]['pv']   ."</td>";
				$totalVolume += $treeVolume[1]['pv'];

			}

		}


		else
		{
			echo "<td>" . $treeVolume[0]['pv']   ."</td>";
			echo "<td>" . $treeVolume[1]['pv']   ."</td>";
			$totalVolume += $treeVolume[0]['pv'] + $treeVolume[1]['pv'];
		}

	
		echo "<td><a href='Bangkok-Downline.php?refer_id=". $row['refer_id'] . "' target='_blank'>" . $totalVolume   ."</a></td>";

		$qualified = "NO";

		if(($directVolume >= 300000) OR ($totalVolume) >= 1500000)
			$qualified = "YES";

		else
			$qualified = "NO";

		echo "<td>" . $qualified . "</td>";

		echo "</tr>";

		$totalVolume = 0;
	
	}


	function getDirectPayout($referID, $db)
	{
		try{
			
			$sql = "SELECT sum(purchase_pv) as pv from my_direct_payout where refer_id='$referID' AND (payout_start_date between '2019-08-15 00:00:00' AND '2019-09-13 23:59:59')";
			$stmt= $db->prepare($sql);
			$stmt->execute();
    		$result = $stmt->fetch(PDO::FETCH_ASSOC);
			return $result['pv'];
	
		}


		catch(Exception $e)
		{
			$e->getMessage();
			return false;

		}

	}


	function getChildVolume($referID, $db)
	{
		try{
			$data = array();
			
			$sql = "SELECT sum(pv) as pv, node FROM my_payout_child where sponsored_id='$referID' AND (date_time between '2019-08-15 00:00:00' AND '2019-09-13 23:59:59') And power_leg=0 GROUP BY node";

			$stmt= $db->prepare($sql);
			$stmt->execute();
			$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

			return $result;
	
		}


		catch(Exception $e)
		{
			$e->getMessage();
			return false;

		}

	}

	function getMemberName($db)
	{
		try{

			//$sql = "SELECT concat(name, ' ', last_name) as name from my_registration where refer_id=$refer_id";
			$sql = "select concat(name, ' ' , last_name) as name, refer_id from my_registration where refer_id IN (select distinct(refer_id) from my_payout_main where incentive>0)"; 
			$stmt= $db->prepare($sql);
			$stmt->execute();
    		$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
			return $result;
	
		}


		catch(Exception $e)
		{
			echo $e->getMessage();
			return false;

		}

	}


	
	
?>