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

if ($_POST['delete']) {
	$id 		= $_POST['delete'];
	$type       = 'delete_lhc_balance';
	$user_id    = $_SESSION['user_detail']['id'];
	$data       = serialize($_POST);
	$company_id = $_SESSION['company_id'];
  	$branch_id  = $_SESSION['branch_id'];

	$select = mysqli_query($conn, "SELECT * FROM `lhc_balance_history` WHERE `id` = '".$id."'");
	$rows 	= mysqli_fetch_assoc($select);
	$lhc_no = $rows['lhc_no'];
	$adjust = $rows['adj_amnt'];

	$lhp = mysqli_query($conn, "SELECT * FROM lhc_payment_status WHERE lhc_no = '".$lhc_no."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."' AND flag = 'LHC BALANCE' AND master_id = '".$id."'");
	$vou = array();
    while ($lhch_row = mysqli_fetch_assoc($lhp)) {
    	$vou[] = $lhch_row;
    }
    foreach ($vou as $key => $vou) {
    	if($vou['payment_type'] == 'CASH'){
    	   $gl = mysqli_query($conn, "DELETE FROM `general_ledger` WHERE `voucher_serial` = '".$vou['lhc_no']."' AND (flag = 'C') AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'");
    	}else{
    	   $gl = mysqli_query($conn, "DELETE FROM `general_ledger` WHERE `voucher_serial` = '".$vou['lhc_no']."' AND (flag = 'B') AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'");
    	}   
    	$payment = mysqli_query($conn, "DELETE FROM `payment` WHERE `voucher_no` = '".$vou['voucher_no']."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'");
    	$lpsd = mysqli_query($conn, "DELETE FROM `lhc_payment_status` WHERE `lhc_no` ='".$lhc_no."' AND `flag` = 'LHC BALANCE' AND id = '".$vou['id']."'");

    	$lbhd = mysqli_query($conn, "DELETE FROM `lhc_balance_history` WHERE `id` = '".$vou['master_id']."'");
    }

    //UPDATE LHC DATA
    $lhc     = mysqli_query($conn, "SELECT * FROM `lhc` WHERE `lhc_no` = '".$lhc_no."'");
    $lhc_row = mysqli_fetch_assoc($lhc);
    $balance = $lhc_row['balance'] + $adjust;
   	
   	$update_lhc = mysqli_query($conn, "UPDATE `lhc` SET `balance` = '".$balance."' WHERE `lhc_no` = '".$lhc_no."' AND `company_id` = '".$company_id."' AND `branch_id` = '".$branch_id."'");

   	//UPDATE TRANS
   	$select_trans = mysqli_query($conn, "SELECT * FROM `trans` WHERE `voucher_no`='".$lhc_no."' AND flag = 'LHC' AND `branch_id`='".$branch_id."' AND `company_id`='".$company_id."'");
   	$trans_row = mysqli_fetch_assoc($select_trans);
   	$new_adjust = $trans_row['adjust'] - $adjust;
   	
	$trans = mysqli_query($conn, "UPDATE `trans` SET `adjust`='".$new_adjust."',`balance`='".$balance."' WHERE `voucher_no`='".$lhc_no."' AND flag = 'LHC' AND `branch_id`='".$branch_id."' AND `company_id`='".$company_id."'");   	

	$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);
	echo json_encode($response);
}
?>