<?php 
require("php_con.php");
require("function.php");

if(isset($_GET['bill_id'])&&isset($_GET['sale_qty'])&&isset($_GET['product_barcode'])&&isset($_GET['price_id'])){
$bill_id=mysqli_real_escape_string($connation,$_GET['bill_id']);
$sale_qty=mysqli_real_escape_string($connation,$_GET['sale_qty']);
$product_barcode=mysqli_real_escape_string($connation,$_GET['product_barcode']);
	
$product_qury=mysqli_query($connation,"SELECT * FROM product_details WHERE product_barcode='$product_barcode'");
if(mysqli_num_rows($product_qury)>0){
	$product_resalt=mysqli_fetch_array($product_qury);
	$price_qury=mysqli_query($connation,"SELECT * FROM price_details WHERE price_id='$_GET[price_id]'");
	$price_resalt=mysqli_fetch_array($price_qury);
	
	if($_GET['wholesale_btn_val']==1){
		$price=$price_resalt['price_wholesale'];	
	}
	elseif($_GET['wholesale_btn_val']==2){
		$price=$price_resalt['price_special'];	
	}
	else{
	
	if($product_resalt['product_add_discount']=="1"){
		$price=$price_resalt['price_discount'];
	}
	else{
		$price=$price_resalt['price_sale'];
	}
	}
	
	$check_sale=mysqli_query($connation,"SELECT * FROM sale_list WHERE sale_billid='$bill_id' and sale_barcode='$product_barcode' and sale_saleprice='$price'");
	if(mysqli_num_rows($check_sale)>0){
		$check_resalt=mysqli_fetch_array($check_sale);
		$update_qty=$check_resalt['sale_qty']+$sale_qty;
		mysqli_query($connation,"UPDATE sale_list SET sale_qty='$update_qty' WHERE sale_id='$check_resalt[sale_id]'");
	}
	else{		
		$sale_buy_price=$price_resalt['price_buy'];
		$sale_real_sale_price=$price_resalt['price_sale'];
		mysqli_query($connation,"INSERT INTO sale_list(sale_billid, sale_barcode, sale_buy_price, sale_real_sale_price, sale_saleprice, sale_qty) VALUES ('$bill_id','$product_barcode','$sale_buy_price','$sale_real_sale_price','$price','$sale_qty')");
	}
	
}
else{
echo "<script>alert('Product not found')</script>";
}
echo "<script>window.location='sale_list.php?bill_id=$bill_id';</script>";
}
?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link href="data_table/css/bootstrap.css" rel="stylesheet" type="text/css">
<link href="icon/css/font-awesome.css" rel="stylesheet" type="text/css">
<link href="icon/css/font-awesome.min.css" rel="stylesheet" type="text/css">
</head>

<body>
<table class="table table-sm table-bordered">
  <tbody>
    <tr>
		<td>Add Time</td>
		<td>Badge Name</td>
		<td>MRP</td>
      <td align="center"><a href="sale_list.php?bill_id=<?php echo $_GET['bill_id']; ?>" class="btn btn-sm btn-secondary"><i class="fa fa-close"></i></a></td>
    </tr>
<?php 
$price_qury=mysqli_query($connation,"SELECT *
FROM price_details prd INNER JOIN product_details pd ON prd.price_systemid=pd.product_systemid
WHERE pd.product_barcode='$_GET[product_barcode]'
ORDER BY prd.price_id DESC");
while($price_resalt=mysqli_fetch_assoc($price_qury)){
?>
    <tr class="bg-danger text-white">
      <td><?php echo date("Y-m-d h:i:s A",strtotime($price_resalt['price_addtime'])); ?></td>
	<td><?php echo $price_resalt['price_name']; ?></td>
      <td align="right"><?php echo number_format((float)$price_resalt['price_sale'],2); ?></td>
      <td align="center"><a href="two_price.php?bill_id=<?php echo $_GET['bill_id']; ?>&sale_qty=<?php echo $_GET['sale_qty']; ?>&product_barcode=<?php echo $_GET['product_barcode']; ?>&price_id=<?php echo $price_resalt['price_id']; ?>&wholesale_btn_val=<?php echo $_GET['wholesale_btn_val'] ?>" class="btn btn-sm btn-dark">Select</a></td>
    </tr>
<?php } ?>
  </tbody>
</table>
	
<!--if(isset($_GET['bill_id'])&&isset($_GET['sale_qty'])&&isset($_GET['product_barcode'])&&isset($_GET['price_id'])){-->
	  
</body>
</html>