<?php

include('db_con.php');


$doc_type = $_POST['doc_type'];
$item_no = $_POST['item_no'];
$v_id = $_POST['v_id'];
$item_detail = '';
$item_detail .= '<div class="row">';
			 			
if($doc_type =='purchase')
{
		$sql = "select m.p_id,m.dated, v.name , d.qty, d.rate from purchase_detail d, purchase m, vendors v where m.p_id=d.p_id and m.v_id=v.v_id and d.item_no = '".$item_no."' order by m.p_id desc limit 5";
		$statement = $connect->query($sql);
		$purchase = $statement->fetchAll();
		$rowcount = $statement->rowCount();
		if($rowcount > 0)
		{
			$item_detail .='<div class="col-md-6">
			Item History:
			<table width="100%">
			<tr style="font-weight:bold">
			<td>PID</td><td>Date</td><td>From</td><td>Qty</td><td align="right">Rate</td>
			</tr>';
			foreach ($purchase as $row) {
				$item_detail .='
				<tr>
				<td>'.$row["p_id"].'</td>
				<td>'.$row["dated"].'</td>
				<td>'.$row["name"].'</td>
				<td>'.$row["qty"].'</td>
				<td align="right">'.$row["rate"].'</td>
				</tr>';
			}

			$item_detail .='</table></div>';
		}else {
			$item_detail .='<div class="col-md-6">
			<table width="100%">
			<tr>
			<td colspan="3">Item History Not Found</td>
			</tr></table></div>';
		}

		$sql = "select m.p_id,m.dated, v.name , d.qty, d.rate from purchase_detail d, purchase m, vendors v where m.p_id=d.p_id and m.v_id=v.v_id and m.v_id = '".$v_id."' and d.item_no = '".$item_no."' order by m.p_id desc limit 5";
		$statement = $connect->query($sql);
		$purchase = $statement->fetchAll();
		$rowcount = $statement->rowCount();
		if($rowcount > 0)
		{
			$item_detail .='<div class="col-md-6">
			Vendor History:
			<table width="100%">
			<tr style="font-weight:bold">
			<td>PID</td><td>Date</td><td>From</td><td>Qty</td><td align="right">Rate</td>
			</tr>';
			foreach ($purchase as $row) {
				$item_detail .='
				<tr>
				<td>'.$row["p_id"].'</td>
				<td>'.$row["dated"].'</td>
				<td style="font-weight:bold">'.$row["name"].'</td>
				<td>'.$row["qty"].'</td>
				<td align="right">'.$row["rate"].'</td>
				</tr>';
			}

			$item_detail .='</table></div>';
		}else {
			$item_detail .='<div class="col-md-6">
			<table width="100%">
			<tr>
			<td colspan="3">To History Not Found</td>
			</tr></table></div>';
		}
		
}elseif($doc_type =='sale')
{

		$sql = "select m.s_id,m.dated, a.acc_name name, d.qty, d.rate from sale_detail d, sale m, accounts a where m.s_id=d.s_id and m.cust_id=a.acc_no and d.item_no = '".$item_no."' order by m.s_id desc limit 5";
		$statement = $connect->query($sql);
		$purchase = $statement->fetchAll();
		$rowcount = $statement->rowCount();
		if($rowcount > 0)
		{
			$item_detail .='<div class="col-md-6">
			Item History:
			<table width="100%">
			<tr style="font-weight:bold">
			<td>SID</td><td>Date</td><td>From</td><td>Qty</td><td align="right">Rate</td>
			</tr>';
			foreach ($purchase as $row) {
				$item_detail .='
				<tr>
				<td>'.$row["s_id"].'</td>
				<td>'.$row["dated"].'</td>
				<td>'.$row["name"].'</td>
				<td>'.$row["qty"].'</td>
				<td align="right">'.$row["rate"].'</td>
				</tr>';
			}

			$item_detail .='</table></div>';
		}else {
			$item_detail .='<div class="col-md-6">
			<table width="100%">
			<tr>
			<td colspan="3">Item History Not Found</td>
			</tr></table></div>';
		}

		$sql = "select m.s_id,m.dated, a.acc_name name , d.qty, d.rate from sale_detail d, sale m, accounts a where m.s_id=d.s_id and m.cust_id=a.acc_no and m.cust_id = '".$v_id."' and d.item_no = '".$item_no."' order by m.s_id desc limit 5";
		$statement = $connect->query($sql);
		$purchase = $statement->fetchAll();
		$rowcount = $statement->rowCount();
		if($rowcount > 0)
		{
			$item_detail .='<div class="col-md-6">
			Customer History:
			<table width="100%">
			<tr style="font-weight:bold">
			<td>SID</td><td>Date</td><td>To</td><td>Qty</td><td align="right">Rate</td>
			</tr>';
			foreach ($purchase as $row) {
				$item_detail .='
				<tr>
				<td>'.$row["s_id"].'</td>
				<td>'.$row["dated"].'</td>
				<td style="font-weight:bold">'.$row["name"].'</td>
				<td>'.$row["qty"].'</td>
				<td align="right">'.$row["rate"].'</td>
				</tr>';
			}

			$item_detail .='</table></div>';
		}else {
			$item_detail .='<div class="col-md-6">
			<table width="100%">
			<tr>
			<td colspan="3">From History Not Found</td>
			</tr></table></div>';
		}
		
}elseif($doc_type =='cash')
{

		$sql = "select m.s_id,m.dated, m.cust_name name, d.qty, d.rate from sale_detail d, sale m, accounts a where m.s_id=d.s_id and m.cust_id=a.acc_no and m.inv_type='Cash' and d.item_no = '".$item_no."' order by m.s_id desc limit 5";
		$statement = $connect->query($sql);
		$purchase = $statement->fetchAll();
		$rowcount = $statement->rowCount();
		if($rowcount > 0)
		{
			$item_detail .='<div class="col-md-6">
			Item History
			<table width="100%">
			<tr style="font-weight:bold">
			<td>SID</td><td>Date</td><td>From</td><td>Qty</td><td align="right">Rate</td>
			</tr>';
			foreach ($purchase as $row) {
				$item_detail .='
				<tr>
				<td>'.$row["s_id"].'</td>
				<td>'.$row["dated"].'</td>
				<td>'.$row["name"].'</td>
				<td>'.$row["qty"].'</td>
				<td align="right">'.$row["rate"].'</td>
				</tr>';
			}

			$item_detail .='</table></div>';
		}else {
			$item_detail .='<div class="col-md-6">
			<table width="100%">
			<tr>
			<td colspan="3">Item History Not Found</td>
			</tr></table></div>';
		}

		$sql = "select m.s_id,m.dated, m.cust_name name , d.qty, d.rate from sale_detail d, sale m, accounts a where m.s_id=d.s_id and m.cust_id=a.acc_no and m.contact = '".$v_id."' and d.item_no = '".$item_no."' order by m.s_id desc limit 5";
		$statement = $connect->query($sql);
		$purchase = $statement->fetchAll();
		$rowcount = $statement->rowCount();
		if($rowcount > 0)
		{
			$item_detail .='<div class="col-md-6">
			'.$v_id.' History:
			<table width="100%">
			<tr style="font-weight:bold">
			<td>SID</td><td>Date</td><td>To</td><td>Qty</td><td align="right">Rate</td>
			</tr>';
			foreach ($purchase as $row) {
				$item_detail .='
				<tr>
				<td>'.$row["s_id"].'</td>
				<td>'.$row["dated"].'</td>
				<td style="font-weight:bold">'.$row["name"].'</td>
				<td>'.$row["qty"].'</td>
				<td align="right">'.$row["rate"].'</td>
				</tr>';
			}

			$item_detail .='</table></div>';
		}else {
			$item_detail .='<div class="col-md-6">
			<table width="100%">
			<tr>
			<td colspan="3">From History Not Found</td>
			</tr></table></div>';
		}
		
}elseif($doc_type =='quote')
{

		$sql = "select m.s_id,m.dated, a.acc_name name, d.qty, d.rate from quotation_detail d, quotation m, accounts a where m.s_id=d.s_id and m.cust_id=a.acc_no and m.cust_id = '".$v_id."' and d.item_no = '".$item_no."' order by m.s_id desc limit 5";
		$statement = $connect->query($sql);
		$purchase = $statement->fetchAll();
		$rowcount = $statement->rowCount();
		if($rowcount > 0)
		{
			$item_detail .='<div class="col-md-6">
			Quotation History:
			<table width="100%">
			<tr style="font-weight:bold">
			<td>SID</td><td>Date</td><td>From</td><td>Qty</td><td align="right">Rate</td>
			</tr>';
			foreach ($purchase as $row) {
				$item_detail .='
				<tr>
				<td>'.$row["s_id"].'</td>
				<td>'.$row["dated"].'</td>
				<td>'.$row["name"].'</td>
				<td>'.$row["qty"].'</td>
				<td align="right">'.$row["rate"].'</td>
				</tr>';
			}

			$item_detail .='</table></div>';
		}else {
			$item_detail .='<div class="col-md-6">
			<table width="100%">
			<tr>
			<td colspan="3">Item History Not Found</td>
			</tr></table></div>';
		}

		$sql = "select m.s_id,m.dated, a.acc_name name , d.qty, d.rate from sale_detail d, sale m, accounts a where m.s_id=d.s_id and m.cust_id=a.acc_no and m.cust_id = '".$v_id."' and d.item_no = '".$item_no."' order by m.s_id desc limit 5";
		$statement = $connect->query($sql);
		$purchase = $statement->fetchAll();
		$rowcount = $statement->rowCount();
		if($rowcount > 0)
		{
			$item_detail .='<div class="col-md-6">
			Sale History:
			<table width="100%">
			<tr style="font-weight:bold">
			<td>SID</td><td>Date</td><td>To</td><td>Qty</td><td align="right">Rate</td>
			</tr>';
			foreach ($purchase as $row) {
				$item_detail .='
				<tr>
				<td>'.$row["s_id"].'</td>
				<td>'.$row["dated"].'</td>
				<td style="font-weight:bold">'.$row["name"].'</td>
				<td>'.$row["qty"].'</td>
				<td align="right">'.$row["rate"].'</td>
				</tr>';
			}

			$item_detail .='</table></div>';
		}else {
			$item_detail .='<div class="col-md-6">
			<table width="100%">
			<tr>
			<td colspan="3">From History Not Found</td>
			</tr></table></div>';
		}
		
}

 
	
$item_detail .='</div>';
$output["item_detail"]  = $item_detail;	
		echo json_encode($output);
?>