<?php
if (isset($_POST["action"])) {
	$output = '';
	require('../config/dbcon.php');
	if ($_POST["action"] == "Add") {
	    $acc_type = mysqli_escape_string($connect, $_POST["acc_type"]);
		$supplier_code = mysqli_escape_string($connect, $_POST["supplier_code"]);
		$srnoupd = $_POST['supplier_code'] + 1;
		$supplier_name = mysqli_escape_string($connect, $_POST["supplier_name"]);
		$supplier_address_one = mysqli_escape_string($connect, $_POST["supplier_address_one"]);
		$supplier_address_two = mysqli_escape_string($connect, $_POST["supplier_address_two"]);
		$supplier_address_three = mysqli_escape_string($connect, $_POST["supplier_address_three"]);
		$supplier_place = mysqli_escape_string($connect, $_POST["supplier_place"]);
		$supplier_pin_code = mysqli_escape_string($connect, $_POST["supplier_pin_code"]);
		$supplier_st_code = mysqli_escape_string($connect, $_POST["supplier_st_code"]);
		$supplier_st_name = mysqli_escape_string($connect, $_POST["supplier_st_name"]);
		$cont_person = mysqli_escape_string($connect, $_POST["cont_person"]);
		$supplier_phone = mysqli_escape_string($connect, $_POST["supplier_phone"]);
		$supplier_mobile = mysqli_escape_string($connect, $_POST["supplier_mobile"]);
		$supplier_email = mysqli_escape_string($connect, $_POST["supplier_email"]);
		$supplier_gstno = mysqli_escape_string($connect, $_POST["supplier_gstno"]);
		$supplier_pan_no = mysqli_escape_string($connect, $_POST["supplier_pan_no"]);
		$supplier_aadhar_no = mysqli_escape_string($connect, $_POST["supplier_aadhar_no"]);
		$tax_code = mysqli_escape_string($connect, $_POST["tax_code"]);
		$opening = mysqli_escape_string($connect, $_POST["opening"]);
		$debit = mysqli_escape_string($connect, $_POST["debit"]);
		$credit = mysqli_escape_string($connect, $_POST["credit"]);
		$clbalance = mysqli_escape_string($connect, $_POST["clbalance"]);
		$transporter_name = mysqli_escape_string($connect, $_POST["transporter_name"]);
		$bankname = mysqli_escape_string($connect, $_POST["bankname"]);
		$bankaccno = mysqli_escape_string($connect, $_POST["bankaccno"]);
		$bankaccname = mysqli_escape_string($connect, $_POST["bankaccname"]);
		$ifsccode = mysqli_escape_string($connect, $_POST["ifsccode"]);
		$due_days = mysqli_escape_string($connect, $_POST["due_days"]);
		$remarks = mysqli_escape_string($connect, $_POST["remarks"]);
		$query = "INSERT INTO supplier_mst(acc_type,id,supplier_code, supplier_name,supplier_address_one,supplier_address_two,supplier_address_three,supplier_place,supplier_pin_code,supplier_st_code,supplier_st_name,cont_person,supplier_phone,supplier_mobile,supplier_email,supplier_gstno,supplier_pan_no,supplier_aadhar_no,tax_code,opening,debit,credit,clbalance,transporter_name,bankname,bankaccno,bankaccname,ifsccode,due_days,remarks) VALUES ('" . $acc_type . "','" . $supplier_code . "','" . $supplier_code . "','" . $supplier_name . "','" . $supplier_address_one . "','" . $supplier_address_two . "','" . $supplier_address_three . "','" . $supplier_place . "','" . $supplier_pin_code . "','" . $supplier_st_code . "','" . $supplier_st_name . "','" . $cont_person . "','" . $supplier_phone . "','" . $supplier_mobile . "','" . $supplier_email . "','" . $supplier_gstno . "','" . $supplier_pan_no . "','" . $supplier_aadhar_no . "','" . $tax_code . "','" . $opening . "','" . $debit . "','" . $credit . "','" . $clbalance . "','" . $transporter_name . "','" . $bankname . "','" . $bankaccno . "','" . $bankaccname . "','" . $ifsccode . "','" . $due_days . "','" . $remarks . "')";
		if (mysqli_query($connect, $query)) {
			$query = "SELECT segment_name from segment_mst"; 
			$rs = mysqli_query($connect, $query) or die("Cannot execute query: $query\n");
			while ($row = mysqli_fetch_assoc($rs)) {
				$segment_name = $row['segment_name'];
				$disc_per = '0';				
				$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 = '" . $srnoupd . "' where description='SUPPLIER'";
			mysqli_query($connect, $update) or die(mysqli_last_error());
		}
	}
	if ($_POST["action"] == "Update") {
		$supplier_code = mysqli_escape_string($connect, $_POST["supplier_code"]);
		$supplier_name = mysqli_escape_string($connect, $_POST["supplier_name"]);
		$supplier_address_one = mysqli_escape_string($connect, $_POST["supplier_address_one"]);
		$supplier_address_two = mysqli_escape_string($connect, $_POST["supplier_address_two"]);
		$supplier_address_three = mysqli_escape_string($connect, $_POST["supplier_address_three"]);
		$supplier_place = mysqli_escape_string($connect, $_POST["supplier_place"]);
		$supplier_pin_code = mysqli_escape_string($connect, $_POST["supplier_pin_code"]);
		$supplier_st_code = mysqli_escape_string($connect, $_POST["supplier_st_code"]);
		$supplier_st_name = mysqli_escape_string($connect, $_POST["supplier_st_name"]);
		$cont_person = mysqli_escape_string($connect, $_POST["cont_person"]);
		$supplier_phone = mysqli_escape_string($connect, $_POST["supplier_phone"]);
		$supplier_mobile = mysqli_escape_string($connect, $_POST["supplier_mobile"]);
		$supplier_email = mysqli_escape_string($connect, $_POST["supplier_email"]);
		$supplier_gstno = mysqli_escape_string($connect, $_POST["supplier_gstno"]);
		$supplier_pan_no = mysqli_escape_string($connect, $_POST["supplier_pan_no"]);
		$supplier_aadhar_no = mysqli_escape_string($connect, $_POST["supplier_aadhar_no"]);
		$tax_code = mysqli_escape_string($connect, $_POST["tax_code"]);
		$opening = mysqli_escape_string($connect, $_POST["opening"]);
		$debit = mysqli_escape_string($connect, $_POST["debit"]);
		$credit = mysqli_escape_string($connect, $_POST["credit"]);
		$clbalance = mysqli_escape_string($connect, $_POST["clbalance"]);
		$transporter_name = mysqli_escape_string($connect, $_POST["transporter_name"]);
		$bankname = mysqli_escape_string($connect, $_POST["bankname"]);
		$bankaccno = mysqli_escape_string($connect, $_POST["bankaccno"]);
		$bankaccname = mysqli_escape_string($connect, $_POST["bankaccname"]);
		$ifsccode = mysqli_escape_string($connect, $_POST["ifsccode"]);
		$due_days = mysqli_escape_string($connect, $_POST["due_days"]);
		$remarks = mysqli_escape_string($connect, $_POST["remarks"]);
		$update = "update supplier_mst set supplier_address_one = '" . $supplier_address_one . "',supplier_address_two = '" . $supplier_address_two . "',supplier_address_three = '" . $supplier_address_three . "',supplier_place = '" . $supplier_place . "',supplier_pin_code = '" . $supplier_pin_code . "',supplier_st_code = '" . $supplier_st_code . "',supplier_st_name = '" . $supplier_st_name . "',cont_person = '" . $cont_person . "',supplier_phone = '" . $supplier_phone . "',supplier_mobile = '" . $supplier_mobile . "',supplier_email = '" . $supplier_email . "',supplier_gstno = '" . $supplier_gstno . "',supplier_pan_no = '" . $supplier_pan_no . "',supplier_aadhar_no = '" . $supplier_aadhar_no . "',tax_code = '" . $tax_code . "',opening = '" . $opening . "',debit = '" . $debit . "',credit = '" . $credit . "',clbalance = '" . $clbalance . "',transporter_name = '" . $transporter_name . "',bankname = '" . $bankname . "',bankaccno = '" . $bankaccno . "',bankaccname = '" . $bankaccname . "',ifsccode = '" . $ifsccode . "',due_days = '" . $due_days . "',remarks = '" . $remarks . "' WHERE id = '" . $_POST["id"] . "'";
		if (mysqli_query($connect, $update)) {
		}
	}
	if ($_POST["action"] == "Delete") {
		$delete = "DELETE FROM supplier_mst WHERE id = '" . $_POST["id"] . "'";
		if (mysqli_query($connect, $delete)) {
		}
	}
}
