<?php
if (isset($_POST["action"])) {
	$output = '';
	require('../config/dbcon.php');
	if ($_POST["action"] == "Add") {
		
		$query = "SELECT srno from control_mst where description='SEGMENT'";
		$rs = mysqli_query($connect, $query) or die("Cannot execute query: $query\n");
		while ($row = mysqli_fetch_assoc($rs)) {
			$srno = $row['srno'];
			$srnoupd = $srno+1;
		}
		
		$segment_name = mysqli_escape_string($connect, $_POST["segment_name"]);
		$query = "INSERT INTO segment_mst(id,segment_name) VALUES ('" . $srno . "','" . $segment_name . "')";
		if (mysqli_query($connect, $query)) {
			
			$update = "update control_mst set srno = '" . $srnoupd . "' where description='SEGMENT'";
			mysqli_query($connect, $update) or die(mysqli_error());
			
			$query = "SELECT customer_code,customer_name,cust_place,acc_type from customer_mst"; 
		$rs = mysqli_query($connect, $query) or die("Cannot execute query: $query\n");
		while ($row = mysqli_fetch_assoc($rs)) {
			$customer_code = $row['customer_code'];
			$customer_name = $row['customer_name'];
			$cust_place = $row['cust_place'];
			$acc_type =  $row['acc_type'];
			$disc_per = '0';			
		$query = "SELECT srno from control_mst where description='DISC'"; 
		$rs1 = mysqli_query($connect, $query) or die("Cannot execute query: $query\n");
		while ($row1 = mysqli_fetch_assoc($rs1)) {
			$srno = $row1['srno']; 
			$srnoupd1 = $srno + 1;
			if($acc_type == "SDE"){
    			$sql = "insert into disc_mst(customer_code,customer_name,cust_place,segment,disc_per) values ('".$customer_code."','".$customer_name."','".$cust_place."','".$segment_name."',".$disc_per.")";
    			$result = mysqli_query($connect, $sql);
			}
		}
		$update="update control_mst set srno = '".$srnoupd1."' where description='DISC'";
		mysqli_query($connect, $update) or die(mysqli_error());
		}		
		$query = "SELECT supplier_code,supplier_name,supplier_place from supplier_mst"; 
		$rs = mysqli_query($connect, $query) or die("Cannot execute query: $query\n");
		while ($row = mysqli_fetch_assoc($rs)) {
			$supplier_code = $row['supplier_code'];
			$supplier_name = $row['supplier_name'];
			$supplier_place = $row['supplier_place'];
			$disc_per = '0';
			
		$query = "SELECT srno from control_mst where description='PDISC'";
		$rs1 = mysqli_query($connect, $query) or die("Cannot execute query: $query\n");
		while ($row1 = mysqli_fetch_assoc($rs1)) {
			$srno = $row1['srno']; 
			$srnoupd1 = $srno + 1;
			$sql = "insert into pdisc_mst(supplier_code,supplier_name,supplier_place,segment,disc_per) values ('".$supplier_code."','".$supplier_name."','".$supplier_place."','".$segment_name."',".$disc_per.")";
			$result = mysqli_query($connect, $sql);
		}
		
		$update="update control_mst set srno = '".$srnoupd1."' where description='PDISC'";
		mysqli_query($connect, $update) or die(mysqli_error());
		}
		}
	}
	if ($_POST["action"] == "Update") {
		$segment_name = mysqli_escape_string($connect, $_POST["segment_name"]);
		$update = "update segment_mst set segment_name = '" . $segment_name . "' WHERE id = '" . $_POST["id"] . "'";
		if (mysqli_query($connect, $update)) {
		}
	}
	if ($_POST["action"] == "Delete") {
		$delete = "DELETE FROM segment_mst WHERE id = '" . $_POST["id"] . "'";
		if (mysqli_query($connect, $delete)) {
		}
	}
}
