<?php
include("../config.php");
$response = array();
if ($_POST['delete']) {
	$id 			   = $_POST['delete'];
	$type              = 'cancel_lhc';
    $user_id           = $_SESSION['user_detail']['id'];
    $data              = serialize($_POST);
    $company_id        = $_SESSION['company_id'];
	$branch_id         = $_SESSION['branch_id'];
    $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'];

    $lhch     = "SELECT * FROM lhc_payment_status WHERE  lhc_no = '".$id."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
    $lhch_res = mysqli_query($conn,$lhch);
    $vou = array();
    while ($lhch_row = mysqli_fetch_array($lhch_res)) {
    	$vou[] = $lhch_row['voucher_no'];
    }
    
    // UPDATE CANCEL FLAG
    $update = mysqli_query($conn, "UPDATE `lhc` SET `cancel_flag` = 'Y' WHERE `lhc_no` = '".$id."'");

    foreach ($vou as $key => $vou) {

    	$gl 	 = "DELETE FROM `general_ledger` WHERE `voucher_no` = '".$vou."' AND flag = 'LHC' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
    	$gl_res  = mysqli_query($conn,$gl);

    	$pay 	 = "DELETE FROM `payment` WHERE `voucher_no` = '".$vou."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
    	$pay_res = mysqli_query($conn,$pay);
    }

    /*lhc_change_truck_history*/
	$gls = "DELETE FROM `general_ledger` WHERE `voucher_no` = '".$id."' AND flag = 'LHC' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
	$gls_res = mysqli_query($conn,$gls);

	$trans = "DELETE FROM trans WHERE voucher_no='".$id."' AND flag = 'LHC' AND `branch_id`='".$branch_id."' AND `company_id`='".$company_id."'";
	$trans_res = mysqli_query($conn,$trans);

	$trans_det = "DELETE FROM trans_details WHERE voucher_no='".$id."' AND flag = 'LHC' AND `branch_id`='".$branch_id."' AND `company_id`='".$company_id."'";
	$result = mysqli_query($conn,$trans_det);

	$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'] = 'Lr Deleted Successfully ...';
	} else {
		$response['status']  = 'error';
		$response['message'] = 'Unable to delete product ...';
	}
	echo json_encode($response);
}
?>