<?php
require('../config/dbcon.php');
    $query = "SELECT timeout from timeout_mst";
    $rs = mysqli_query($connect, $query) or die("Cannot execute query: $query\n");
    while ($row = mysqli_fetch_assoc($rs)) {
    	$timeout = $row['timeout'];
    }
    
    $query = "select screenwidth from width_mst";
    $rs = mysqli_query($connect, $query) or die("Cannot execute query: $query\n");
    while ($row = mysqli_fetch_assoc($rs)) {
    	$width = $row['screenwidth'];
    	$width1 = $width - '15';
    }
session_start();
if(isset($_SESSION["user"]))
{
 if((time() - $_SESSION['last_time']) > $timeout) // Time in Seconds
 {
 header("location:../login/logout.php");
 }
 else
 {
 $_SESSION['last_time'] = time();
 //echo "<h1 align='center'>Welcome ".$_SESSION["user"]. "</h1>";
 //echo "<h3 align='center'>Automatic Logout after 1 minute of inactive</h3>";
 //echo "<p align='center'><a href='../login/logout.php'>Logout</a></p>";
 //echo "<p align='center'><a href='../main/welcome.php'>Welcome1</a></p>";
 }
}
else
{
 header('Location:../login/index.php');
}
?>
<?php
require('../config/dbcon.php');
$query = "SELECT srno from control_mst where description='SUPPLIER'";
$rs = mysqli_query($connect, $query) or die("Cannot execute query: $query\n");
while ($row = mysqli_fetch_assoc($rs)) {
	$srno = $row['srno'];
}

$query = "SELECT company_st_code from company_mst";
$rs = mysqli_query($connect, $query) or die("Cannot execute query: $query\n");
while ($row = mysqli_fetch_assoc($rs)) {
	$company_st_code = $row['company_st_code'];
}
?>
<html>

<head>
	<title>IMS-Product Master</title>
</head>
<script type="text/javascript">
	function preventBack() {
		window.history.forward();
	}
	setTimeout("preventBack()", 0);
	window.onunload = function() {
		null
	};
</script>
<title>Item Master</title>
<link rel = "icon" href = "https:../image/ims.png" type = "image/x-icon">
<script src='../js/jquery-3.1.1.min.js' type='text/javascript'></script>
<script src='../js/jquery-ui.min.js' type='text/javascript'></script>
<link href="../css/style.css" rel="stylesheet" />
<link href="../css/style1.css" rel="stylesheet" />
<link href='../css/jquery-ui.min.css' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="../css/all.css">
<link href="../css/bootstrap.min.css" rel="stylesheet" />
<link href="../css/font-awesome.min.css" rel="stylesheet" />
<link href="../css/styleg.css" rel="stylesheet" />
<script type="text/javascript" src="../js/jquery.placeholder.label.js"></script>
<style>
    label {
        font-size: 15px;
        top: 11px;
        margin-bottom: .5rem;
        margin-left: 5px;
        font-weight:bold;
        display: inline-transparent;
    }
