<?
session_start();
include("conexion.php");
$query = "SELECT id AS usersell FROM User WHERE login ='".$_SESSION['login']."'";
$result = mysql_query($query, $conexion);
$row = mysql_fetch_array($result);

$ownerid = $_POST["ownerid"];
$volume = $_POST["volume"];
$price = $_POST["price"];
$stock = $_POST["stock"];



//check if someone wants to buy this
$query = "SELECT user AS buyer, id AS idbuy, stock AS stock, volume FROM Buy WHERE stock =".$stock." AND price=".$price;
$result = mysql_query($query, $conexion);
if (mysql_affected_rows($conexion) > 0) {
	$row2 = mysql_fetch_array($result);	
	
	$insertedvolume = $volume;
	if($volume > $row2["volume"])
	{
		$insertedvolume = $row2["volume"];
	}	
	
	//delete or update row of table Buy
	if($row2["volume"] <= $volume)
	{
		$query = "delete from Buy where id = ".$row2["idbuy"];
		$result = mysql_query($query, $conexion);
	}
	else
	{	
		if($row2['volume']-$volume>0){
			$query = "update Buy set volume=volume -".$insertedvolume." where id = ".$row2["idbuy"];
			$result = mysql_query($query, $conexion);
			
		}
		else
		{
			$query = "delete from Buy where id = ".$row2["idbuy"];
			$result = mysql_query($query, $conexion);
		}
	}	
	


	
	//The user who buys will have more stocks
	$query = "select * from Ownership where user=".$row2["buyer"]." and stock=".$row2["stock"]." and price=".$price;
	$result = mysql_query($query, $conexion);
	if (mysql_affected_rows($conexion) == 0) {
		$query = "insert into Ownership(user, stock, volume, price) values (".$row2["buyer"].",".$row2["stock"].",".$insertedvolume.",".$price.")";
		mysql_query($query, $conexion);
	} else {
		$query = "update Ownership set volume=volume +".$insertedvolume." where user='".$row2["buyer"]."' and stock=".$row2["stock"];
		mysql_query($query, $conexion);
	}
	
	//The user that wants to buy will have less cash
	$query = "SELECT * FROM User WHERE id = ".$row2["buyer"];
	$result = mysql_query($query, $conexion);
	$rowinfobuyer = mysql_fetch_array($result);	
	$amount = $price * $insertedvolume;
	$total = $rowinfobuyer["cash"] - $amount;
	$query = "update User set cash=".$total." where id='".$row2['buyer']."'";
	mysql_query($query, $conexion);

	
	//insert or update row in the table Sell
	
	$insertedsellvolume = $volume-$insertedvolume;
	$query = "SELECT * FROM Sell WHERE price=".$price." AND stock=".$stock." AND user=".$row["usersell"];
	$result = mysql_query($query, $conexion);
	if (mysql_affected_rows($conexion) > 0) {
		$query = "update Sell set volume=volume +".$insertedsellvolume." where user=".$row["usersell"]." and stock=".$stock." and price=".$price;
		$result = mysql_query($query, $conexion);
	}
	else{
		if($insertedsellvolume > 0)
		{
			$query = "insert into Sell(price, volume, stock, user) values (".$price.",".$insertedsellvolume.",".$stock.",".$row["usersell"].")";
			$result = mysql_query($query, $conexion);
		}
	
	}
	
	
	//The user who sells will have less stocks
	$query = "update Ownership set volume=volume -".$insertedvolume." where user='".$row["usersell"]."' and stock=".$stock;
	mysql_query($query, $conexion);
	
	//Delete the row with volume=0 in the table ownership
	$query = "delete from Ownership where volume <= 0";
	$result = mysql_query($query, $conexion);

	
	//The user who sells will have more cash
	$usertosell = $row["usersell"];
	$query = "update User set cash=cash +".$amount." where id = ".$usertosell;
	mysql_query($query, $conexion);
	
	//Insert the info in History
	$query = "insert into History(time, price, buyer, seller, stock, volume) values (now(),".$price.",".$row2["buyer"].",".$usertosell.",".$stock.",".$insertedvolume.")";
	mysql_query($query, $conexion);


}
else{
	$query = "SELECT * FROM Sell WHERE price=".$price." AND stock=".$stock." AND user=".$row["usersell"];
	$result = mysql_query($query, $conexion);
	if (mysql_affected_rows($conexion) > 0) {
		$query = "update Sell set volume=volume +".$volume." where user=".$row["usersell"]." and stock=".$stock." and price=".$price;
		$result = mysql_query($query, $conexion);
	}
	else{

		$query = "insert into Sell(price, volume, stock, user) values (".$price.",".$volume.",".$stock.",".$row["usersell"].")";
		$result = mysql_query($query, $conexion);
	
	}

}


header("Location: ../index.php");
?>
