<?php
if (isset($_POST["action"])) {
	$output = '';
	require('../config/dbcon.php');
	if ($_POST["action"] == "Add") {
		$query = "select srno from control_mst where description='PART'";
		$rs = mysqli_query($connect, $query) or die("Cannot execute query: $query\n");
		while ($row = mysqli_fetch_assoc($rs)) {
			$srno = $row['srno'];
			$srnoupd = $srno+1;
		}
		$partno = mysqli_escape_string($connect, $_POST["partno"]);
		$description_one = mysqli_escape_string($connect, $_POST["description_one"]);
		$description_two = mysqli_escape_string($connect, $_POST["description_two"]);
		$hsn_code = mysqli_escape_string($connect, $_POST["hsn_code"]);
		$list = mysqli_escape_string($connect, $_POST["list"]);
		$moq = mysqli_escape_string($connect, $_POST["moq"]);
		$boxqty = mysqli_escape_string($connect, $_POST["boxqty"]);
		$cost = mysqli_escape_string($connect, $_POST["cost"]);
		$disc_per = mysqli_escape_string($connect, $_POST["disc_per"]);
		$disc_per = mysqli_escape_string($connect, $_POST["disc_amt"]);
		$mrp = mysqli_escape_string($connect, $_POST["mrp"]);
		$sgst_per = mysqli_escape_string($connect, $_POST["sgst_per"]);
		$sgst_per = mysqli_escape_string($connect, $_POST["sgst_amt"]);
		$cgst_per = mysqli_escape_string($connect, $_POST["cgst_per"]);
		$cgst_amt = mysqli_escape_string($connect, $_POST["cgst_amt"]);
		$igst_per = mysqli_escape_string($connect, $_POST["igst_per"]);
		$igst_amt = mysqli_escape_string($connect, $_POST["igst_amt"]);
		$igst_amt = mysqli_escape_string($connect, $_POST["op_stk"]);
		$stk_qty = mysqli_escape_string($connect, $_POST["stk_qty"]);
		$stk_qty = mysqli_escape_string($connect, $_POST["stk_val"]);
		$unit = mysqli_escape_string($connect, $_POST["unit"]);
		$ly_sales = mysqli_escape_string($connect, $_POST["ly_sales"]);
		$cy_sales = mysqli_escape_string($connect, $_POST["cy_sales"]);
		$pending = mysqli_escape_string($connect, $_POST["pending"]);
		$dispatch1 = mysqli_escape_string($connect, $_POST["dispatch1"]);
		$min_qty = mysqli_escape_string($connect, $_POST["min_qty"]);
		$max_qty = mysqli_escape_string($connect, $_POST["max_qty"]);
		$reord_qty = mysqli_escape_string($connect, $_POST["reord_qty"]);
		$segment = mysqli_escape_string($connect, $_POST["segment"]);
		$remarks = mysqli_escape_string($connect, $_POST["remarks"]);
		$rec_qty = "0.00";
		$bal_qty = "0.00";
		$query = "insert into item_mst(id,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,bal_qty,rec_qty) VALUES ('" . $srno . "','" . $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 . "','" . $bal_qty . "','" . $rec_qty . "')";
		if (mysqli_query($connect, $query)) {
			$update = "update control_mst set srno = '" . $srnoupd . "' where description='PART'";
			mysqli_query($connect, $update) or die(mysqli_last_error());
		}
	}
	if ($_POST["action"] == "Update") {
		$partno = mysqli_escape_string($connect, $_POST["partno"]);
		$description_one = mysqli_escape_string($connect, $_POST["description_one"]);
		$description_two = mysqli_escape_string($connect, $_POST["description_two"]);
		$hsn_code = mysqli_escape_string($connect, $_POST["hsn_code"]);
		$list = mysqli_escape_string($connect, $_POST["list"]);
		$moq = mysqli_escape_string($connect, $_POST["moq"]);
		$boxqty = mysqli_escape_string($connect, $_POST["boxqty"]);
		$cost = mysqli_escape_string($connect, $_POST["cost"]);
		$disc_per = mysqli_escape_string($connect, $_POST["disc_per"]);
		$disc_amt = mysqli_escape_string($connect, $_POST["disc_amt"]);
		$mrp = mysqli_escape_string($connect, $_POST["mrp"]);
		$sgst_per = mysqli_escape_string($connect, $_POST["sgst_per"]);
		$sgst_amt = mysqli_escape_string($connect, $_POST["sgst_amt"]);
		$cgst_per = mysqli_escape_string($connect, $_POST["cgst_per"]);
		$cgst_amt = mysqli_escape_string($connect, $_POST["cgst_amt"]);
		$igst_per = mysqli_escape_string($connect, $_POST["igst_per"]);
		$igst_amt = mysqli_escape_string($connect, $_POST["igst_amt"]);
		$op_stk = mysqli_escape_string($connect, $_POST["op_stk"]);
		$stk_qty = mysqli_escape_string($connect, $_POST["stk_qty"]);
		$stk_val = mysqli_escape_string($connect, $_POST["stk_val"]);
		$unit = mysqli_escape_string($connect, $_POST["unit"]);
		$ly_sales = mysqli_escape_string($connect, $_POST["ly_sales"]);
		$cy_sales = mysqli_escape_string($connect, $_POST["cy_sales"]);
		$pending = mysqli_escape_string($connect, $_POST["pending"]);
		$dispatch1 = mysqli_escape_string($connect, $_POST["dispatch1"]);
		$min_qty = mysqli_escape_string($connect, $_POST["min_qty"]);
		$max_qty = mysqli_escape_string($connect, $_POST["max_qty"]);
		$reord_qty = mysqli_escape_string($connect, $_POST["reord_qty"]);
		$segment = mysqli_escape_string($connect, $_POST["segment"]);
		$remarks = mysqli_escape_string($connect, $_POST["remarks"]);
		$update = "update item_mst set 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 . "' WHERE id = '" . $_POST["id"] . "'";
		if (mysqli_query($connect, $update)) {
		}
	}
	if ($_POST["action"] == "Delete") {
		$delete = "delete from item_mst where id = '" . $_POST["id"] . "'";
		if (mysqli_query($connect, $delete)) {
		}
	}
}
