<?php

	header('Content-Type: application/json');
	ini_set('display_startup_errors',1);
	ini_set('display_errors',1);
	error_reporting(1);
	ini_set("log_errors", 1);
	ini_set("error_log", "php-error.log");
	date_default_timezone_set('Asia/Kolkata');

	require_once("database.php");


	class PayoutRelease
	{
		private $conn;
		public  $payout_id;

		function __Construct()
		{
			$dbconnect = new Database();
			$this->conn = $dbconnect->getConnection();
		}

		function checkPayoutRelease()
		{
			 try{

				$stmt  = $this->conn->prepare("SELECT id, status from my_payout_run where id=?");
				$stmt->execute(array($this->payout_id));
				$result = $stmt->fetch(PDO::FETCH_ASSOC);

				if(empty($result['id']))
					return false;

				else if($result['status']==1)
					return false;

				else
					return true;

				return $result;
			
			  }catch(Exception $err){
				return 0;
			  } 

		}

		function approvePayout()
		{

		   try{

				$stmt  = $this->conn->prepare("UPDATE my_payout_run set status=1 WHERE id=$this->payout_id");
				$stmt->execute();
			 	return true;

			
			  }catch(Exception $err){
				return false;
			  } 
		
		}


		/*function getPayoutMemberList()
		{

		   try{

				$stmt  = $this->conn->prepare("SELECT a.refer_id FROM my_member_payout as a left join my_payout_main as b on a.refer_id=b.refer_id WHERE b.payout_id='$this->payout_id' AND b.incentive>0 AND a.approve=1");
				$stmt->execute();
				$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
			 	return $result;
			
			  }catch(Exception $err){
				return 0;
			  } 
		
		}*/


		/*function ReleasePayout($memberList)
		{

		   try{
   	 			 $this->conn->beginTransaction();

				 foreach($memberList as $row)
				 {
					 $stmt1 = $this->conn->prepare("SELECT incentive from my_payout_main WHERE refer_id=? AND payout_id=? AND incentive_load=0");
					 $stmt2 = $this->conn->prepare("SELECT refer_id, balance from my_pv_balance WHERE refer_id=?");
					 $stmt3 = $this->conn->prepare("INSERT INTO my_pv_transactions(id, refer_id, transfer_to, mode, initial_pv, pv, final_pv, date_entered, description) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)");
  					 		 
					 $stmt4_1 = $this->conn->prepare("INSERT INTO my_pv_balance(id, refer_id, balance, date_entered) VALUES(?, ?, ?, ?)");
 					 $stmt4_2 = $this->conn->prepare("UPDATE my_pv_balance  set balance=?, date_modified=? WHERE refer_id=?");

					 $stmt5 = $this->conn->prepare("UPDATE my_payout_main set incentive_load=1 WHERE refer_id=? AND payout_id=?");
					 $stmt6 = $this->conn->prepare("UPDATE my_payout_run set status=1 WHERE id=?");

					 $date = date("Y-m-d H:i:s");
					
					 //Incentive for payout
					 $stmt1->execute(array($row['refer_id'], $this->payout_id));
				     $result1 = $stmt1->fetch(PDO::FETCH_ASSOC);
					 $incentive = $result1['incentive'];

					//Balance of existing Wallet	
					 $stmt2->execute(array($row['refer_id']));
				     $result2 = $stmt2->fetch(PDO::FETCH_ASSOC);
					 if(empty($result2['balance']))
						 $balance = 0;
					 
					 else
						$balance = $result2['balance'];

					 //Insert Transaction Entry
					 $txnID 	= md5(date("Y-m-d H:i:s"));
					 $stmt3->execute(array($txnID, $row['refer_id'], $row['refer_id'], 'Cr', $balance,  $incentive, $incentive+$balance, $date, 'Payout'));

					 //Insert or Wallet Balance
 					 $walletID 	= md5(date("Y-m-d H:i:s").$row['refer_id']);
					 if(empty($result2['refer_id']))
						 $stmt4_1->execute(array($walletID, $row['refer_id'], $incentive, date("Y-m-d H:i:s")));

					else
						 $stmt4_2->execute(array($incentive+$balance, $date, $row['refer_id']));


					 $stmt5->execute(array($row['refer_id'], $this->payout_id));
					 $stmt6->execute(array($this->payout_id));
					

					 sleep(1.5);


					 //echo "Refer ID:" . $row['refer_id'] . "  Incentive:" . $incentive . " Balance:" . $balance . "\n";

				 }

				$this->conn->commit();
			 	return true;
			
			  }catch(Exception $err){
				$this->conn->rollBack();
				throw $err;
				echo $err->getMessage();
				return false;
			  } 
		
		}*/
		
	}


	if(empty($_REQUEST['payout_id']))
	{
		$result = array();
		$result['success'] = true;
		$result['message'] = 'Kindly mention the payout to release!';
		echo json_encode($result);
		exit;
	}

	$result = array();
	$payout = new PayoutRelease();
	$payout->payout_id = $_REQUEST['payout_id'];


	$verifyPayout = $payout->checkPayoutRelease();

	if(!($verifyPayout))
	{
		$result = array();
		$result['success'] = true;
		$result['message'] = $verifyPayout;
		echo json_encode($result);
		exit;
	}


	//$memberList = $payout->getPayoutMemberList();
	//$releasePayout = $payout->releasePayout($memberList);
	$releasePayout = $payout->approvePayout();



	if($releasePayout)
		//$result['message'] = 'Payout has been successfully credited to all members account!';
		$result['message'] = 'Payout has been approved!';


	else
		$result['message'] = 'Payout released failed due to some technical error!';


	
	$result['success'] = true;
	echo json_encode($result);



?>

