<?php
include("../connect.php"); 
$response = array();
if(isset($_POST['commission_id']) && isset($_POST['admin_id']) && isset($_POST['status']))
{
	$admin_id=$_POST['admin_id'];
	$txn_id=$_POST['commission_id'];
	$type=$_POST['type'];
	$transaction_number=$_POST['txn_number'];
	$reason=$_POST['reason'];
	$status=$_POST['status'];
	date_default_timezone_set('Asia/Calcutta');
	$cdate  = date("Y-m-d");
	$ctime=date('h:i:sa');
	$success_flag=false;
	if($status=='Approve')
	{
		$res_product_stock = mysqli_query($con,"SELECT * FROM  tbl_commission where commission_id='$txn_id' and status='Pending'");
		if(mysqli_num_rows($res_product_stock)>0)
		{	
			while($fetch_detail=mysqli_fetch_assoc($res_product_stock))
			{
				$actual_adjust_amt_final=$fetch_detail['amount'];
				$actual_adjust_amt=$actual_adjust_amt_final;
				$agent_id=$fetch_detail['agent_id'];
				$res_purchase_adjust = mysqli_query($con,"SELECT * FROM tbl_agent_commission where agent_id='$agent_id' and status='Pending'");
				while($fetch_pending_data=mysqli_fetch_assoc($res_purchase_adjust))
				{
					$update_txn_id=$fetch_pending_data['credit_list_id'];
					$sale_credit_amt=round($fetch_pending_data['credit_amt']-$fetch_pending_data['adjust_amount']);
					if($sale_credit_amt==0)
						$uq1=mysqli_query($con,"UPDATE tbl_agent_commission SET status='Complete' where credit_list_id='$update_txn_id' and agent_id='$agent_id'");
					if($actual_adjust_amt<=0)
						break;
					if($actual_adjust_amt>$sale_credit_amt)
					{
						if($sale_credit_amt>0)
						{
							$actual_adjust_amt11=round($sale_credit_amt+$fetch_pending_data['adjust_amount']);
							$uq1=mysqli_query($con,"UPDATE tbl_agent_commission SET settlement_date='$cdate',adjust_amount='$actual_adjust_amt11',status='Complete' where agent_id='$agent_id' and credit_list_id='$update_txn_id'");
							$actual_adjust_amt=$actual_adjust_amt-$sale_credit_amt;
						}
					}
					else if($sale_credit_amt>0)
					{
						$actual_adjust_amt11=round($actual_adjust_amt+$fetch_pending_data['adjust_amount']);
						$uq1=mysqli_query($con,"UPDATE tbl_agent_commission SET adjust_amount='$actual_adjust_amt11' where agent_id='$agent_id' and credit_list_id='$update_txn_id'");
						$actual_adjust_amt=0;
						break;
					}
				}
				$actual_adjust_amt_credit=$actual_adjust_amt_final-$actual_adjust_amt;
				if($actual_adjust_amt_credit==0)
				{
					$uq2=mysqli_query($con,"update tbl_commission set status='Complete',pay_type='$type',transaction_number='$transaction_number', complete_date='$cdate',complete_time='$ctime' where commission_id='$txn_id'"); 
				}
				else if($actual_adjust_amt_credit>0)
				{
					$uq2=mysqli_query($con,"update tbl_commission set status='Complete',pay_type='$type',transaction_number='$transaction_number',complete_date='$cdate',complete_time='$ctime',paid_amt='$actual_adjust_amt_credit' where commission_id='$txn_id'"); 
				}	
			}
			$response['success']='1';
			$response['details']="Complete Successful";			
			$success_flag=true;
		}
		else
		{
			$response['success']='0';
			$response['details']="Transaction ID not valid";
			$success_flag=false;
		}
	}
	else
	{
		$uq2=mysqli_query($con,"update tbl_commission set status='Reject',rejection_reason='$reason',complete_date='$cdate',complete_time='$ctime' where commission_id='$txn_id'"); 
		$response['success']='1';
		$response['details']="Complete Successful";	
		$success_flag=true;
	}

	if($success_flag==true){
		$res_agent = mysqli_query($con,"SELECT *,agent_name,manager_name FROM `tbl_commission`,`tbl_agent`,`tbl_manager` where tbl_commission.agent_id = tbl_agent.agent_id and tbl_commission.commission_id='$txn_id'and admin_id='$admin_id' and manage_type='Admin'");
		if(mysqli_num_rows($res_agent)>0)
		{	
			$fetch_detail=mysqli_fetch_assoc($res_agent);
			$agent_name=$fetch_detail['agent_name'];
			$admin_name=$fetch_detail['manager_name'];
			$amount=$fetch_detail['amount'];
			$agent_id=$fetch_detail['agent_id'];
		}	

		$title = "Commission Request Response";
		$message="$admin_name has $status your request for Commission of Agent $agent_name of amount Rs. $amount";

		$res_device_id = mysqli_query($con,"SELECT * FROM `tbl_commission`,`tbl_manager` where tbl_commission.manager_id=tbl_manager.manager_id and tbl_commission.commission_id='$txn_id'");
		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')");
			}
		}
	}
}
else
{
	$response['success']='0';
	$response['details']="Required field missing";
}	
echo json_encode($response);
?>