<?php
	include("../config.php");
	$response = array();
	if ($_POST['delete']) {
		$id = $_POST['delete'];
		$type              = 'delete_commission';
	    $user_id           = $_SESSION['user_detail']['id'];
	    $data              = serialize($_POST);
	    $created_datetime  = date('Y-m-d H:i:s');
	    $created_id        = $_SESSION['user_detail']['id'];
	    $updated_datetime  = date('Y-m-d H:i:s');
	    $updated_id        = $_SESSION['user_detail']['id'];

	    $get_lr 		   = "SELECT * FROM mst_commission WHERE bill_no = '".$id."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
	    $get_lr_res        = mysqli_query($conn,$get_lr);

	    $lr = array();
	    while ($get_lr_row        = mysqli_fetch_assoc($get_lr_res)) {
	    	$lr[] = $get_lr_row;
	    }
	    foreach ($lr as $key => $lr) {
	    	$lr 	= "UPDATE `billing` SET `commission_flag`='N' WHERE `invoice_no`='".$lr['bill_no']."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
			$lr_res = mysqli_query($conn,$lr);
	    }
	    

	    $bill 			   = "DELETE FROM mst_commission  WHERE bill_no = '".$id."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
	    $bill_res 		   = mysqli_query($conn,$bill);
 		

	    $activiti = "INSERT INTO `activities`(`type`, `user_id`, `data`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$type."','".$user_id."','".$data."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
    	$acttivity_res = mysqli_query($conn,$activiti);

		if ($result) {
			$response['status']  = 'success';
			$response['message'] = 'Commission Deleted Successfully ...';
		} else {
			$response['status']  = 'error';
			$response['message'] = 'Unable to delete Commission ...';
		}
		echo json_encode($response);
	}
?>