<% Option Explicit %>
<!--#include file="config.asp"-->
<!--#include file="common.asp"-->
<!--#include file="adm_admin.asp"-->
<%
	Dim TotalIncome, TotalWaitingPayments, TotalPayouts, MyCommissions, Balance

	PageHeader


	If Session("IsAdminLogin") Then
		PageMenu
	Else
		PageLogin
		Response.End
	End If


	Dim rs

	Command	= Request.Querystring("Command")
%>
	<br>
	<table border=0 width=100%>
	<tr valign=top>
		<td>
			<% MoneyBalance %><br>
			<% PreferedSpends %>
		</td>
		<td>
			<% IncomeLast30 %>
		</td>
		<td>
			<% PayoutsLast30 %>
		</td>
	</tr>
	</table>

	</body>
	</html>
<%


	Sub MoneyBalance()

		Dim rs

		OpenConnection

		Set rs = DBC.Execute("SELECT SUM(Cash) FROM PayIns")
		TotalIncome = Str2Float(rs(0))

		Set rs = DBC.Execute("SELECT SUM(Cash) FROM PayOuts WHERE BatchNumber <> 0 And To <> " & MyEGold)
		TotalPayouts = Str2Float(rs(0))

		Set rs = DBC.Execute("SELECT SUM(Cash), COUNT(*) FROM PayOuts WHERE BatchNumber = 0 And To <> " & MyEGold)
		TotalWaitingPayments	= Str2Float(rs(0))

		Set rs = DBC.Execute("SELECT SUM(Cash) FROM PayIns WHERE Paid = True")
		MyCommissions	= Str2Float(rs(0)) * MyCommission / 100

		Set rs = DBC.Execute("SELECT SUM(Cash) FROM PayOuts WHERE To = " & MyEGold)

		If Str2Float(rs(0)) > 0 Then
			MyCommissions = Str2Float(MyCommissions + rs(0))
		End If


		Balance = TotalIncome - (TotalWaitingPayments + TotalPayouts) - MyCommissions
%>
		<table border=0 align=center bgcolor=006699 cellspacing=1 cellpadding=2 width=240>
		<tr bgcolor=006699 class=PageText>
			<td align=center colspan=2>
				<font color=white>Balance</font>
			</td>
		</tr>
		<tr bgcolor=white class=PageText>
			<td>
				Total investments
			</td>
			<td align=right width=60>
				$<%= FormatNumber(TotalIncome, 2) %>
			</td>
		</tr>
		<tr bgcolor=white class=PageText>
			<td>
				Total payouts
			</td>
			<td align=right>
				$<%= FormatNumber(TotalPayouts, 2) %>
			</td>
		</tr>
		<tr bgcolor=white class=PageText>
			<td>
				Total waiting payments
			</td>
			<td align=right>
				$<%= FormatNumber(TotalWaitingPayments, 2) %>
			</td>
		</tr>
		<tr bgcolor=white class=PageText>
			<td>
				Money for the next payout
			</td>
			<td align=right>
				$<%= FormatNumber(Balance, 2) %>
			</td>
		</tr>
		<tr bgcolor=00FF00 class=PageText>
			<td>
				My earnings
			</td>
			<td align=right>
				$<%= FormatNumber(MyCommissions, 2) %>
			</td>
		</tr>
		</table>
<%
	End Sub


	Sub PreferedSpends()
%>
		<table border=0 align=center bgcolor=006699 cellspacing=1 cellpadding=2 width=240>
		<tr bgcolor=006699 class=PageText>
			<td align=center colspan=2>
				<font color=white>Prefered Spends</font>
			</td>
		</tr>
<%
		Set rs = DBC.Execute("SELECT Cash, COUNT(*) AS Count FROM PayIns GROUP BY Cash ORDER BY COUNT(*) DESC")

		Do While Not rs.EOF
%>
		<tr bgcolor=white class=PageText>
			<td align=right width=60>
				$<%= FormatNumber(rs("Cash"), 2) %>
			</td>
			<td>
				<%= rs("Count") %> time(s)
			</td>
		</tr>
<%
			rs.MoveNext
		Loop
%>
		</table>
<%
	End Sub


	Sub IncomeLast30()
		Dim Sum
%>
		<table border=0 align=center bgcolor=006699 cellspacing=1 cellpadding=2 width=180>
		<tr bgcolor=006699 class=PageText>
			<td align=center colspan=2>
				<font color=white>Income for the last 30 days</font>
			</td>
		</tr>
<%
		Set rs = DBC.Execute("SELECT TOP 30 CreatedOn, SUM(Cash) as Cash4ADay " & _
						"FROM PayIns " & _
						"GROUP BY CreatedOn " & _
						"ORDER BY CreatedOn DESC")

		Sum = 0

		Do While Not rs.EOF
%>
		<tr bgcolor=white class=PageText>
			<td align=right>
				<%= rs("CreatedOn") %>
			</td>
			<td align=right>
				$<%= FormatNumber(rs("Cash4ADay"), 2) %>
			</td>
		</tr>
<%
			Sum = Sum + rs("Cash4ADay")
			rs.MoveNext
		Loop
%>
		<tr bgcolor=80AAFF class=PageText>
			<td align=right>
				<b>Total</b>
			</td>
			<td align=right>
				<b>$<%= FormatNumber(Sum, 2) %></b>
			</td>
		</tr>
		</table>
<%
	End Sub


	Sub PayoutsLast30()
		Dim Sum
%>
		<table border=0 align=center bgcolor=006699 cellspacing=1 cellpadding=2 width=180>
		<tr bgcolor=006699 class=PageText>
			<td align=center colspan=2>
				<font color=white>Payouts for the last 30 days</font>
			</td>
		</tr>
<%
		Set rs = DBC.Execute("SELECT TOP 30 PayoutDate, SUM(Cash) as Cash4ADay " & _
						"FROM PayOuts " & _
						"WHERE BatchNumber <> 0 " & _
						"GROUP BY PayoutDate " & _
						"ORDER BY PayoutDate DESC")

		Sum = 0

		Do While Not rs.EOF
%>
		<tr bgcolor=white class=PageText>
			<td align=right>
				<%= rs("PayoutDate") %>
			</td>
			<td align=right>
				$<%= FormatNumber(rs("Cash4ADay"), 2) %>
			</td>
		</tr>
<%
			Sum = Sum + rs("Cash4ADay")
			rs.MoveNext
		Loop
%>
		<tr bgcolor=80AAFF class=PageText>
			<td align=right>
				<b>Total</b>
			</td>
			<td align=right>
				<b>$<%= FormatNumber(Sum, 2) %></b>
			</td>
		</tr>
		</table>
<%
	End Sub
%>