<?php
	include("../config.php");
	$response = array();
	if ($_POST['delete']) {
		$id = $_POST['delete'];
		$type              = 'cash_payment_delete';
	    $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'];

	    $gld 			   = "DELETE FROM `general_ledger` WHERE voucher_no = '".$id."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."'";
		$gld_res 	       = mysqli_query($conn,$gld);

		$payment 	 			= "SELECT * FROM payment WHERE voucher_no='".$id."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."'";
		$payment_res 			= mysqli_query($conn,$payment);
		$payment_row 			= mysqli_fetch_assoc($payment_res);
		
		$payment_trip 	 			= "DELETE FROM lr_expenses WHERE voucher_no='".$id."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."' AND flag = 'C'";
		$payment_res_trip 			= mysqli_query($conn,$payment_trip);
		
		
		$lhc 	 	 			= "SELECT * FROM lhc WHERE lhc_no = '".$payment_row['lhc_no']."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."'";
		$lhc_res 	 			= mysqli_query($conn,$lhc);
		$lhc_row	 			= mysqli_fetch_assoc($lhc_res);
		$last_bank_amnt 		= $lhc_row['cash'] - $payment_row['amount'];
		
		$lhc_update     		= "UPDATE `lhc` SET `balance`='".$payment_row['amount']."', `cash`='".$last_bank_amnt."' WHERE `lhc_no` = '".$payment_row['lhc_no']."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."'";
		$lhc_update_res 		= mysqli_query($conn,$lhc_update);

		$lhc_payment_status     = "DELETE FROM `lhc_payment_status` WHERE `voucher_no` = '".$id."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."'";
		$lhc_payment_status_res = mysqli_query($conn,$lhc_payment_status);

		$trans     				= "SELECT * FROM `trans` WHERE `voucher_no` = '".$payment_row['lhc_no']."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."'";
		$trans_res 			 	= mysqli_query($conn,$trans);
		$trans_row 			 	= mysqli_fetch_assoc($trans_res);
		
		$last_trans_adj_amnt 	= $trans_row['adjust'];
		$new_adjust_mnt 		= $last_trans_adj_amnt - $payment_row['amount'];
		$new_balance 			= $payment_row['amount'];

		$trans_update = "UPDATE `trans` SET `adjust`='".$new_adjust_mnt."',`balance`='".$new_balance."' WHERE `voucher_no`='".$payment_row['lhc_no']."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."'";
		$trans_up_res = mysqli_query($conn,$trans_update);

		$trans_de_update = "UPDATE `trans_details` SET `adjust`='".$new_adjust_mnt."',`balance`='".$new_balance."' WHERE `voucher_no`='".$payment_row['lhc_no']."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."'";
		$trans_de_res = mysqli_query($conn,$trans_de_update);

		$query  		   = "DELETE FROM payment WHERE voucher_no='".$id."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."'";
		$result 		   = mysqli_query($conn,$query);
		
		/*$row    = mysqli_fetch_array($result);*/
		$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'] = 'Payment Deleted Successfully ...';
		} else {
			$response['status']  = 'error';
			$response['message'] = 'Unable to delete product ...';
		}
		echo json_encode($response);
	}
?>