<?php

//$_REQUEST['refer_id'] 	= '816978632';
//$_REQUEST['package_id'] = 'b97775df-efff-c8f9-81ec-5a93b2c2f09f';

error_reporting(E_ERROR | E_PARSE);

date_default_timezone_set('Asia/Kolkata');
$validationErr ="";

if(!isset($_REQUEST['refer_id']) || $_REQUEST['refer_id'] == '') {
	$validationErr = "Please enter refer id!";
} else if(!isset($_REQUEST['package_id']) || $_REQUEST['package_id'] == '') {
	$validationErr = "Please enter package id!";
}

$file = fopen("../API_logs/API_uploadvolume.txt","a");
$today = date("Y-m-d H:i:s");
$parameter = "-------------------".$today."----------------------------"."\n";
//$parameter = $parameter."File Name=>".$_FILES['videofile']['name']."\n";
foreach($_REQUEST as $key=>$value) {
	$parameter = $parameter."$key=>$value"."\n";
}
$parameter = $parameter."-----------------------------------------------"."\n";
//print "<br>".$parameter;
fwrite($file, $parameter);
fclose($file);

if($validationErr != '') {

	$result1 = array();
	$result1['status'] = '0';
	$result1['message'] = $validationErr;
	print $return = json_encode($result1);

	$file = fopen("../API_logs/API_uploadvolume.txt","a");
	$today = date("Y-m-d H:i:s");
	$parameter = "-------------------".$today."----------------------------"."\n";
	//$parameter = $parameter."File Name=>".$_FILES['videofile']['name']."\n";
	$parameter = $parameter.$return."\n";
	$parameter = $parameter."-----------------------------------------------"."\n";
	//print "<br>".$parameter;
	fwrite($file, $parameter);
	fclose($file);
	
	exit;

} 

require_once('database.php');

class UploadVolume{

	public $db;
	public $pdo;	
	
	function __construct() {

		$db = new Database();
		$this->pdo = $db->getConnection();
		$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		$this->pdo->beginTransaction();


    }



	function findExtremeMember($referID, $i) {

		$sqlSelect 	= "SELECT sponsor_id, left_node, right_node FROM my_registration where refer_id='$referID'";
		$stmt 		= $this->pdo->prepare($sqlSelect);
		$stmt->execute();
		$result 	= $stmt->fetch(PDO::FETCH_ASSOC);
		if(!empty($result))
		{
			$varChar = $result['sponsor_id'].":".$result['left_node'].":".$result['right_node'];
			if($result['sponsor_id'] != "" && $result['sponsor_id'] != 0) {
				$_REQUEST['uptreelist'][] = $varChar;
			}
			$i = $i+1;
			$this->findExtremeMember($result['sponsor_id'], $i);
		}

		
	}




	function get_member_package($packageID) {
		$refer_id 	= $_REQUEST['refer_id'];
		$sqlPackage = "SELECT product_price, total_PV, power_leg, date_entered FROM `my_member_package` where upload_volume=0 AND deleted=0 AND id='$packageID' AND refer_id='$refer_id'";
		$stmt 	   	= $this->pdo->prepare($sqlPackage);
		$stmt->execute();
		$sqlCnt    	= $stmt->rowCount();

		if($sqlCnt == 0) {
			$result = 0;
		} else {
			$result   = $stmt->fetch(PDO::FETCH_ASSOC);
		}
		return $result;		
	}


	
	function update_member_package($packageID) {
		$date = date("Y-m-d H:i:s");
		$sqlPackage = "UPDATE `my_member_package` set payment_status=1, upload_volume=1, date_modified='$date' WHERE id='$packageID'";
		$stmt 	   	= $this->pdo->prepare($sqlPackage);
		$stmt->execute();
	}





