<?php
session_start();
require_once('include/constant.php');
include 'get_user_info.php';
require_once('include/functions.php');

//$data = get_purchase_comission('B00015', 'B00010', 1000);
//P($data);

if(isset($_REQUEST['purchase_btn']))
{
	
	$security_check = isset($_REQUEST['security_check']) ? $_REQUEST['security_check'] : '';
	if(isset($_SESSION['requirity_check']) && $_SESSION['requirity_check'] == $security_check && isset($got_unique_id))
	{
	
		$main_user = $got_unique_id; // $_REQUEST['fetch_id'];
	if ($_REQUEST['user'] == 'self') {
		$sub_user = $got_unique_id; // $_REQUEST['fetch_id'];
	} else if ($_REQUEST['user'] == 'others') {
		$sub_user = $got_unique_id; // $_REQUEST['buy_user_idd'];
	}
	
	if(!isset($total_balance) || $total_balance =='' || (int)$total_balance <1)
	{
		 echo '<script> alert("Insufficient wallet balance\nPlease add money to wallet."); window.location="buy_products.php" </script>';
		 die;
	}
	else if(isset($_REQUEST['treansaction_pwd_buy']) && trim($_REQUEST['treansaction_pwd_buy']) != $transaction_pwd)
	{
		echo '<script> alert("Sorry, Invalid transaction password."); window.location="buy_products.php" </script>';
		die;
	}
	else
	{
	
	$tax_amount = $_REQUEST['get_tax_amount'];
	$prodyct_array = explode('+', $_REQUEST['product']);
	$product_id = $prodyct_array['0'];
	$product_price = $prodyct_array['1'];
	$product_bv = $prodyct_array['2'];
	$total_product_price_with_tax = $product_price + $tax_amount;
	
	//  Mailing address
	$mailing_address = array(
		'purchase_by'	=> isset($_REQUEST['purchase_by']) ? $_REQUEST['purchase_by'] : '',
		'name'	=> isset($_REQUEST['m_name']) ? $_REQUEST['m_name'] : '',
		'co'	=> isset($_REQUEST['m_co']) ? $_REQUEST['m_co'] : '',
		'address'	=> isset($_REQUEST['m_address']) ? $_REQUEST['m_address'] : '',
		'landmark'	=> isset($_REQUEST['m_landmark']) ? $_REQUEST['m_landmark'] : '',
		'pincode'	=> isset($_REQUEST['m_pin']) ? $_REQUEST['m_pin'] : '',
		'mobile'	=> isset($_REQUEST['m_mobile']) ? $_REQUEST['m_mobile'] : '',
		'remarks'	=>	isset($_REQUEST['m_remarks']) ? $_REQUEST['m_remarks'] : '',
	);
	
	
	if((int)$total_balance < (int)$total_product_price_with_tax)
	{
		echo '<script> alert("You do not sufficient amount to buy this product ."); window.location="buy_products.php" </script>';
		die;
	}
	
	if ($main_user == $sub_user && strtoupper($main_user) != ADMIN_ID) {
		
		/*********************************
		Product purchase amount send to admin
		*******/
		wallet($wallet = array('sender_id'=> $sub_user, 'receiver_id'=>  ADMIN_ID, 'amount'=> $total_product_price_with_tax));

		$wallet_arr = array(
			'sender_id'			=>	$sub_user,
			'receiver_id'		=>	ADMIN_ID,
			'amount'			=>	$total_product_price_with_tax,
			'respective_user'	=>	$sub_user,
			'transaction_type'	=>	2,  //  Purchase product
			'remarks'			=>  'User '.$sub_user.' purchased Product',
			'status'			=>	1,
			'transaction_id'	=>  get_unique_id('t')
		);
		add_wallet_history($wallet_arr);
		
		/**********************************
		Comission send to referral id
		**********/
		
		$referrer_comission_amout = get_purchase_comission($sub_user, $refferance_id, $product_price);
		//P($referrer_comission_amout);
		if(isset($referrer_comission_amout['purchase_commission']) && (int)$referrer_comission_amout['purchase_commission']>0 && strtoupper($refferance_id) != ADMIN_ID)
		{	
			wallet($wallet = array('sender_id'=> ADMIN_ID, 'receiver_id'=>  $refferance_id, 'amount'=> $referrer_comission_amout['purchase_commission']));

			$wallet_arr = array(
				'sender_id'			=>	ADMIN_ID,
				'receiver_id'		=>	$refferance_id,
				'amount'			=>	$referrer_comission_amout['purchase_commission'],
				'respective_user'	=>	$sub_user,
				'transaction_type'	=>	4,  //  Purchasing commission for referrer
				'remarks'			=>  'User '.$sub_user.' purchased Product',
				'status'			=>	1,
				'transaction_id'	=>  get_unique_id('t')
			);
			add_wallet_history($wallet_arr);
		
			/******************************
			Reward give to referrer user
			***********/
			$reward = array(
				'user_id'			=>	$refferance_id,
				'reward'			=>	'Rs. '.$referrer_comission_amout['purchase_commission'],
				'reward_desc'		=>	'You have got this reward for your downline user('.$sub_user.') purchased product.',
				'respective_user'	=>	$sub_user,
				'reward_type'		=>  1,
				'status'			=>	1, //  received
				'remarks'			=>  'User '.$sub_user.' purchased Product',
				'status'			=>	1,
				'transaction_id'	=>  get_unique_id('r')
			);
			give_reward($reward);
		}
		// Pair purchase bonus
		/*if(isset($referrer_comission_amout['pair_purchase_commission']) && (int)$referrer_comission_amout['pair_purchase_commission']>0)
		{
			wallet($wallet = array('sender_id'=> ADMIN_ID, 'receiver_id'=>  $refferance_id, 'amount'=> $referrer_comission_amout['pair_purchase_commission']));

			$wallet_arr = array(
				'sender_id'			=>	ADMIN_ID,
				'receiver_id'		=>	$refferance_id,
				'amount'			=>	$referrer_comission_amout['pair_purchase_commission'],
				'respective_user'	=>	$sub_user,
				'transaction_type'	=>	4,  //  Purchasing commission for referrer
				'remarks'			=>  'User '.$sub_user.' purchased Product. You have got pairing bonus',
				'status'			=>	1,
				'transaction_id'	=>  get_unique_id('t')
			);
			add_wallet_history($wallet_arr);
		
			// Reward give to referrer user
			
			$reward = array(
				'user_id'			=>	$refferance_id,
				'reward'			=>	'Rs. '.$referrer_comission_amout['pair_purchase_commission'],
				'reward_desc'		=>	'Congratulations, you have got your pairing bonus',
				'respective_user'	=>	$sub_user,
				'reward_type'		=>  2,
				'status'			=>	1, //  received
				'remarks'			=>  'Congratulations, you have got your pairing bonus',
				'status'			=>	1,
				'transaction_id'	=>  get_unique_id('r')
			);
			give_reward($reward);
		}*/
		
		//  Pairing bonus [ 100, 200, 400 pair ]
		/*if(isset($referrer_comission_amout['is_main_pair_applicable']) && isset($referrer_comission_amout['pair_level']))
		{
		
			//Reward give to referrer user
			
			$main_reward = array(
				'user_id'			=>	$refferance_id,
				'reward'			=>	$referrer_comission_amout['pair_level'],
				'reward_desc'		=>	$referrer_comission_amout['pair_msg'],
				'respective_user'	=>	null,
				'reward_type'		=>  3,
				'status'			=>	1, //  received
				'remarks'			=>  'You have completed '.$referrer_comission_amout['pair_level'].' pairs',
				'status'			=>	1,
				'transaction_id'	=>  get_unique_id('r')
			);
			give_reward($main_reward);
		
		// Update pair count
		$update_pair_count = "UPDATE registration SET pair_count = '".$referrer_comission_amout['pair_level']."' WHERE unique_user_id = '$refferance_id' ";
		mysqli_query($conn, $update_pair_count);
			
		}*/
		
		// window.location="buy_products.php"
		//window.location="buy_products.php"
		
		// PURCHASE PRODUCT
		$purchase_id=get_unique_id('p');
		$query = "insert into product_purchase(purchase_user_id,purchase_product_id,purchase_product_price,tax_amount,purchase_product_bv,purchase_unique_id,mailing_address,purchase_date)
				values('$sub_user','$product_id','$product_price','$tax_amount','$product_bv','$purchase_id','".json_encode($mailing_address)."','$today_date') ";
		$connection = mysqli_query($conn, $query);
		if ($connection) 
		{
			$update_status = "UPDATE registration SET active_deactive = 1 WHERE unique_user_id = '$sub_user' ";
			mysqli_query($conn, $update_status);
			
			echo '<script> alert("Product purchased successfully");  window.location="buy_products.php"   </script>';
	    }
		else
		{
		  echo '<script> alert("Sorry, We are facing technical issue"); window.location="buy_products.php"  </script>';
		}

	 }  //  user type check
	 
	}  //  wallet amoutn check

	}//  scam check
	else
	{
	  echo '<script> alert("Scam user not allowed"); window.location="buy_products.php" </script>';
	}

}//  submit check