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

$ticker = $_POST["ticker"];
$volume = $_POST["volume"];
$price = $_POST["price"];

$amount = $price * $volume ; 
$total = $row["cash"] - $amount;
if ($row["cash"] > $amount){

	

	
	
	
	//check if someone wants to sell this
	$query = "SELECT user AS seller, id AS idsell, stock AS stock, volume FROM Sell WHERE stock =".$ticker." AND price=".$price;
	$result = mysql_query($query, $conexion);
	if (mysql_affected_rows($conexion) > 0) {
		$row2 = mysql_fetch_array($result);	
	
	
	//delete or update row of table Sell
	if($row2["volume"] <= $volume)
	{
		$query = "delete from Sell where id = ".$row2["idsell"];
		$result = mysql_query($query, $conexion);
	}
	else
	{
		$query = "update Sell set volume=volume -".$volume." where user=".$row2["seller"]." and stock=".$ticker." and price=".$price;
		$result = mysql_query($query, $conexion);
		
	}
	//The user who buys will have more stocks
	$query = "select * from Ownership where user=".$row["id"]." and stock=".$ticker." and price=".$price;
	
	$result = mysql_query($query, $conexion);
	$insertedvolume = $volume;
	if($volume > $row2["volume"])
	{
		$insertedvolume = $row2["volume"];
	}
	if (mysql_affected_rows($conexion) == 0) {
		$query = "insert into Ownership(user, stock, volume, price) values (".$row["id"].",".$ticker.",".$insertedvolume.",".$price.")";
		mysql_query($query, $conexion);
		
	} else {
		$query = "update Ownership set volume=volume +".$insertedvolume." where user=".$row["id"]." and stock=".$ticker." and price=".$price;
		mysql_query($query, $conexion);
		
	}
	
	//The user that wants to buy will have less cash
	$query = "SELECT * FROM User WHERE id = ".$row["id"];
	$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='".$row['id']."'";
	mysql_query($query, $conexion);


$insertedbuyvolume = $volume - $insertedvolume;
$query = "SELECT * FROM Buy WHERE price=".$price." AND stock=".$ticker." AND user=".$row["id"];
$result = mysql_query($query, $conexion);
if (mysql_affected_rows($conexion) > 0) {		
	$query = "update Buy set volume=volume +".$insertedbuyvolume." where price=".$price." AND stock=".$ticker." AND user=".$row["id"];
	$result = mysql_query($query, $conexion);
}
else{	
	if($insertedbuyvolume > 0)
	{
		$query = "insert into Buy(price, volume, stock, user) values (".$price.",".$insertedbuyvolume.",".$ticker.",".$row["id"].")";
		$result = mysql_query($query, $conexion);		
	}
}

	
	//The user who sells will have less stocks
	$query = "update Ownership set volume=volume -".$row2["volume"]." where user='".$row2["seller"]."' and stock=".$ticker;
	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 = $row2["seller"];
	$query = "update User set cash=cash +".$amount." where id = ".$seller;
	mysql_query($query, $conexion);
	
	//Insert the info in History
	$query = "insert into History(time, price, buyer, seller, stock, volume) values (now(),".$price.",".$row["id"].",".$seller.",".$ticker.",".$insertedvolume.")";
	mysql_query($query, $conexion);
	
	


}
else{

	$query = "SELECT * FROM Buy WHERE price=".$price." AND stock=".$ticker." AND user=".$row["id"];
	$result = mysql_query($query, $conexion);
	if (mysql_affected_rows($conexion) > 0) {		
		$query = "update Buy set volume=volume +".$volume." where price=".$price." AND stock=".$ticker." AND user=".$row["id"];
		$result = mysql_query($query, $conexion);
	}
	else{	
		$query = "insert into Buy(price, volume, stock, user) values (".$price.",".$volume.",".$ticker.",".$row["id"].")";
		$result = mysql_query($query, $conexion);		
	}

}

	
	
}	
	header("Location: ../index.php");





?>