</style>
<body style="min-width: <?php echo $width; ?>; width: auto;">
<div style="min-width: <?php echo $width; ?>; width: auto;">
     <nav class="navbar navbar-expand-lg navbar-dark bg-primary" style="min-width: <?php echo $width; ?>; width: auto; height: 62px; background-color: #007bff; font-size:24px;">
        <input style = "background-color: #007bff; font-size:24px; width:100px; height : 55px; margin-top : px;" type="button" onclick="location.href='../main/index';" value="Home">
        <input style = "background-color: #007bff; font-size:24px; width:200px; height : 55px; margin-top : px;" type="button" onclick="location.href='../invoice/index';" value="Go to Invoice">
        <input style = "background-color: #007bff; font-size:24px; width:200px; height : 55px; margin-top : px;" type="button" onclick="location.href='../pruchase/index';" value="Go to Purchase">
        <input style = "background-color: #007bff; font-size:24px; height : 55px; margin-top : px;" type="button" onclick="exportToExcel('tblexportData', 'Export-data')" value="Export to Excel">
        <h1 style="color:white; font-size:24px; margin-top: -40px;" class="text-primary" align="center">Product Master</h1>
        <h1 style="color:white; font-size:24px; margin-top : -40px;margin-right :20px;" class="text-primary" id="demo2" align="right"></h1>
    </nav>
	<div class="container" style="min-width: <?php echo $width; ?>; width: auto; margin-left: 0px; margin-top: -20px;">
		<center>
			<tr>
			    <div class="col-xs-2">
			        <label style="font-size:18px;" class=" col-sm-2 control-label">partno</label>
					<input type="text" id="partno" class="roundedCategory" style="font-size:18px; width:100%; height :40px; font-weight:bold; color: #000066;" placeholder="" oninput="this.value = this.value.toUpperCase()" autocomplete="off" required autofocus>
				</div>
				<div class="col-xs-4">
			        <label style="font-size:18px;" class=" col-sm-2 control-label">description_one</label>
					<input type="text" id="description_one" class="roundedCategory" style="font-size:18px; width:100%; height :40px; font-weight:bold; color: #000066;" placeholder="" oninput="this.value = this.value.toUpperCase()" autocomplete="off" required autofocus>
				</div>
				<div class="col-xs-2">
			        <label style="font-size:18px;" class=" col-sm-2 control-label">description_two</label>
					<input type="text" id="description_two" class="roundedCategory" style="font-size:18px; width:100%; height :40px; font-weight:bold; color: #000066;" placeholder="" oninput="this.value = this.value.toUpperCase()" autocomplete="off" required autofocus>
				</div>
				<div class="col-xs-1">
			        <label style="font-size:18px;" class=" col-sm-2 control-label">hsn_code</label>
					<input type="text" id="hsn_code" class="roundedCategory" style="font-size:18px; width:100%; height :40px; font-weight:bold; color: #000066;" placeholder="" oninput="this.value = this.value.toUpperCase()" autocomplete="off" required autofocus>
				</div>
				<div class="col-xs-1">
			        <label style="font-size:18px;" class=" col-sm-2 control-label">list</label>
					<input type="text" id="list" class="roundedCategory" style="font-size:18px; width:100%; height :40px; font-weight:bold; color: #000066;" placeholder="" onkeypress="return isNumberKey(this, event);" autocomplete="off" required autofocus>
				</div>
				<div class="col-xs-1">
			        <label style="font-size:18px;" class=" col-sm-2 control-label">disc_per</label>
					<input type="text" id="disc_per" class="roundedCategory" style="font-size:18px; width:100%; height :40px; font-weight:bold; color: #000066;" placeholder="" onkeypress="return isNumberKey(this, event);" autocomplete="off" required autofocus>
				</div>
				<div class="col-xs-1">
			        <label style="font-size:18px;" class=" col-sm-2 control-label">disc_amt</label>
					<input type="text" readonly id="disc_amt" class="roundedCategory" style="font-size:18px; height :40px; width:100%; font-weight:bold; color: #000066;" placeholder="" onkeypress="return isNumberKey(this, event);" autocomplete="off" required autofocus>
				</div>
			</tr>
		</center>
		<center>
			<tr>
			    <div class="col-xs-1">
			        <label style="font-size:18px;" class=" col-sm-2 control-label">cost</label>
					<input type="text" readonly id="cost" class="roundedCategory" style="font-size:18px; width:100%; height :40px; font-weight:bold; color: #000066;" placeholder="" onkeypress="return isNumberKey(this, event);" autocomplete="off" required autofocus>
				</div>
				<div class="col-xs-1">
			        <label style="font-size:18px;" class=" col-sm-2 control-label">stk_qty</label>
					<input type="text" id="stk_qty" class="roundedCategory" style="font-size:18px; width:100%; height :40px; font-weight:bold; color: #000066;" placeholder="" onkeypress="return isNumberKey(this, event);" autocomplete="off" required autofocus>
				</div>
				<div class="col-xs-1">
			        <label style="font-size:18px;" class=" col-sm-2 control-label">moq</label>
					<input type="text" id="moq" class="roundedCategory" style="font-size:18px; width:100%; height :40px; font-weight:bold; color: #000066;" placeholder="" onkeypress="return isNumberKey(this, event);" autocomplete="off" required autofocus>
				</div>
				<div class="col-xs-1">
			        <label style="font-size:18px;" class=" col-sm-2 control-label">boxqty</label>
					<input type="text" id="boxqty" class="roundedCategory" style="font-size:18px; width:100%; height :40px; font-weight:bold; color: #000066;" placeholder="" onkeypress="return isNumberKey(this, event);" autocomplete="off" required autofocus>
				</div>
				<div class="col-xs-1">
			        <label style="font-size:18px;" class=" col-sm-2 control-label">mrp</label>
					<input type="text" id="mrp" class="roundedCategory" style="font-size:18px; width:100%; height :40px; font-weight:bold; color: #000066;" placeholder="" onkeypress="return isNumberKey(this, event);" autocomplete="off" required autofocus>
				</div>
				<div class="col-xs-1">
			        <label style="font-size:18px;" class=" col-sm-2 control-label">sgst_per</label>
					<input type="text" id="sgst_per" class="roundedCategory" style="font-size:18px; width:100%; height :40px; font-weight:bold; color: #000066;" placeholder="" onkeypress="return isNumberKey(this, event);" autocomplete="off" required autofocus>
				</div>
				<div class="col-xs-1">
			        <label style="font-size:18px;" class=" col-sm-2 control-label">sgst_amt</label>
					<input type="text" id="sgst_amt" class="roundedCategory" style="font-size:18px; width:100%; height :40px; font-weight:bold; color: #000066;" placeholder="" onkeypress="return isNumberKey(this, event);" autocomplete="off" required autofocus>
				</div>
				<div class="col-xs-1">
			        <label style="font-size:18px;" class=" col-sm-2 control-label">cgst_per</label>
					<input type="text" readonly id="cgst_per" class="roundedCategory" style="font-size:18px; width:100%; height :40px; font-weight:bold; color: #000066;" placeholder="" onkeypress="return isNumberKey(this, event);" autocomplete="off" required autofocus>
				</div>
				<div class="col-xs-1">
			        <label style="font-size:18px;" class=" col-sm-2 control-label">cgst_amt</label>
					<input type="text" readonly id="cgst_amt" class="roundedCategory" style="font-size:18px; width:100%; height :40px; font-weight:bold; color: #000066;" placeholder="" onkeypress="return isNumberKey(this, event);" autocomplete="off" required autofocus>
				</div>
				<div class="col-xs-1">
			        <label style="font-size:18px;" class=" col-sm-2 control-label">igst_per</label>
					<input type="text" readonly id="igst_per" class="roundedCategory" style="font-size:18px; width:100%; height :40px; font-weight:bold; color: #000066;" placeholder="" onkeypress="return isNumberKey(this, event);" autocomplete="off" required autofocus>
				</div>
				<div class="col-xs-1">
			        <label style="font-size:18px;" class=" col-sm-2 control-label">igst_amt</label>
					<input type="text" readonly id="igst_amt" class="roundedCategory" style="font-size:18px; width:100%; height :40px; font-weight:bold; color: #000066;" placeholder="" onkeypress="return isNumberKey(this, event);" autocomplete="off" required autofocus>
				</div>
				<div class="col-xs-1">
			        <label style="font-size:18px;" class=" col-sm-2 control-label">op_stk</label>
					<input type="text" id="op_stk" class="roundedCategory" style="font-size:18px; width:100%; height :40px; font-weight:bold; color: #000066;" placeholder="" onkeypress="return isNumberKey(this, event);" autocomplete="off" required autofocus>
				</div>
			</tr>
		</center>
		<center>
			<tr>
			    <div class="col-xs-1">
			        <label style="font-size:18px;" class=" col-sm-2 control-label">stk_val</label>
					<input type="text" id="stk_val" class="roundedCategory" style="font-size:18px; width:100%; height :40px; font-weight:bold; color: #000066;" placeholder="" onkeypress="return isNumberKey(this, event);" autocomplete="off" required autofocus>
				</div>
				<div class="col-xs-1">
			        <label style="font-size:18px;" class=" col-sm-2 control-label">unit</label>
					<input type="text" id="unit" class="roundedCategory" style="font-size:18px; width:100%; height :40px; font-weight:bold; color: #000066;" placeholder="" oninput="this.value = this.value.toUpperCase()" autocomplete="off" required autofocus>
				</div>
				<div class="col-xs-1">
			        <label style="font-size:18px;" class=" col-sm-2 control-label">ly_sales</label>
					<input type="text" id="ly_sales" class="roundedCategory" style="font-size:18px; width:100%; height :40px; font-weight:bold; color: #000066;" placeholder="" onkeypress="return isNumberKey(this, event);" autocomplete="off" required autofocus>
				</div>
				<div class="col-xs-1">
			        <label style="font-size:18px;" class=" col-sm-2 control-label">cy_sales</label>
					<input type="text" id="cy_sales" class="roundedCategory" style="font-size:18px; width:100%; height :40px; font-weight:bold; color: #000066;" placeholder="" onkeypress="return isNumberKey(this, event);" autocomplete="off" required autofocus>
				</div>
				<div class="col-xs-1">
			        <label style="font-size:18px;" class=" col-sm-2 control-label">pending</label>
					<input type="text" id="pending" class="roundedCategory" style="font-size:18px; width:100%; height :40px; font-weight:bold; color: #000066;" placeholder="" onkeypress="return isNumberKey(this, event);" autocomplete="off" required autofocus>
				</div>
				<div class="col-xs-1">
			        <label style="font-size:18px;" class=" col-sm-2 control-label">dispatch1</label>
					<input type="text" id="dispatch1" class="roundedCategory" style="font-size:18px; width:100%; height :40px; font-weight:bold; color: #000066;" placeholder="" onkeypress="return isNumberKey(this, event);" autocomplete="off" required autofocus>
				</div>
				<div class="col-xs-1">
			        <label style="font-size:18px;" class=" col-sm-2 control-label">min_qty</label>
					<input type="text" id="min_qty" class="roundedCategory" style="font-size:18px; width:100%; height :40px; font-weight:bold; color: #000066;" placeholder="" onkeypress="return isNumberKey(this, event);" autocomplete="off" required autofocus>
				</div>
				<div class="col-xs-1">
			        <label style="font-size:18px;" class=" col-sm-2 control-label">max_qty</label>
					<input type="text" id="max_qty" class="roundedCategory" style="font-size:18px; width:100%; height :40px; font-weight:bold; color: #000066;" placeholder="" onkeypress="return isNumberKey(this, event);" autocomplete="off" required autofocus>
				</div>
				<div class="col-xs-1">
			        <label style="font-size:18px;" class=" col-sm-2 control-label">reord_qty</label>
					<input type="text" id="reord_qty" class="roundedCategory" style="font-size:18px; width:100%; height :40px; font-weight:bold; color: #000066;" placeholder="" onkeypress="return isNumberKey(this, event);" autocomplete="off" required autofocus>
				</div>
				<div class="col-xs-2">
			        <label style="font-size:18px;" class=" col-sm-2 control-label">segment</label>
					<input type="text" id="segment" class="roundedCategory" style="font-size:18px; width:100%; height :40px; font-weight:bold; color: #000066;" placeholder="" oninput="this.value = this.value.toUpperCase()" autocomplete="off" required autofocus>
				</div>
				<div class="col-xs-1">
			        <label style="font-size:18px;" class=" col-sm-2 control-label">remarks</label>
					<input type="text" id="remarks" class="roundedCategory" style="font-size:18px; width:100%; height :40px; font-weight:bold; color: #000066;" placeholder="" oninput="this.value = this.value.toUpperCase()" autocomplete="off" required autofocus>
				</div>
			</tr>
		</center>
		<br>
		<center>
			<tr>
				<input type="hidden" name="user_id" id="user_id" />
				<input style = "background-color: #007bff; height : 45px; margin-top : 0px;  margin-left : 0px;" type="button" name="action" id="action" value="Add">
			</tr>
		</center>
		<div style="min-width: <?php echo $width; ?>; width: auto; margin-left : 0px; margin-top : 10px; height: 45px;" class=container">
    		<div class="form-group">
    		    <center>
        			<div class="input-group">
        			    <input style ="margin-left : 0px; width : 1800px;  height :40px; font-size:20px; font-weight:bold;" name="search_text" id="search_text" type="text" placeholder="search" alt="search">
        			</div>
    			</center>
    		</div>
    	</div>
    	<br>
		<center>
			<tr>
				<div style="margin-top : 0px; margin-left : 10px;min-width: <?php echo $width; ?>; width: auto; font-size:20px; margin-left: 0px; margin-top: 0px; overflow:auto;" id="result" class="table-responsive"></div>
			</tr>
		</center>
		<div style="height: 60px; margin-left:0px; margin-top:0px; font-size:30px;" class="success"></div>
		<div style="height: 60px; margin-left:0px; margin-top:0px; font-size:30px;" class="error"></div>
		</div>