	function member_volume_update($node, $sponsoredID, $purchase, $pv, $i ) {
		$tableName 		= "my_member_volume";
		$insertInChild 	= "SELECT * FROM $tableName WHERE refer_id='$sponsoredID'";
		$stmt 		   	= $this->pdo->prepare($insertInChild);
		$stmt->execute();
		$sqlCnt    					= $stmt->rowCount();
		$left_member_purchase_count = 0;
		$left_member_purchase		= 0;
		$left_member_pv				= 0;
		$right_member_purchase_count= 0;
		$right_member_purchase		= 0;
		$right_member_pv			= 0;
		

		if($sqlCnt != 0) {
			$result    		= $stmt->fetch(PDO::FETCH_ASSOC);

			$cdate 			= date('Y-m-d H:i:s');	
			//print "<br><br>".$result['total_bbt'];
			//print "<br>".$totalBBT;

			$_REQUEST['logs'] = $_REQUEST['logs'].$result['total_purchase']."\n\n";
			$_REQUEST['logs'] = $_REQUEST['logs'].$purchase."\n\n";
			$_REQUEST['logs'] = $_REQUEST['logs'].$result['total_pv']."\n\n";
			$_REQUEST['logs'] = $_REQUEST['logs'].$pv."\n\n";

			$totalPurchase 				= $result['total_purchase'] + $purchase;
			$totalPV 					= $result['total_pv'] + $pv;
			$left_member_purchase_count = $result['left_member_purchase_count'];
			$left_member_purchase		= $result['left_member_purchase'];
			$left_member_pv				= $result['left_member_pv'];
			$right_member_purchase_count= $result['right_member_purchase_count'];
			$right_member_purchase		= $result['right_member_purchase'];
			$right_member_pv			= $result['right_member_pv'];

			if($node=='l')
			{
				$left_member_purchase_count = $left_member_purchase_count + 1;
				$left_member_purchase		= $left_member_purchase + $purchase;
				$left_member_pv				= $left_member_pv + $pv;
			}

			else
			{
				$right_member_purchase_count = $right_member_purchase_count + 1;
				$right_member_purchase		 = $right_member_purchase + $purchase;
				$right_member_pv			 = $right_member_pv + $pv;
			}


			$updateChild 	= "UPDATE $tableName SET date_modified='$cdate', total_purchase='$totalPurchase', total_pv='$totalPV', left_member_purchase_count='$left_member_purchase_count', left_member_purchase='$left_member_purchase', left_member_pv='$left_member_pv', right_member_purchase_count='$right_member_purchase_count', right_member_purchase='$right_member_purchase', right_member_pv='$right_member_pv' WHERE refer_id='$sponsoredID'";
			$stmtUpdate	   	= $this->pdo->prepare($updateChild);
			$stmtUpdate->execute();
			$_REQUEST['logs'] = $_REQUEST['logs'].$updateChild."\n\n";


		} else {

			if($node=='l')
			{
				$left_member_purchase_count = $left_member_purchase_count + 1;
				$left_member_purchase		= $left_member_purchase + $purchase;
				$left_member_pv				= $left_member_pv + $pv;
			}

			else
			{
				$right_member_purchase_count = $right_member_purchase_count + 1;
				$right_member_purchase		 = $right_member_purchase + $purchase;
				$right_member_pv			 = $right_member_pv + $pv;
			}


			$sqlInsert 	= "Insert into $tableName(refer_id, total_purchase, total_pv, left_member_purchase_count, left_member_purchase, left_member_pv, right_member_purchase_count, right_member_purchase, right_member_pv, status) values('$sponsoredID', '$purchase', '$pv', '$left_member_purchase_count', '$left_member_purchase', '$left_member_pv', '$right_member_purchase_count', '$right_member_purchase', '$right_member_pv', '0')";

			$stmtInsert	= $this->pdo->prepare($sqlInsert);
			$stmtInsert->execute();
			$_REQUEST['logs'] = $_REQUEST['logs'].$sqlInsert."\n\n";
		}
	}



