<?php
include("../config.php");
$response = array();

if ($_POST['delete']) {
	$id = $_POST['delete'];
	$type              = 'delete_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 = mysqli_query($conn, "SELECT * FROM lhc_payment_status WHERE  lhc_no = '".$id."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'");
    $vou = array();
    while ($lhch_row = mysqli_fetch_assoc($lhch)) {
    }

    $lhc = mysqli_query($conn, "SELECT * FROM lhc WHERE lhc_no='".$id."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'");
    while ($lhc_row = mysqli_fetch_assoc($lhc)) {
    	$update_lr = mysqli_query($conn, "UPDATE `lr_mst` SET `lhc_status`='N' WHERE `lr_no`='".$lhc_row['lr_no']."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'");
    }

    $lhc_detail = mysqli_query($conn, "DELETE FROM `lhc_details` WHERE `lhc_id` = '".$id."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'");

    $lhc_exp = mysqli_query($conn, "DELETE FROM `lhc_expense_details` WHERE `lhc_no` = '".$id."'");

    $lhc_truck_his_del = mysqli_query($conn, "DELETE FROM `lhc_change_truck_history` WHERE `lhc_no` = '".$id."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'");

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


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

    $delete_gl = mysqli_query($conn, "DELETE FROM `general_ledger` WHERE `voucher_no` = '".$id."' AND flag = 'G' AND mark = 'P' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'");

    $delete_lhc = mysqli_query($conn, "DELETE FROM lhc WHERE lhc_no='".$id."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'");

	$delete_lhc_history = mysqli_query($conn, "DELETE FROM lhc_balance_history WHERE lhc_no='".$id."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'");

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

	$trans_detail_delete = mysqli_query($conn, "DELETE FROM trans_details WHERE voucher_no='".$id."' AND flag = 'LHC' AND `branch_id`='".$branch_id."' AND `company_id`='".$company_id."'");
	
	$log = mysqli_query($conn, "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."')");
	echo json_encode($response);
}
?>