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

    $query = mysqli_query($conn, "SELECT * FROM `credit_note` WHERE `id` = '".$id."'");
    $rows  = mysqli_fetch_assoc($query);
    $code = $rows['code'];
    $id   = $rows['id'];

	$gl_delete = mysqli_query($conn, "DELETE FROM `general_ledger` WHERE `voucher_no` = '".$code."' AND (`flag` = 'CREDIT NOTE' OR `mark` = 'CREDIT NOTE')");
		
	$trans_delete = mysqli_query($conn, "DELETE FROM `trans` WHERE `voucher_no` = '".$code."' AND `flag` = 'CREDIT NOTE'");

	$detail_delete = mysqli_query($conn, "DELETE FROM `credit_note_details` WHERE `master_id` = '".$id."'");

	$tax_delete = mysqli_query($conn, "DELETE FROM `credit_note_tax_details` WHERE `master_id` = '".$id."'");
	
	$delete = mysqli_query($conn, "DELETE FROM `credit_note` WHERE `id` = '".$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);
}
?>