<?php
require_once("config/configDB.php"); 
// storing  request (ie, get/post) global array to a variable  
$requestData= $_REQUEST;



$selectspare     = $_POST['selectspare'];


 	$columns = array( 
			0 => 'productname',
			1 => 'explosiveqty'
		);
		

		$sql = "SELECT sm.id ,sm.productname,sum(psd.purchase_eqty) as explosiveqty FROM product_master sm LEFT JOIN  purchasedetails as psd ON sm.id=psd.productID WHERE   sm.isdelete=1  and psd.productID>0 and psd.status='' and sm.product_type='Explosive'  ";

		if($selectspare !='')  {
		
			$sql.="  and sm.id='".$selectspare."' ";
		}
		
		$sql.=" GROUP BY psd.productID ";
		
	 
	$query=mysqli_query($conn, $sql) or die("brandgriddata.php: get brand Data step 1".$sql);
	$totalData = mysqli_num_rows($query);
	$totalFiltered = $totalData;

	if( !empty($requestData['search']['value']) ) {   
		$sql.=" AND ( sm.productname LIKE '".$requestData['search']['value']."%' ";    
		$sql.=" OR psd.purchase_eqty LIKE '".$requestData['search']['value']."%' ";
		
		$sql.="  )";
	}

	


$query=mysqli_query($conn, $sql) or die("brandgriddata.php: get brand Data step 2".$sql);
$totalFiltered = mysqli_num_rows($query); 

$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";


$query=mysqli_query($conn, $sql) or die("brandgriddata.php: get brand Data step 3".$sql);
	

$data = array();
while( $row=mysqli_fetch_array($query) ) { 
	$nestedData=array(); 
		
		$spareName		= $row['productname'];
		$nestedData[] 	= $row['productname'];
		

		$spareid    = $row['id'];

//echo "SELECT sum(isd.qty) as explosiveqty   FROM explosive_utiliz_dtl as isd WHERE isd.explosiveID='".$spareid."' and  isd.status=''  ";

//echo "SELECT sum(isd.qty) as explosiveqty   FROM explosive_utiliz_dtl as isd WHERE isd.explosiveID='".$spareid."' and  isd.status=''";

		$resultSpareQty = mysqli_query($conn,"SELECT sum(isd.qty) as explosiveqty   FROM explosive_utiliz_dtl as isd WHERE isd.explosiveID='".$spareid."' and  isd.status=''  ");
		$SpareQty    = mysqli_fetch_array($resultSpareQty,MYSQLI_ASSOC);
		$ASpareQty   = ($SpareQty['explosiveqty']>0) ? $SpareQty['explosiveqty'] : 0;
		
		
		$resultPwo 		= mysqli_query($conn,"SELECT sum(psd.purchase_eqty) as explosiveqty   FROM purchasedetailswo as psd WHERE psd.productID='".$spareid."' and  psd.status='' ");
		$PSpareQtywo 		= mysqli_fetch_array($resultPwo,MYSQLI_ASSOC);
		
		$resultP 		= mysqli_query($conn,"SELECT sum(psd.purchase_eqty) as explosiveqty   FROM purchasedetails as psd WHERE psd.productID='".$spareid."' and  psd.status='' ");
		$PSpareQty 		= mysqli_fetch_array($resultP,MYSQLI_ASSOC);
		$AwSpareQty   	= ($PSpareQty['explosiveqty']+$PSpareQtywo['explosiveqty']) - ($ASpareQty); 
		$nestedData[] 	= $AwSpareQty;
		//$nestedData[] 	= $spareName;
	
	$data[] = $nestedData;
}

$json_data = array(
			"draw"            => intval( $requestData['draw'] ),   
			"recordsTotal"    => intval( $totalData ),  
			"recordsFiltered" => intval( $totalFiltered ), 
			"data"            => $data   
			);

echo json_encode($json_data);  

?>