	function add_member_in_child($date_entered, $node, $sponsoredID, $refer_id, $package_id, $purchase, $pv, $addon, $power_leg) {
		$tableName 		= "my_payout_child";
		$insertInChild = "INSERT INTO $tableName(date_time, sponsored_id, refer_id, node, package_id, purchase, pv, addon, power_leg) VALUES('$date_entered', '$sponsoredID', '$refer_id', '$node', '$package_id', '$purchase', '$pv', '$addon','$power_leg')";
		$stmt 		   = $this->pdo->prepare($insertInChild);
		$stmt->execute();
		$_REQUEST['logs'] = $_REQUEST['logs'].$insertInChild."\n\n";
	}


	

}

$obj 			= new UploadVolume();

try 
{
	$i = 1;
	
	$package_id 	= $_REQUEST['package_id'];	
	$packageDetails		= $obj->get_member_package($package_id);
	$totalPurchase	    = $packageDetails['product_price'];
	$totalPV			= $packageDetails['total_PV'];
	$date_entered       = $packageDetails['date_entered'];
	$power_leg          = $packageDetails['power_leg'];


	
	if($totalPurchase == 0 || $totalPV == 0) {

		$result1['status'] 	= '0';
		$result1['message'] = 'May be this package is already added in above tree members volume or does not found!';

	} else {

		$sponsorID 		= $obj->findExtremeMember($_REQUEST['refer_id'], $i);

		////print "<pre>$totalBBT";
		//print_r($_REQUEST['uptreelist']);
		//print "</pre>";
		
		if(count($_REQUEST['uptreelist']) > 0) {
			$i=1;
			foreach($_REQUEST['uptreelist'] as $key => $value) {

				$valueArr 		= explode(":", $value);

				$sponsoredID 	= $valueArr[0];
				$refer_id 		= $_REQUEST['refer_id'];

				if($valueArr[1] == 1) {
					$node 	= 'l';
				} else if($valueArr[2] == 1) {
					$node 	= 'r';
				}

				$obj->add_member_in_child($date_entered, $node, $sponsoredID, $refer_id, $package_id,  $totalPurchase, $totalPV, $addon, $power_leg);
				$obj->member_volume_update( $node, $sponsoredID, $totalPurchase, $totalPV, $i );
				$i=$i+1;
			}

			$obj->update_member_package($package_id);

			$result1['status'] 	= '1';
			$result1['message'] = 'Records are Added Successfully!';
			$obj->pdo->commit();
		}
	}
}


catch(PDOException $e)
{
	$obj->pdo->rollback();
	//echo "Error: " . $e->getMessage();
	$result1 =	$e->getMessage();

	$fileName = $_REQUEST['refer_id'].'.txt';
	$file = fopen("../API_logs/my_member_volume/".$fileName,"a");
	$today = date("Y-m-d H:i:s");
	$parameter = "-------------------".$today."----------------------------"."\n";
	//$parameter = $parameter."File Name=>".$_FILES['videofile']['name']."\n";
	$parameter = $parameter.$result1."\n";
	$parameter = $parameter."-----------------------------------------------"."\n";
	//print "<br>".$parameter;
	fwrite($file, $parameter);
	fclose($file);

	$result1['status'] 			= '0';
	$result1['message'] 		= 'Unable to process the request at this time. Please try after some time!';
}

print $result1 = json_encode($result1['message']);

$fileName = $_REQUEST['refer_id'].'.txt';
$file = fopen("../API_logs/my_member_volume/".$fileName,"a");
$today = date("Y-m-d H:i:s");
$parameter = "-------------------".$today."----------------------------"."\n";
//$parameter = $parameter."File Name=>".$_FILES['videofile']['name']."\n";
$parameter = $parameter.$_REQUEST['logs']."\n";
$parameter = $parameter."-----------------------------------------------"."\n";
//print "<br>".$parameter;
fwrite($file, $parameter);
fclose($file);

?>

