<?php
	session_start();
	include('includes/config.php');
	include('includes/functions.php');
	
	if ($_COOKIE['oam_username'] != md5($settings['user']) && $_COOKIE['oam_pass'] != md5($settings['pass'])) {
		header("Location:login.php");
	}
	
	$result = mysql_query('
		SELECT *
		FROM oam_listings
		WHERE  listings_date_bought != "0000-00-00 00:00:00"
		ORDER BY listings_date_bought ASC
		LIMIT 1');
	$first_listing = mysql_fetch_array($result);
	$first_month = date('m',strtotime($first_listing['listings_date_bought']));
	$first_year = date('Y',strtotime($first_listing['listings_date_bought']));
	
	$real_next_month = date('m',time()+60*60*24*28);
	$real_next_year = date('Y',time()+60*60*24*28);
	while ($first_month != $real_next_month || $first_year != $real_next_year) {
		//echo $first_month. '!='.$real_next_month .'&&'. $first_year .'!=' .$real_next_year . '<hr>';
		$earned_total = '';
		$spent_total = '';
		if ($first_month == 12) {
			$next_month = '01';
			$next_year = $first_year + 1;
		} else {
			$next_month = $first_month + 1;
			if (strlen($next_month) == 1) {
				$next_month = '0' . $next_month;
			}
			$next_year = $first_year;
		}
		$sql = '
			SELECT *
			FROM oam_listings
			WHERE listings_date_bought > "' . $first_year . '-' . $first_month . '-00 00:00:00" and
				listings_date_bought <= "' . $next_year . '-' . $next_month . '-00 00:00:00"';
		$result = mysql_query($sql);
		while ($row = mysql_fetch_array($result)) {
			$spent_total = $spent_total + $row['listings_bought_price'] + $row['listings_listing_fee'] + $row['listings_sale_fee'] + $row['listings_shipping_fee'];
		}
		$result = mysql_query('
			SELECT *
			FROM oam_listings
			WHERE listings_sold_date > "' . $first_year . '-' . $first_month . '-00 00:00:00" and
				listings_sold_date <= "' . $next_year . '-' . $next_month . '-00 00:00:00"');
		while ($row = mysql_fetch_array($result)) {
			$earned_total = $earned_total + $row['listings_sold_price'] + $row['listings_shipping_charge'];
		}
		
		$row_style = ternary($row_style,'main-row-1','main-row-2');
		$profit = $earned_total - $spent_total;
		$profit = number_format($profit,2);
		if ($profit < 0) {
			$profit = '<div class="red"><strong>$' . $profit  . '</strong></div>';
		} else {
			$profit = '<div class="green"><strong>$' . $profit  . '</strong></div>';
		}
			
		$first_year = $next_year;
		$first_month = $next_month;
		$expense_table = '
			<tr>
				<td class="' . $row_style . '">' . date('M',strtotime($first_year . '-' . $first_month . '-00 00:00:00')) . '</td>
				<td class="' . $row_style . '">' . date('Y',strtotime($first_year . '-' . $first_month . '-00 00:00:00')) . '</td>
				<td class="' . $row_style . '">$' . number_format($spent_total,2) . '</td>
				<td class="' . $row_style . '">$' .number_format($earned_total,2) . '</td>
				<td class="' . $row_style . '">' . $profit  . '</td>
			</tr>' . $expense_table;
	}
		
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="includes/styles.css">
<script type="text/javascript" src="includes/toggle_div.js"></script>
</head>
<body>
	<h2>Earnings Statistics</h2>
<p><a href="add-listings.php">Add New Listings</a> | <a href="index.php">Manage Listings</a> | <a href="stats.php">Earnings Statistics</a> | <a href="login.php">Log Out</a> | <a href="instructions.php">Instructions</a></p>
	<table class="main-table">
		<tr>
			<th>Month</th>
			<th>Year</th>
			<th>Expense</th>
			<th>Income</th>
			<th>Profit</th>
		</tr><?php echo $expense_table; ?>
	</table>
</body>
<p>Auction Business Manager Developed by <a href="http://www.small-business-ideas.net">www.Small-Business-Ideas.net</a></p>
</html>