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

	    //GENERAL SALES
	    $gs       = mysqli_query($conn, "SELECT * FROM `general_purchase` WHERE `id` = '".$id."'");
	    $gs_row   = mysqli_fetch_assoc($gs);
	    $bill_no  = $gs_row['bill_no'];
	
	    //GENERAL LEDGER
	    $gl 			= "DELETE FROM general_ledger  WHERE voucher_no = '".$bill_no."' AND `flag` = 'GENERAL PURCHASE' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
	    $gl_res 		= mysqli_query($conn,$gl);
 		
 		// TRANS
 		$tra 			= "DELETE FROM trans  WHERE voucher_no = '".$bill_no."' AND `flag` = 'GENERAL PURCHASE' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
	    $tra_res 		= mysqli_query($conn,$tra);

	    // TRANS DETAILS
	    $trans_de 	    = "DELETE FROM trans_details  WHERE voucher_no = '".$bill_no."' AND `flag` = 'GENERAL PURCHASE' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
	    $result 		= mysqli_query($conn,$trans_de);

	    // GENERAL SALES
	    $bill = mysqli_query($conn, "DELETE FROM `general_purchase` WHERE `id` = '".$id."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'");

		$bill_detail = mysqli_query($conn, "DELETE FROM `general_purchase_details` WHERE `gs_id` = '".$id."'");

		//GENERAL SALES TAX
 		$gst = mysqli_query($conn, "DELETE FROM `general_purchase_tax_history` WHERE `gs_id` = '".$id."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'");

		
		// ACTIVITY LOG
		$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);
	}
?>