<?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");
	setlocale(LC_MONETARY, 'en_IN');

	date_default_timezone_set('Asia/Kolkata');

	require_once("database.php");



	class PurchaseList
	{
		private $conn;
		public  $typeRequest;

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

		}


		function getPurchaseList()
		{

		   try{

				$stmt  = $this->conn->prepare("select a.id, a.name as productName, b.name, b.last_name, a. refer_id,  a.date_entered, a.product_price, a.total_PV, a.order_id, a.online_TransactionID,
				a.paid_by_voucher, a.voucher_code,	a.paid_by_deposit, a.paid_by_cash, a.paid_by_BBT, a.total_paid, a.payment_status, a.upload_volume from my_member_package as a LEFT JOIN my_registration as b on a.refer_id=b.refer_id  where a.power_leg='$this->typeRequest' && a.deleted='0' order by a.date_entered desc");
				$stmt->execute();
				$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
			 	return $result;
			
			  }catch(Exception $err){
				return 0;
			  } 
		
		}


		function getPurchaseData()
		{

		   try{

				$stmt  = $this->conn->prepare("select count(distinct(refer_id)) as totalMember, count(id) as totalPurchase, sum(product_price) as totalPrice, sum(total_paid)  as totalPaid from my_member_package where power_leg=0");
				$stmt->execute();
				$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
			 	return $result;
			
			  }catch(Exception $err){
				return 0;

			  } 
		
		}
		
	}

	$result = array();

	$purchase = new PurchaseList();
	$purchase->typeRequest = $_REQUEST['typeRequest'];
	$purchaseList = $purchase->getPurchaseList();

	$result['success'] = true;
	$result['message'] = "Success";
	$result['purchaseList']  = $purchaseList;
	$result['data'] = '';

	if($_REQUEST['typeRequest']==0)
	{
		$result['data']  = $purchase->getPurchaseData();

	}


	echo json_encode($result);



?>

