<?php
session_start();
include("../leadsnew/branch_connection.php");

/* ADD TAX */
if(isset($_POST['btn_add_tax'])){
    $taxname = trim($_POST['taxname']);
    $tax = $_POST['tax'];

    $chk = mysqli_query($conn,"SELECT tid FROM tax_master WHERE tax_name='$taxname'");
    if(mysqli_num_rows($chk)>0){
        echo "duplicate";
        exit;
    }

    mysqli_query($conn,"
        INSERT INTO tax_master (tax_name, tax_per)
        VALUES ('$taxname','$tax')
    ");
    echo "added";
    exit;
}

/* EDIT TAX */
if(isset($_POST['btn_edit_tax'])){
    $tid = (int)$_POST['tid'];
    $taxname = trim($_POST['taxname']);
    $tax = $_POST['tax'];

    $chk = mysqli_query($conn,"
        SELECT tid FROM tax_master 
        WHERE tax_name='$taxname' AND tid!=$tid
    ");
    if(mysqli_num_rows($chk)>0){
        echo "duplicate";
        exit;
    }

    mysqli_query($conn,"
        UPDATE tax_master
        SET tax_name='$taxname', tax_per='$tax' 
        WHERE tid=$tid
    ");
    echo "updated";
    exit;
}

/* DELETE TAX */
if(isset($_POST['btn_delete_tax'])){
    $tid = (int)$_POST['tid'];
    mysqli_query($conn,"DELETE FROM tax_master WHERE tid=$tid");
    echo "deleted";
    exit;
}
?>
