<?php
//user_action.php
include('db_con.php');
//	include('function.php');
$rate = 0;
$cost = 0;
$stock = 0;
 		//stock_update($connect);
		
		
 			$item_type_history = '<div class="row">
			 			<div class="col-md-6">';
 						
	
				
$query = "
				SELECT  IFNULL(d.rate,0) rate FROM sale_detail d,sale m , items i 
				WHERE m.s_id=d.s_id and m.cust_id = :cust_id and i.item_id=d.item_no and i.type_id = :type_id";
				
				$statement = $connect->prepare($query);
				$statement->execute(
					array(
						
						':cust_id'	=>	$_POST["cust_id"],
						':type_id'	=>	$_POST["type_id"]
					)
				);
				$rate = $statement->fetchColumn();
				if($rate > 0)
				{
				 $item_type_history .='
			 			
			 			<table width ="100%">
			 			<tr>
			 			<th width="25%">Date</th>
			 			<th width="25%">S ID.</th>
			 			<th width="25%">Item</th>
			 			<th width="25%">Rate</th>
			 			</tr>
			 			';
			 			
					$sql = "select m.dated, m.s_id,i.item_name,ifnull(d.rate,0) rate,ifnull(d.dis_pr,0) dis_pr,ifnull(d.dis_rate,0) dis_rate from sale_detail d, sale m, items i, item_type t where m.s_id = d.s_id and m.cust_id = '".$_POST['cust_id']."' and t.type_id=i.type_id and d.item_no=i.item_id and t.type_id = '".$_POST['type_id']."' order by s_id desc LIMIT 5";
					 $statement = $connect->prepare($sql);
					 $statement->execute();
					  
					 
					 $result = $statement->fetchAll();
					
						foreach($result as $row)
							{
						  
						  $rate = $row["rate"];
						
							$item_type_history .= '
						  
						  
						  
							<tr>
							  <td width="25%">'.$row["dated"].'</td>
							  <td width="25%">'.$row["s_id"].'</td>
							  <td width="25%">'.$row["item_name"].'</td>
							   <td width="25%" class="rightside"><b>'.number_format($rate, 2).'</b></td>
							  
							   
							    
						    </tr>
						  ';
						}
					}else {$item_type_history .= '
						  
						  
						  
							<tr>
							  <td width="25%">Sale History :</td>
							  <td width="25%">Not Found</td>
							  <td width="25%"></td>
							   <td width="25%" class="rightside"><b></b></td>
							  
							   
							    
						    </tr>
						  ';}
			  
				$item_type_history .='</table><br /></div>';
				
				$output['item_type_history']  =  $item_type_history;
				if($rate == 0)
				{
					
				}
				$output['rate']  =  $rate;
	
		echo json_encode($output);
?>