<?php
if (isset($_POST["action"])) {
	$output = '';
	require('../config/dbcon.php');
	if ($_POST["action"] == "Add") {
	    $acc_type = mysqli_escape_string($connect, $_POST["acc_type"]);
		$customer_code = mysqli_escape_string($connect, $_POST["customer_code"]);
		$srnoupd = $_POST['customer_code'] + 1;
		$customer_name = mysqli_escape_string($connect, $_POST["customer_name"]);
		$cust_address_one = mysqli_escape_string($connect, $_POST["cust_address_one"]);
		$cust_address_two = mysqli_escape_string($connect, $_POST["cust_address_two"]);
		$cust_address_three = mysqli_escape_string($connect, $_POST["cust_address_three"]);
		$cust_place = mysqli_escape_string($connect, $_POST["cust_place"]);
		$cust_pin_code = mysqli_escape_string($connect, $_POST["cust_pin_code"]);
		$cust_st_code = mysqli_escape_string($connect, $_POST["cust_st_code"]);
		$cust_st_name = mysqli_escape_string($connect, $_POST["cust_st_name"]);
		$cont_person = mysqli_escape_string($connect, $_POST["cont_person"]);
		$cust_phone = mysqli_escape_string($connect, $_POST["cust_phone"]);
		$cust_mobile = mysqli_escape_string($connect, $_POST["cust_mobile"]);
		$cust_email = mysqli_escape_string($connect, $_POST["cust_email"]);
		$cust_gstno = mysqli_escape_string($connect, $_POST["cust_gstno"]);
		$cust_pan_no = mysqli_escape_string($connect, $_POST["cust_pan_no"]);
		$cust_aadhar_no = mysqli_escape_string($connect, $_POST["cust_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 customer_mst(acc_type,id,customer_code, customer_name,cust_address_one,cust_address_two,cust_address_three,cust_place,cust_pin_code,cust_st_code,cust_st_name,cont_person,cust_phone,cust_mobile,cust_email,cust_gstno,cust_pan_no,cust_aadhar_no,tax_code,opening,debit,credit,clbalance,transporter_name,bankname,bankaccno,bankaccname,ifsccode,due_days,remarks) VALUES ('" . $acc_type . "','" . $customer_code . "','" . $customer_code . "','" . $customer_name . "','" . $cust_address_one . "','" . $cust_address_two . "','" . $cust_address_three . "','" . $cust_place . "','" . $cust_pin_code . "','" . $cust_st_code . "','" . $cust_st_name . "','" . $cont_person . "','" . $cust_phone . "','" . $cust_mobile . "','" . $cust_email . "','" . $cust_gstno . "','" . $cust_pan_no . "','" . $cust_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';
				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 = '" . $srnoupd . "' where description='CUSTOMER'";
			mysqli_query($connect, $update) or die(mysqli_last_error());
		}
		
	}
	if ($_POST["action"] == "Update") {
		$customer_code = mysqli_escape_string($connect, $_POST["customer_code"]);
		$customer_name = mysqli_escape_string($connect, $_POST["customer_name"]);
		$cust_address_one = mysqli_escape_string($connect, $_POST["cust_address_one"]);
		$cust_address_two = mysqli_escape_string($connect, $_POST["cust_address_two"]);
		$cust_address_three = mysqli_escape_string($connect, $_POST["cust_address_three"]);
		$cust_place = mysqli_escape_string($connect, $_POST["cust_place"]);
		$cust_pin_code = mysqli_escape_string($connect, $_POST["cust_pin_code"]);
		$cust_st_code = mysqli_escape_string($connect, $_POST["cust_st_code"]);
		$cust_st_name = mysqli_escape_string($connect, $_POST["cust_st_name"]);
		$cont_person = mysqli_escape_string($connect, $_POST["cont_person"]);
		$cust_phone = mysqli_escape_string($connect, $_POST["cust_phone"]);
		$cust_mobile = mysqli_escape_string($connect, $_POST["cust_mobile"]);
		$cust_email = mysqli_escape_string($connect, $_POST["cust_email"]);
		$cust_gstno = mysqli_escape_string($connect, $_POST["cust_gstno"]);
		$cust_pan_no = mysqli_escape_string($connect, $_POST["cust_pan_no"]);
		$cust_aadhar_no = mysqli_escape_string($connect, $_POST["cust_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 customer_mst set cust_address_one = '" . $cust_address_one . "',cust_address_two = '" . $cust_address_two . "',cust_address_three = '" . $cust_address_three . "',cust_place = '" . $cust_place . "',cust_pin_code = '" . $cust_pin_code . "',cust_st_code = '" . $cust_st_code . "',cust_st_name = '" . $cust_st_name . "',cont_person = '" . $cont_person . "',cust_phone = '" . $cust_phone . "',cust_mobile = '" . $cust_mobile . "',cust_email = '" . $cust_email . "',cust_gstno = '" . $cust_gstno . "',cust_pan_no = '" . $cust_pan_no . "',cust_aadhar_no = '" . $cust_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 customer_mst WHERE id = '" . $_POST["id"] . "'";
		if (mysqli_query($connect, $delete)) {
		}
	}
}