<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<?php session_start();
if(isset($_SESSION['email']))
{
echo "";
}
else
{
	header( "Location: login.php" ); 
}
?>
<html>
 <head>
  <title> New Document </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
 </head>
  <style>
  p{
	  font-family: Calibri;
	  color: black;
	  font-size: 20;
	}
td
{
	font-family: calibri;
	color: black;
	font-size: 20;
	text-align:center;
	background-image:url('lightgray.jpg');
	background-size:100% 100%;
	background-repeat:no-repeat;
}
  </style>

 <body style="background-image:url('gray.jpg');background-size:100% 300%;
background-repeat:no-repeat;">
 <?php
  //retrieving stock data and storing them in the database
$row = 1;
if (($handle = fopen("http://download.finance.yahoo.com/d/quotes.csv?s=AAPL+ADBE+AMZN+APOL+CSCO+CTSH+DELL+ERIC+GOOG+INFY+INTC+MSFT+ORCL+PROV+QCOM+SNDK+SWIR+VRTU+YHOO+ACCL+ACIW+BBSI+BITS+BLKB+BCSI+CVLT+CNQR+CCUR+PROJ+ELRC+ERII+FFDF+FNGN+GSBC+GRNB+GCBC+HALL+HBNK+HBHC+IBOC+JXSB+JFBC+KINS
+LKFN+SIRI+RAX+VVUS+DNDN+EBAY+RMBS&f=nsabp5p2v", "r")) !== FALSE)
{
	$con = mysql_connect("localhost:3306","","");
	if (!$con)
	{
		die('Could not connect: ' . mysql_error());
	}
	mysql_select_db("test", $con);
	mysql_query("delete from stockdata");
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) 
	{
		$num = count($data);
		$row++;
		for ($c=0; $c < $num; $c++) 
		{
		   $var[$c]= $data[$c] ;
		}
		//inserting fields into stockdata database
		mysql_query("INSERT INTO stockdata (company,symbol,ask,bid,price,stockchange,stockvolume)
		VALUES ('$var[0]','$var[1]','$var[2]','$var[3]','$var[4]','$var[5]','$var[6]')");
	}
	
    fclose($handle);
}
$email=$_SESSION['email'];
$useridres=mysql_query("select userid from userdata where Email='$email';");
while($row=mysql_fetch_array($useridres))
{
	$userid=$row['userid'];
}

//extracting the stocks user has bought and sold
$buy_result=mysql_query("SELECT tickersymbol,sum(quantity) as s from orders 
			where userid=$userid and action='buy' and status='completed'
			group by tickersymbol");
$countb=mysql_num_rows($buy_result);
$sell_result=mysql_query("SELECT tickersymbol,sum(quantity) as s from orders 
			where userid=$userid and action='sell' and status='completed'
			group by tickersymbol");
$counts=mysql_num_rows($sell_result);
$i=0;
while($row = mysql_fetch_array($buy_result))
{
	$buy_ticker[$i] = $row['tickersymbol'];
	$buy_quantity[$i]=$row['s'];
	$i++;
}
$j=0;
while($row = mysql_fetch_array($sell_result))
{
	$sell_ticker[$j] = $row['tickersymbol'];
	$sell_quantity[$j]=$row['s'];
	$j++;
}

//getting the stocks and quantity of stocks he owns
$k=0;
$flag=0;
$count=0;
for($l=0;$l<$countb;$l++)
{
	$flag=0;
	for($m=0;$m<$counts;$m++)
	{
		if($buy_ticker[$l]==$sell_ticker[$m])
		{
			$res_ticker[$k]=$buy_ticker[$l];
			$res_quantity[$k]=$buy_quantity[$l]-$sell_quantity[$m];
			$k++;
			$count++;
			$flag=1;
		}
	}
	if($flag==0)
	{
		$res_ticker[$k]=$buy_ticker[$l];
		$res_quantity[$k]=$buy_quantity[$l];
		$k++;
		$count++;
	}
}

//getting the current ask prices of the stocks he owns
$index=0;
for($n=0;$n<$count;$n++)
{
	$ask_result=mysql_query("select ask,company from stockdata where symbol='$res_ticker[$n]'");
	while($row = mysql_fetch_array($ask_result))
{
	$ask[$index] = $row['ask'];
	$res_company[$index]=$row['company'];
}
$index++;
}

//calculating the portfolio value
$current_result=mysql_query("SELECT incentive from portfolio where userid=$userid");
while($row = mysql_fetch_array($current_result))
{
	$incentive	 = $row['incentive'];
}
$portfolio_val=0;
for($o=0;$o<$count;$o++)
{
	$portfolio_val=$portfolio_val+($res_quantity[$o]*$ask[$o]);
}
$currentportfolio=$portfolio_val+$incentive;
//updating the portfolio value
mysql_query("update portfolio set portfoliovalue=$currentportfolio,datemodified=SYSDATE() where userid=$userid;");

//extracting cashbalance
$cashresult=mysql_query("select cashbalance from portfolio where userid=$userid;");
while($row=mysql_fetch_array($cashresult))
{
	$cashbalance=$row['cashbalance'];
}

//displaying data
echo "<p>";
echo "Portfolio-Value:".$currentportfolio."$</br>";
echo "Cash Balance:".$cashbalance."$</br></br>";
echo "STOCK HOLDINGS:"."</br>";
echo "<table>";
echo "<tr>";
echo "<td>COMPANY</td>";
echo "<td>QUANTITY</td>";
echo "</tr>";
for($q=0;$q<$count;$q++)
{
	if($res_quantity[$q]!=0)
	{
	echo "<tr>";
	echo "<td>$res_company[$q]</td>";
	echo "<td>$res_quantity[$q]</td>";
	echo "</tr>";
	}
}
echo "</table>";
echo "</p>";

//displaying pending transactions
$tableresult=mysql_query("select * from orders where userid=$userid and status='pending';");
$count = mysql_num_rows($tableresult);
$i=0;
while($row = mysql_fetch_array($tableresult))
		  {
			$tickersymbol[$i]=$row['tickersymbol'];
			$company[$i]=$row['company'];
			$threshold1[$i]=$row['threshold1'];
			$threshold2[$i]=$row['threshold2'];
			$action[$i]=$row['action'];
			$order_type[$i]=$row['order_type'];
			$quantity[$i]=$row['quantity'];
			$transaction_date[$i]=$row['transaction_date'];
			$status[$i]=$row['status'];
			$i++;
		  }
if($count!=0)
{
	echo "</br></br><p>";
	echo "PENDING TRANSACTIONS:"."</br>";
	echo "<table align='left' width='773px'>";
	echo "<tr>";
	echo "<td>TICKER</td>";
	echo "<td>COMPANY</td>";
	echo "<td>THRESHOLD</td>";
	echo "<td>ACTION</td>";
	echo "<td>ORDER-TYPE</td>";
	echo "<td>QUANTITY</td>";
	echo "<td>DATE</td>";
	echo "<td>STATUS</td>";
	echo "</tr>";
	for($i=0;$i<$count;$i++)
	{
	echo "<tr>";
	echo "<td>$tickersymbol[$i]</td>";
	echo "<td>$company[$i]</td>";
	if($threshold1[$i]!=Null)
	{
		echo "<td>$threshold1[$i]</td>";
	}
	else
	{
		echo "<td>$threshold2[$i]</td>";
	}
	echo "<td>$action[$i]</td>";
	echo "<td>$order_type[$i]</td>";
	echo "<td>$quantity[$i]</td>";
	echo "<td>$transaction_date[$i]</td>";
	echo "<td>$status[$i]</td>";
	echo "</tr>";
	}
	echo "</table>";
	echo "</p>";
}
mysql_close($con);
?>

 </body>
</html>
