<?php
include("../config.php");
$response = array();
if ($_POST['delete']) {
	$id 			   = $_POST['delete'];
	$type              = 'cancel_bill';
    $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 billing WHERE invoice_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;
    }
   	
   	//UPDATE CANCEL FLAG
   	$update = mysqli_query($conn, "UPDATE `billing` SET `cancel_flag` = 'Y' WHERE invoice_no = '".$id."'");

    $gl 			   = "DELETE FROM general_ledger  WHERE voucher_no = '".$id."' AND `flag` = 'BILLING' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
    $gl_res 		   = mysqli_query($conn,$gl);
		
		$tra 			   = "DELETE FROM trans  WHERE voucher_no = '".$id."' AND `flag` = 'BILLING' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
    $tra_res 		   = mysqli_query($conn,$tra);

    $trans_de 	       = "DELETE FROM trans_details  WHERE voucher_no = '".$id."' AND `flag` = 'BILLING' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
    $result 		   = mysqli_query($conn,$trans_de);

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