<?php
/* Database connection start */
require_once("../includes/config.php");
/////get status//
$arrstatus = getFullStatus("master",$link1);

////get access product details
$access_product = getAccessProduct($_SESSION['asc_code'],$link1);
////get access brand details
$access_brand = getAccessBrand($_SESSION['asc_code'],$link1);

/* Database connection end */
// storing  request (ie, get/post) global array to a variable  
$requestData= $_REQUEST;
## selected  Status
if($_REQUEST['product']!='' && $_REQUEST['brand']==''){
	$mod_id =  " product_id='".$_REQUEST['product']."'  and brand_id in (".$access_brand.") ";
}else if($_REQUEST['product']!='' && $_REQUEST['brand']!=''){
	$mod_id =  " product_id='".$_REQUEST['product']."' and brand_id  = '".$_REQUEST['brand']."' ";
}else if($_REQUEST['product']=='' && $_REQUEST['brand']!=''){
	$mod_id =  " product_id in (".$access_product.") and brand_id  = '".$_REQUEST['brand']."' ";
}else{
	$mod_id =  " product_id in (".$access_product.") and brand_id in (".$access_brand.") ";
}

/*
 if($_REQUEST['product']!='' && $_REQUEST['brand']=='' && $_REQUEST['model']==""){
	$mod_id =  "product_id='".$_REQUEST['product']."'";
	}else if($_REQUEST['product']!='' && $_REQUEST['brand']!='' && $_REQUEST['model']==""){
		$mod_id =  "product_id='".$_REQUEST['product']."' and brand_id  = '".$_REQUEST['brand']."'";
		}else if($_REQUEST['product']!='' && $_REQUEST['brand']!='' && $_REQUEST['model']!=""){
			$mod_id =  "product_id='".$_REQUEST['product']."' and brand_id  = '".$_REQUEST['brand']."' and model_id  Like '%".$_REQUEST['model']."%'";
		}else if($_REQUEST['product']=='' && $_REQUEST['brand']!='' && $_REQUEST['model']!=""){
			$mod_id =  " brand_id  = '".$_REQUEST['brand']."' and model_id  Like '%".$_REQUEST['model']."%'";
		}else if($_REQUEST['product']=='' && $_REQUEST['brand']!='' && $_REQUEST['model']==""){
			$mod_id =  " brand_id  = '".$_REQUEST['brand']."' ";
		}else if($_REQUEST['product']=='' && $_REQUEST['brand']=='' && $_REQUEST['model']==""){
			$mod_id =  "1 ";
		}else{
			$mod_id= " 1";
			}
			
			*/


$columns = array( 
// datatable column index  => database column name
	0 => 'partcode', 
	1 => 'hsn_code',
	2 => 'part_name',
	3 => 'part_category',
	4 => 'status'
);

// getting total number records without any search
 $sql = "SELECT *";
$sql.=" FROM partcode_master where ".$mod_id;

$query=mysqli_query($link1, $sql) or die("partcode-grid-asc.php: get partcode master");
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;  // when there is no search parameter then total number rows = total number filtered rows.

 	$sql = "SELECT *";
	$sql.=" FROM partcode_master where ".$mod_id;
if( !empty($requestData['search']['value']) ) {   // if there is a search parameter, $requestData['search']['value'] contains search parameter
	$sql.=" AND ( partcode LIKE '".$requestData['search']['value']."%'";    
	$sql.=" OR hsn_code LIKE '".$requestData['search']['value']."%'";
	$sql.=" OR part_name LIKE '".$requestData['search']['value']."%'";
	$sql.=" OR part_desc LIKE '".$requestData['search']['value']."%'";
	$sql.=" OR model_id in (select model_id from model_master where model LIKE '%".$requestData['search']['value']."%') ";
	$sql.=" OR vendor_partcode LIKE '".$requestData['search']['value']."%'";
	$sql.=" OR customer_partcode LIKE '".$requestData['search']['value']."%'";
	$sql.=" OR status LIKE '".$requestData['search']['value']."%' )";
}
	$query=mysqli_query($link1, $sql) or die("partcode-grid-asc.php: get partcode master");
	$totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result. 

//echo $sql;
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";
/* $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc  */	
$query=mysqli_query($link1, $sql) or die("partcode-grid-asc.php: get partcode master");

$data = array();
$j=1;
//echo mysqli_num_rows($query)."dadat";
while( $row=mysqli_fetch_array($query) ) {      // preparing an array
	$part= explode(",",$row['model_id']); 
   $partpresent   = count($part);
   if($partpresent == '1'){
   $name = getAnyDetails($part[0],"model","model_id","model_master",$link1 );
   }
   else if($partpresent >1){
	 $name ='';
   for($i=0 ; $i<$partpresent; $i++){					 
	$name.=  getAnyDetails($part[$i],"model","model_id","model_master",$link1 ).",";
	}}
		///////////////////////////////////////////////////////////
		
		//getAnyDetails($row["brand_id"],"brand","brand_id","brand_master",$link1)
	$brnd_map= explode(",",$row['brand_map']); 
	$brndmap   = count($brnd_map);
	if($brndmap == '1'){
	   $brnd = getAnyDetails($brnd_map[0],"brand","brand_id","brand_master",$link1);
	}else if($brndmap >1){
	   $brnd ='';
	   for($c=0 ; $c<$brndmap; $c++){					 
		   $brnd.=  getAnyDetails($brnd_map[$c],"brand","brand_id","brand_master",$link1).",";
	   }
	}
	/////////////////////////////////////////	
						
	 	$res_tax = mysqli_query($link1,"SELECT id,sgst,igst,cgst FROM tax_hsn_master where hsn_code='".$row['hsn_code']."'");
        		$row_tax = mysqli_fetch_assoc($res_tax) ;
	
	
	$nestedData=array(); 
     
	$nestedData[] = $j; 
	$nestedData[] = $row["partcode"]." (". $row["customer_partcode"].")";
	$nestedData[] = $row["hsn_code"];
	$nestedData[] = $name;
	$nestedData[] = $row["part_name"];
	$nestedData[] = $row["vendor_partcode"];
	$nestedData[] = $brnd;
	$nestedData[] = getAnyDetails($row["product_id"],"product_name","product_id","product_master",$link1);
	$nestedData[] = $row["part_category"];
	$nestedData[] = $row["customer_price"];
	$nestedData[] = "<div align='center'><a href='#' title='view Available Stock' onClick=checkstockAvl('".$row["partcode"]."')><i class='fa fa-map-o fa-lg faicon' title='view Available Stock'></i></a></div>";
	$nestedData[] = "<a href='".$row["img_url"]."' target='_blank'><img src='".$row["img_url"]."' width='100%'></a>";
	//$nestedData[] = "<div align='center'><a href='edit_partcode.php?op=Edit&refid=".base64_encode($row['partcode'])."&status=".$_REQUEST['status']."".$pagenav."' title='view'><i class='fa fa-eye fa-lg faicon' title='view/edit partcode details'></i></a></div>";
	
	$data[] = $nestedData;
	$j++;
}





$json_data = array(
			"draw"            => intval( $requestData['draw'] ),   // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw. 
			"recordsTotal"    => intval( $totalData ),  // total number of records
			"recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
			"data"            => $data   // total data array
			);

echo json_encode($json_data);  // send data as json format
?>
