<?php 
require('../config/dbcon.php');//connect
if(isset($_POST['search'])){
    $search = $_POST['search'];	
	$query = "SELECT * FROM item_mst WHERE Lower(partno) LIKE Lower('%".$search."%') or Lower(description_one) LIKE Lower('%".$search."%') order by partno limit 10";
    $result = mysqli_query($connect,$query);    
    while($row = mysqli_fetch_array($result) ){
		$segment=$row['segment'];	
		$update="update custsegment_mst set segment = '".$segment."'";
		mysqli_query($connect, $update) or die(mysqli_last_error());
		
		$query = "Select customer_code from custsegment_mst"; 
		$rs = mysqli_query($connect, $query) or die("Cannot execute query: $query\n");
		while ($row1 = mysqli_fetch_assoc($rs)) {
			$customer_code = $row1['customer_code'];
		}
		$query1 = "Select disc_per from disc_mst where customer_code = '".$customer_code."' and segment = '".$segment."'"; 
		$rs1 = mysqli_query($connect, $query1) or die("Cannot execute query: $query\n");
		while ($row1 = mysqli_fetch_assoc($rs1)) {
			$disc_per = $row1['disc_per']; 				
		}
        $response[] = array("label"=>$row['partno'],"value"=>$row['description_one'],"list"=>number_format($row['list'],2, '.', ''),"description_two"=>$row['description_two'],"hsn_code"=>$row['hsn_code'],"cost"=>number_format($row['cost'],3, '.', ''),"segment"=>$row['segment'],"stk_qty"=>number_format($row['stk_qty'],0, '.', ''),"boxqty"=>number_format($row['boxqty'],0, '.', ''),"disc_per"=>number_format($disc_per,2, '.', ''),"sgst_per"=>number_format($row['sgst_per'],2, '.', ''),"cgst_per"=>number_format($row['cgst_per'],2, '.', ''),"igst_per"=>number_format($row['igst_per'],2, '.', ''),"moq"=>number_format($row['moq'],0, '.', ''),"ly_sales"=>number_format($row['ly_sales'],2, '.', ''),"cy_sales"=>number_format($row['cy_sales'],2, '.', ''),"pending"=>number_format($row['pending'],0, '.', ''),"dispatch1"=>number_format($row['dispatch1'],0, '.', ''));
    }
    echo json_encode($response);
}
exit;
?>