<?php
//user_action.php
include('db_con.php');
include('function.php');
$rate = 0;
$cost = 0;
$stock = 0;
 		stock_update($connect);
		
		
 			$item_detail = '<div class="row">
			 			<div class="col-md-6">';
 						
	
				
$query = "
				SELECT  IFNULL(d.rate,0) rate FROM sale_detail d,sale m 
				WHERE m.s_id=d.s_id and m.contact = :contact and
				d.item_no = :item_no";
				
				$statement = $connect->prepare($query);
				$statement->execute(
					array(
						':item_no'	=>	$_POST["item_no"],
						':contact'	=>	$_POST["contact"]
					)
				);
				$rate = $statement->fetchColumn();
				if($rate > 0)
				{
				 $item_detail .='
			 			
			 			<table width ="100%">
			 			<tr>
			 			<th width="34%">Date</th>
			 			<th width="33%">S ID.</th>
			 			<th width="33%">Rate</th>
			 			</tr>
			 			';
			 			
					$sql = "select m.dated, m.s_id,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 
					where m.s_id = d.s_id and m.contact = '".$_POST['contact']."' and 
					d.item_no = '".$_POST['item_no']."' order by s_id desc LIMIT 5";
					 $statement = $connect->prepare($sql);
					 $statement->execute();
					  
					 
					 $result = $statement->fetchAll();
					
						foreach($result as $row)
							{
						  
						  $rate = $row["rate"];
						
							$item_detail .= '
						  
						  
						  
							<tr>
							  <td width="34%">'.$row["dated"].'</td>
							  <td width="33%">'.$row["s_id"].'</td>
							   <td width="33%" class="rightside"><b>'.number_format($rate, 2).'</b></td>
							  
							   
							    
						    </tr>
						  ';
						}
					}else {$item_detail .= '
						  
						  
						  
							<tr>
							  <td width="34%">Sale History :</td>
							  <td width="33%">Not Found</td>
							   <td width="33%" class="rightside"><b></b></td>
							  
							   
							    
						    </tr>
						  ';}
			  
				$item_detail .='</table><br /></div>';
				$query = "
				SELECT  rate FROM purchase_detail d
				WHERE 
				d.item_no = :item_no";
				
				$statement = $connect->prepare($query);
				$statement->execute(
					array(
						':item_no'	=>	$_POST["item_no"]
						
					)
				);
				$rate = $statement->fetchColumn();
				if($rate > 0)
				{
				 $item_detail .='
			 			
			 			<div class="col-md-6">
			 			<table width ="100%">
			 			<tr>
			 			<th width="34%">Date</th>
			 			<th width="33%">ID.</th>
			 			<th width="33%">PCS/ctn</th>
			 			</tr>
			 			';
			 			
					$sql = "select m.dated, m.p_id,d.ctn,ifnull(d.rate,0) rate from purchase_detail d, purchase m 
					where m.p_id = d.p_id and  
					d.item_no = '".$_POST['item_no']."' order by p_id desc LIMIT 5";
					 $statement = $connect->prepare($sql);
					 $statement->execute();
					  
					 
					 $result = $statement->fetchAll();
					
						foreach($result as $row)
							{
						  
						  $ctn = $row["ctn"];
						
							$item_detail .= '
						  
						  
						  
							<tr>
							  <td width="34%">'.$row["dated"].'</td>
							  <td width="33%">'.$row["p_id"].'</td>
							   <td width="33%" class="rightside"><b>'.number_format($ctn, 0).'</b></td>
							  
							   
							    
						    </tr>
						  ';
						}
					}else {$item_detail .= '
						  
						  
						  
							<tr>
							  <td width="34%">Purchase History :</td>
							  <td width="33%">Not Found</td>
							   <td width="33%" class="rightside"><b></b></td>
							  
							   
							    
						    </tr>
						  ';}
			  
				$item_detail .='</table><br /></div></div>';
				$output['item_detail']  =  $item_detail;
				if($rate == 0)
				{
					$sql = " select retail_price from items where item_id = '".$_POST['item_no']."'";
					 $statement = $connect->prepare($sql);
					 $statement->execute();
					 $rate = $statement->fetchColumn();
				}
				$output['rate']  =  $rate;
	
		echo json_encode($output);
?>