123 lines
3.9 KiB
PHP
123 lines
3.9 KiB
PHP
<?php
|
|
include_once("vars.inc");
|
|
include_once("functions.inc");
|
|
|
|
session_start();
|
|
|
|
if (!isset($_SESSION['user'])) /* disallow access unless authenticated */
|
|
{
|
|
header("Location: $MY_SERVER/login.php");
|
|
exit(0);
|
|
}
|
|
|
|
if (
|
|
!isset($_GET['year']) && preg_match('/^\d+$/', $_GET['year'])
|
|
)
|
|
{
|
|
print('<html><body>No year specified</body></html>');
|
|
exit(0);
|
|
}
|
|
|
|
/* Some variables */
|
|
$userID = get_userID($_SESSION['user']);
|
|
$year = $_GET['year'];
|
|
$tip_totals = Array();
|
|
$check_totals = Array();
|
|
|
|
/* Print some header HTML */
|
|
print <<<EOP
|
|
<html>
|
|
<head>
|
|
<title>TipMan Report for $year</title>
|
|
<link rel="stylesheet" href="report.css" />
|
|
</head>
|
|
<body>
|
|
EOP;
|
|
|
|
/* Do a query to get the tip totals per period */
|
|
$dbc = connect_db();
|
|
$qRes = do_query(<<<EOQ
|
|
SELECT MONTH(tips.date) AS month,
|
|
IF(DAYOFMONTH(tips.date) < 16, 1, 2) AS period,
|
|
SUM(tips.amount) AS tip_total
|
|
FROM tips
|
|
WHERE user = $userID
|
|
AND YEAR(tips.date) = $year
|
|
GROUP BY month, period;
|
|
EOQ
|
|
, $dbc);
|
|
while (($row = mysql_fetch_assoc($qRes)) !== false)
|
|
{
|
|
$tip_totals[$row['month']][$row['period']] = $row['tip_total'];
|
|
}
|
|
|
|
$qRes = do_query(<<<EOQ
|
|
SELECT MONTH(checks.date) AS month,
|
|
IF(DAYOFMONTH(checks.date) < 16, 1, 2) AS period,
|
|
SUM(checks.amount) AS checks_total
|
|
FROM checks
|
|
WHERE user = $userID
|
|
AND YEAR(checks.date) = $year
|
|
GROUP BY month, period;
|
|
EOQ
|
|
, $dbc);
|
|
while (($row = mysql_fetch_assoc($qRes)) !== false)
|
|
{
|
|
$check_totals[$row['month']][$row['period']] = $row['checks_total'];
|
|
}
|
|
|
|
/* Now format the totals in a nice table */
|
|
|
|
$yearTipTotal = 0.0;
|
|
$yearChecksTotal = 0.0;
|
|
print('<table border="0" cellspacing="0" cellpadding="3" style="border: solid 1px #000;">');
|
|
print('<tr><th>Month</th><th>Period</th><th>Tips</th><th>Checks</th><th>Total</th></tr>');
|
|
for ($monthIndex = 1; $monthIndex <= 12; $monthIndex++)
|
|
{
|
|
/* Get a timestamp for the current month */
|
|
$monthTimestamp = mktime(0, 0, 0, $monthIndex, 1, $year);
|
|
|
|
/* Set the totals for each period to 0 if they are not set */
|
|
if (!isset($tip_totals[$monthIndex][1]))
|
|
$tip_totals[$monthIndex][1] = 0;
|
|
if (!isset($tip_totals[$monthIndex][2]))
|
|
$tip_totals[$monthIndex][2] = 0;
|
|
if (!isset($check_totals[$monthIndex][1]))
|
|
$check_totals[$monthIndex][1] = 0;
|
|
if (!isset($check_totals[$monthIndex][2]))
|
|
$check_totals[$monthIndex][2] = 0;
|
|
|
|
/* Print a row in the report table */
|
|
print('<tr>');
|
|
printf('<td rowspan="2">%s</td>', date('F', $monthTimestamp));
|
|
print('<td>1 - 15</td>');
|
|
printf('<td class="amnt">$%.2f</td>', $tip_totals[$monthIndex][1]);
|
|
printf('<td class="amnt">$%.2f</td>', $check_totals[$monthIndex][1]);
|
|
printf('<td rowspan="2" class="amnt">$%.2f</td>',
|
|
$tip_totals[$monthIndex][1] + $tip_totals[$monthIndex][2] +
|
|
$check_totals[$monthIndex][1] + $check_totals[$monthIndex][2]);
|
|
print('</tr><tr>');
|
|
printf('<td>16 - %d</td>', date('t', $monthTimestamp));
|
|
printf('<td class="amnt">$%.2f</td>', $tip_totals[$monthIndex][2]);
|
|
printf('<td class="amnt">$%.2f</td>', $check_totals[$monthIndex][2]);
|
|
print("</tr>\n");
|
|
|
|
/* Increment the year's totals */
|
|
$yearTipTotal += $tip_totals[$monthIndex][1];
|
|
$yearTipTotal += $tip_totals[$monthIndex][2];
|
|
$yearChecksTotal += $check_totals[$monthIndex][1];
|
|
$yearChecksTotal += $check_totals[$monthIndex][2];
|
|
}
|
|
printf('<tr><td colspan="2"
|
|
style="font-alignment: right; font-weight: bold;">Total:</td>
|
|
<td class="amnt">$%.2f</td>
|
|
<td class="amnt">$%.2f</td>
|
|
<td class="amnt">$%.2f</td></tr>',
|
|
$yearTipTotal,
|
|
$yearChecksTotal,
|
|
$yearTipTotal + $yearChecksTotal);
|
|
print('</table>');
|
|
?>
|
|
</body>
|
|
</html>
|