</body>
</html>
<script type="text/javascript">
    function isNumberKey(txt, evt) {
        var charCode = (evt.which) ? evt.which : evt.keyCode;
        if (charCode == 46) {
            if (txt.value.indexOf('.') === -1) {
              return true;
            } else {
              return false;
            }
        } else {
        if (charCode > 31 &&
            (charCode < 48 || charCode > 57))
            return false;
        }
        return true;
    }
</script>
<?php
	$date = new DateTime();
	$current_timestamp = $date->getTimestamp();
?>
<script>
    function jump() {
var key = window.event.keyCode;

if (key ==106) {
// alert(window.event.altKey && 40);

window.event.keyCode = 9;

}

}
</script>
<script>
    $(document).on('keyup', function(e) {
        if (e.key == "Escape"){
          window.location.href = "../main/index"
        }
    });
</script>
<script>
	$(document).keydown(function(objEvent) {
		if (objEvent.keyCode == 9) {
			objEvent.preventDefault();
		}
	})
	flag_time = true;
	timer = '';
	setInterval(function(){phpJavascriptClock();},1000);
	
	function phpJavascriptClock()
	{
		if ( flag_time ) {
		timer = <?php echo $current_timestamp;?>*1000;
		}
		var d = new Date(timer);
		months = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec');
		
		month_array = new Array('January', 'Febuary', 'March', 'April', 'May', 'June', 'July', 'Augest', 'September', 'October', 'November', 'December');
		
		day_array = new Array( 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
		
		currentYear = d.getFullYear();
		month = d.getMonth();
		var currentMonth = months[month];
		var currentMonth1 = month_array[month];
		var currentDate = d.getDate();
		currentDate = currentDate < 10 ? '0'+currentDate : currentDate;
		
		var day = d.getDay();
		current_day = day_array[day];
		var hours = d.getHours();
		var minutes = d.getMinutes();
		var seconds = d.getSeconds();
		
		var ampm = hours >= 12 ? 'PM' : 'AM';
		hours = hours % 12;
		hours = hours ? hours : 12; // the hour ’0′ should be ’12′
		minutes = minutes < 10 ? '0'+minutes : minutes;
		seconds = seconds < 10 ? '0'+seconds : seconds;
		var strTime = hours + ':' + minutes+ ':' + seconds + ' ' + ampm;
		timer = timer + 1000;
		document.getElementById("demo2").innerHTML='Date : ' + currentDate+'-' +(month+1)+'-' +currentYear + ' Time : ' + strTime ;
		flag_time = false;
	}
</script>
<script>
	$(document).ready(function() {
		load_data();

		function load_data(query) {
			$.ajax({
				url: "search",
				method: "post",
				data: {
					query: query
				},
				success: function(data) {
					$('#result').html(data);
					$('#partno').val('');
					$('#description_one').val('');
					$('#description_two').val('');
					$('#hsn_code').val('');
					$('#list').val('');
					$('#moq').val('');
					$('#boxqty').val('');
					$('#cost').val('');
					$('#disc_per').val('');
					$('#disc_amt').val('');
					$('#mrp').val('');
					$('#sgst_per').val('');
					$('#sgst_amt').val('');
					$('#cgst_per').val('');
					$('#cgst_amt').val('');
					$('#igst_per').val('');
					$('#igst_amt').val('');
					$('#op_stk').val('');
					$('#stk_qty').val('');
					$('#stk_val').val('');
					$('#unit').val('');
					$('#ly_sales').val('');
					$('#cy_sales').val('');
					$('#pending').val('');
					$('#dispatch1').val('');
					$('#min_qty').val('');
					$('#max_qty').val('');
					$('#reord_qty').val('');
					$('#segment').val('');
					$('#remarks').val('');
					$('#action').text("Add");
				}
			});
		}
		$('#search_text').keyup(function() {
			var search = $(this).val();
			if (search !== '') {
				load_data(search);
			} else {
				load_data();
			}
		});
		$(function() {
			$("#supp_st_code").autocomplete({
				source: function(request, response) {
					$.ajax({
						url: "fetchcustomer",
						type: 'post',
						dataType: "json",
						data: {
							search: request.term
						},
						success: function(data) {
							response(data);
						}
					});
				},
				select: function(event, ui) {
					$('#supp_st_name').val(ui.item.label);
					$('#supp_st_code').val(ui.item.value);
					$('#supp_gstno').val(ui.item.value);
					document.getElementById("supp_gstno").focus();
					return false;
				}
			});
		});
		$('#action').click(function() {
			var partno = $('#partno').val();
			var description_one = $('#description_one').val();
			var description_two = $('#description_two').val();
			var hsn_code = $('#hsn_code').val();
			var list = $('#list').val();
			var moq = $('#moq').val();
			var boxqty = $('#boxqty').val();
			var cost = $('#cost').val();
			var disc_per = $('#disc_per').val();
			var disc_amt = $('#disc_amt').val();
			var mrp = $('#mrp').val();
			var sgst_per = $('#sgst_per').val();
			var sgst_amt = $('#sgst_amt').val();
			var cgst_per = $('#cgst_per').val();
			var cgst_amt = $('#cgst_amt').val();
			var igst_per = $('#igst_per').val();
			var igst_amt = $('#igst_amt').val();
			var op_stk = $('#op_stk').val();
			var stk_qty = $('#stk_qty').val();
			var stk_val = $('#stk_val').val();
			var unit = $('#unit').val();
			var ly_sales = $('#ly_sales').val();
			var cy_sales = $('#cy_sales').val();
			var pending = $('#pending').val();
			var dispatch1 = $('#dispatch1').val();
			var min_qty = $('#min_qty').val();
			var max_qty = $('#max_qty').val();
			var reord_qty = $('#reord_qty').val();
			var segment = $('#segment').val();
			var remarks = $('#remarks').val();
			var id = $('#user_id').val();
			var action = $('#action').text();
			$actiontext = $('#action').text();
			if (partno != '' && description_one != '' && description_two != '' && hsn_code != '' && list != '' && moq != '' && boxqty != '' && cost != '' && disc_per != '' && disc_amt != '' && mrp != '' && sgst_per != '' && sgst_amt != '' && cgst_per != '' && cgst_amt != '' && igst_per != '' && igst_amt != '' && op_stk != '' && stk_qty != '' && stk_val != '' && unit != '' && ly_sales != '' && cy_sales != '' && pending != '' && dispatch1 != '' && min_qty != '' && max_qty != '' && reord_qty != '' && segment != '' && remarks != '') {
				$.ajax({
					url: "action",
					method: "POST",
					data: {
						partno: partno,
						description_one: description_one,
						description_two: description_two,
						hsn_code: hsn_code,
						list: list,
						moq: moq,
						boxqty: boxqty,
						cost: cost,
						disc_per: disc_per,
						disc_amt: disc_amt,
						mrp: mrp,
						sgst_per: sgst_per,
						sgst_amt: sgst_amt,
						cgst_per: cgst_per,
						cgst_amt: cgst_amt,
						igst_per: igst_per,
						igst_amt: igst_amt,
						op_stk: op_stk,
						stk_qty: stk_qty,
						stk_val: stk_val,
						unit: unit,
						ly_sales: ly_sales,
						cy_sales: cy_sales,
						pending: pending,
						dispatch1: dispatch1,
						min_qty: min_qty,
						max_qty: max_qty,
						reord_qty: reord_qty,
						segment: segment,
						remarks: remarks,
						id: id,
						action: action
					},
					success: function(data) {						
						$('.success').show(1000).html("Record Saved Successfully.").delay(1000).fadeOut(1000);
						load_data();
						$('#partno').focus();
					}
				});
			} else {
				$('.success').show(1000).html("All Fields are Required").delay(1000).fadeOut(1000);
			}
		});

		$(document).on('click', '.update', function() {
			var id = $(this).attr("id");
			$.ajax({
				url: "fetch",
				method: "POST",
				data: {
					id: id
				},
				dataType: "json",
				success: function(data) {
					$('#action').text("Update");
					$('#user_id').val(id);
					$('#partno').val(data.partno);
					$('#description_one').val(data.description_one);
					$('#description_two').val(data.description_two);
					$('#hsn_code').val(data.hsn_code);
					$('#list').val(data.list);
					$('#moq').val(data.moq);
					$('#boxqty').val(data.boxqty);
					$('#cost').val(data.cost);
					$('#disc_per').val(data.disc_per);
					$('#disc_amt').val(data.disc_amt);
					$('#mrp').val(data.mrp);
					$('#sgst_per').val(data.sgst_per);
					$('#sgst_amt').val(data.sgst_amt);
					$('#cgst_per').val(data.cgst_per);
					$('#cgst_amt').val(data.cgst_amt);
					$('#igst_per').val(data.igst_per);
					$('#igst_amt').val(data.igst_amt);
					$('#op_stk').val(data.op_stk);
					$('#stk_qty').val(data.stk_qty);
					$('#stk_val').val(data.stk_val);
					$('#unit').val(data.unit);
					$('#ly_sales').val(data.ly_sales);
					$('#cy_sales').val(data.cy_sales);
					$('#pending').val(data.pending);
					$('#dispatch1').val(data.dispatch1);
					$('#min_qty').val(data.min_qty);
					$('#max_qty').val(data.max_qty);
					$('#reord_qty').val(data.reord_qty);
					$('#segment').val(data.segment);
					$('#remarks').val(data.remarks);
				}
			})
		});
		$(document).on('click', '.delete', function() {
			var id = $(this).attr("id");
			if (confirm("Are you sure you want to remove this data?")) {
				var action = "Delete";
				$.ajax({
					url: "action",
					method: "POST",
					data: {
						id: id,
						action: action
					},
					success: function(data) {
						$('.success').show(1000).html("Record Deleted Successfully.").delay(1000).fadeOut(1000);
						load_data();
						$('#partno').focus();
					}
				})
			} else {
				return false;
			}
		});
		//To check the input is not empty and send the cursor to next input.
		$(document).ready(function() {
			$('input').on('keypress', function(e) {
				if (e.which == 13) {
					switch ($(this).attr('id')) {
						case 'partno':
							if (!document.getElementById("partno").value.length == 0) {
								$('#description_one').focus();
								e.preventDefault();
								break;
							}
							case 'description_one':
								if (!document.getElementById("description_one").value.length == 0) {
									$('#description_two').focus();
									e.preventDefault();
									break;
								}
								case 'description_two':
									if (!document.getElementById("description_two").value.length == 0) {
										$('#hsn_code').focus();
										e.preventDefault();
										break;
									}
									case 'hsn_code':
										if (!document.getElementById("hsn_code").value.length == 0) {
											$('#list').focus();
											e.preventDefault();
											break;
										}
										case 'list':
											if (!document.getElementById("list").value.length == 0) {
												$('#disc_per').focus();
												e.preventDefault();
												break;
											}
											case 'disc_per':
												if (!document.getElementById("disc_per").value.length == 0) {
													$('#stk_qty').focus();
													e.preventDefault();
													break;
												}
												case 'stk_qty':
													if (!document.getElementById("stk_qty").value.length == 0) {
														$('#moq').focus();
														e.preventDefault();
														break;
													}
													case 'moq':
														if (!document.getElementById("moq").value.length == 0) {
															$('#boxqty').focus();
															e.preventDefault();
															break;
														}
														case 'boxqty':
															if (!document.getElementById("boxqty").value.length == 0) {
																$('#mrp').focus();
																e.preventDefault();
																break;
															}
															case 'mrp':
																if (!document.getElementById("mrp").value.length == 0) {
																	$('#sgst_per').focus();
																	e.preventDefault();
																	break;
																}
																case 'sgst_per':
																	if (!document.getElementById("sgst_per").value.length == 0) {
																		$('#op_stk').focus();
																		e.preventDefault();
																		break;
																	}
																	case 'op_stk':
																		if (!document.getElementById("op_stk").value.length == 0) {
																			$('#stk_val').focus();
																			e.preventDefault();
																			break;
																		}
																		case 'stk_val':
																			if (!document.getElementById("stk_val").value.length == 0) {
																				$('#unit').focus();
																				e.preventDefault();
																				break;
																			}
																			case 'unit':
																				if (!document.getElementById("unit").value.length == 0) {
																					$('#ly_sales').focus();
																					e.preventDefault();
																					break;
																				}
																				case 'ly_sales':
																					if (!document.getElementById("ly_sales").value.length == 0) {
																						$('#cy_sales').focus();
																						e.preventDefault();
																						break;
																					}
																					case 'cy_sales':
																						if (!document.getElementById("cy_sales").value.length == 0) {
																							$('#pending').focus();
																							e.preventDefault();
																							break;
																						}
																						case 'pending':
																							if (!document.getElementById("pending").value.length == 0) {
																								$('#dispatch1').focus();
																								e.preventDefault();
																								break;
																							}
																							case 'dispatch1':
																								if (!document.getElementById("dispatch1").value.length == 0) {
																									$('#min_qty').focus();
																									e.preventDefault();
																									break;
																								}
																								case 'min_qty':
																									if (!document.getElementById("min_qty").value.length == 0) {
																										$('#max_qty').focus();
																										e.preventDefault();
																										break;
																									}
																									case 'max_qty':
																										if (!document.getElementById("max_qty").value.length == 0) {
																											$('#reord_qty').focus();
																											e.preventDefault();
																											break;
																										}
																										case 'reord_qty':
																											if (!document.getElementById("reord_qty").value.length == 0) {
																												$('#segment').focus();
																												e.preventDefault();
																												break;
																											}
																											case 'segment':
																												if (!document.getElementById("segment").value.length == 0) {
																													$('#remarks').focus();
																													e.preventDefault();
																													break;
																												}
																												case 'remarks':
																													if (!document.getElementById("remarks").value.length == 0) {
																														$('#action').focus();
																														e.preventDefault();
																														break;
																													}
					}
				}
			});
		});
	});
</script>
<script type="text/javascript">
	function exportToExcel(tableID, filename = '') {
		var downloadurl;
		var dataFileType = 'application/vnd.ms-excel';
		var tableSelect = document.getElementById(tableID);
		var tableHTMLData = tableSelect.outerHTML.replace(/ /g, '%20');

		// Specify file name
		filename = filename ? filename + '.xls' : 'export_excel_data.xls';

		// Create download link element
		downloadurl = document.createElement("a");

		document.body.appendChild(downloadurl);

		if (navigator.msSaveOrOpenBlob) {
			var blob = new Blob(['\ufeff', tableHTMLData], {
				type: dataFileType
			});
			navigator.msSaveOrOpenBlob(blob, filename);
		} else {
			// Create a link to the file
			downloadurl.href = 'data:' + dataFileType + ', ' + tableHTMLData;

			// Setting the file name
			downloadurl.download = filename;

			//triggering the function
			downloadurl.click();
		}
	}
</script>
<script>
	$(function() {
		$("#segment").autocomplete({
			source: function(request, response) {
				$.ajax({
					url: "fetchsegment",
					type: 'post',
					dataType: "json",
					data: {
						search: request.term
					},
					success: function(data) {
						response(data);
					}
				});
			},
			select: function(event, ui) {
				$('#segment').val(ui.item.label);
				return false;
			}
		});
	});
	$(document).ready(function() {
		$('#disc_per').keyup(function(ev) {
			var listprice = Number(document.getElementById("list").value);
			var discper = Number(document.getElementById("disc_per").value) / 100;
			var totalValue = listprice - (listprice * discper)
			var discamt = (listprice * discper)
			document.getElementById("disc_amt").value = discamt.toFixed(2);
			document.getElementById("cost").value = totalValue.toFixed(2);
			var sqkqty = Number(document.getElementById("stk_qty").value);
			var stkval = (sqkqty * costprice)
			document.getElementById("stk_val").value = stkval.toFixed(2);

		});
		$('#sgst_per').keyup(function(ev) {
			var costprice = Number(document.getElementById("cost").value);
			var sgstper = Number(document.getElementById("sgst_per").value);
			var cgstper = Number(document.getElementById("sgst_per").value);
			var igstper = sgstper + cgstper;


			var sgstamt = (costprice * sgstper) / 100;
			var cgstamt = (costprice * cgstper) / 100;
			var igstamt = (costprice * igstper) / 100;

			var sqkqty = Number(document.getElementById("stk_qty").value);
			var stkval = (sqkqty * costprice)
			document.getElementById("stk_val").value = stkval.toFixed(2);

			document.getElementById("cgst_per").value = cgstper.toFixed(2);
			document.getElementById("igst_per").value = igstper.toFixed(2);
			document.getElementById("sgst_amt").value = sgstamt.toFixed(2);
			document.getElementById("cgst_amt").value = cgstamt.toFixed(2);
			document.getElementById("igst_amt").value = igstamt.toFixed(2);
		});
		$('#stk_qty').keyup(function(ev) {
			var sqkqty = Number(document.getElementById("stk_qty").value);
			var costprice = Number(document.getElementById("cost").value);
			var stkval = (sqkqty * costprice)
			document.getElementById("stk_val").value = stkval.toFixed(2);

		});
	});
</script>