<?php 
require('../config/dbcon.php');
if(isset($_POST['search'])){
    $search = $_POST['search'];
	//customer_name,cust_place,customer_code,tax_code,cust_gstno,transporter_name,srno,opening,debit,credit,clbalance,cust_address_one,cust_address_two,cust_address_three,cust_pin_code,cust_phone,cust_mobile,cust_st_code,cust_pan_no,cust_email,cust_aadhar_no,cust_st_name,due_days
	$query = "SELECT * FROM customer_mst WHERE acc_type ='SDE' and Lower(customer_name) LIKE Lower('%".$search."%') or Lower(customer_code) LIKE Lower('%".$search."%') or Lower(cust_place) LIKE Lower('%".$search."%') order by customer_name limit 10";
    $result = mysqli_query($connect,$query);	
	$curr_month = date('m');
	if($curr_month >= 4 && $curr_month <= 12){
		$year = date('y') . '-' . date('y', strtotime("+1 year"));
	} else{
		$year = date("y",strtotime("-1 year")) . '-' . date("y");
	}	
    while($row = mysqli_fetch_array($result) ){
		$tax_code = $row['tax_code']; 
		if ($tax_code == "GST-B2B")
		{
			$query = "SELECT srno FROM control_mst WHERE description='CUSTDN'";
			$rs = mysqli_query($connect, $query) or die("Cannot execute query: $query\n");
			while ($row1 = mysqli_fetch_assoc($rs)) {
				$srno = 'B2BDN- '.$row1['srno'].'  '.'('.$year.')';
				$inv_number = $row1['srno']; 
			}
		}
		if ($tax_code == "I-GST")
		{
			$query = "SELECT srno FROM control_mst WHERE description='CUSTDN'";
			$rs = mysqli_query($connect, $query) or die("Cannot execute query: $query\n");
			while ($row1 = mysqli_fetch_assoc($rs)) {
				$srno = 'IGSTDN- '.$row1['srno'].'  '.'('.$year.')';
				$inv_number = $row1['srno']; 
			}
		}
		if ($tax_code == "GST-B2C")
		{
			$query = "SELECT srno FROM control_mst WHERE description='CUSTDN'";
			$rs = mysqli_query($connect, $query) or die("Cannot execute query: $query\n");
			while ($row1 = mysqli_fetch_assoc($rs)) {
				$srno = 'B2CDN- '.$row1['srno'].'  '.'('.$year.')';
				$inv_number = $row1['srno']; 
			}
		}
        $response[] = array("label"=>$row['customer_name'],"value"=>$row['customer_code'],"cust_place"=>$row['cust_place'],"tax_code"=>$row['tax_code'],"cust_gstno"=>$row['cust_gstno'],"transporter_name"=>$row['transporter_name'],"srno"=>$srno,"opening"=>$row['opening'],"debit"=>$row['debit'],"credit"=>$row['credit'],"clbalance"=>$row['clbalance'],"cust_address_one"=>$row['cust_address_one'],"cust_address_two"=>$row['cust_address_two'],"cust_address_three"=>$row['cust_address_three'],"cust_pin_code"=>$row['cust_pin_code'],"cust_phone"=>$row['cust_phone'],"cust_mobile"=>$row['cust_mobile'],"cust_st_code"=>$row['cust_st_code'],"cust_pan_no"=>$row['cust_pan_no'],"cust_email"=>$row['cust_email'],"cust_aadhar_no"=>$row['cust_aadhar_no'],"cust_st_name"=>$row['cust_st_name'],"due_days"=>$row['due_days'],"inv_number"=>$inv_number);
    }
    echo json_encode($response);
}
exit;
?>