<?php
include("../connect.php"); 
$response = array();
if(isset($_POST['manager_id']) && isset($_POST['amount']) && isset($_POST['agent_id'])&& isset($_POST['to_be_paid']))
{
	$ver = $_POST['manager_id'];
	$agent_id = $_POST['agent_id'];
	$date1=$_POST['date1'];
	$date2=$_POST['date2'];
	$to_be_paid=$_POST['to_be_paid'];
	$amount=$_POST['amount'];
	date_default_timezone_set('Asia/Calcutta');
	$cdate  = date("Y-m-d");
	$ctime=date('h:i:sa');

	$res_product_stock = mysqli_query($con,"SELECT * FROM  tbl_commission where agent_id='$agent_id' and date1='$date1'and date2='$date2'");
	if(mysqli_num_rows($res_product_stock)<=0){

		$query = mysqli_query($con,"INSERT INTO `tbl_commission`(`agent_id`, `amount`,`date1`,`date2`, `to_be_paid`, `insert_date`, `insert_time`, `manager_id`, `status`) VALUES ('$agent_id','$amount','$date1','$date2','$to_be_paid','$cdate','$ctime','$ver','Pending')");

		$res_agent = mysqli_query($con,"SELECT agent_name,manager_name FROM `tbl_agent`,`tbl_manager` where agent_id ='$agent_id' and tbl_manager.manager_id = '$ver'");
		if(mysqli_num_rows($res_agent)>0)
		{	
			$fetch_detail=mysqli_fetch_assoc($res_agent);
			$agent_name=$fetch_detail['agent_name'];
			$manager_name=$fetch_detail['manager_name'];
		}	

		$title = "Agent Commission Request";
		$message="$manager_name has added request for Commission of Agent $agent_name of amount Rs. $amount";

		$res_device_id = mysqli_query($con,"SELECT device_id,manager_id FROM `tbl_manager` where manage_type = 'Admin'");
		if(mysqli_num_rows($res_device_id)>0)
		{	
			while( $fetch_detail=mysqli_fetch_assoc($res_device_id))
			{
				$key=$fetch_detail['device_id'];
				$manager_id=$fetch_detail['manager_id'];
				include("send_notification.php");
				$query = mysqli_query($con,"INSERT INTO `tbl_notification_history`(`manager_id`, `agent_id`, `title`, `message`, `date`, `time`) VALUES ('$manager_id','$agent_id','$title','$message','$cdate','$ctime')");
			}
		}

		$response['success']='1';
		$response['details']="Insert Successful";

	}else{
		$response['success']='0';
		$response['details']="Commission already Requested for Selected Period";
	}
}
else
{
	$response['success']='0';
	$response['details']="Required field missing";
}	
echo json_encode($response);
?>