tipman/main.php
Josh Holtrop ff1358ea28 added report page
git-svn-id: svn://anubis/tipman@2 ce01c143-e732-0410-ac0e-c064f6e6c7ef
2008-09-08 01:52:45 +00:00

384 lines
14 KiB
PHP

<?php
include("vars.inc");
include("functions.inc");
session_start();
if (!isset($_SESSION['user'])) /* disallow access unless authenticated */
{
header("Location: $MY_SERVER/login.php");
exit(0);
}
/*
* Draw month bar on left
* $whichOne : 0-($captions - 1) index of selected one
* $showTips : initial value of calendar's visibility
*/
function draw_nav_bar($whichOne, $showTips, $showStats)
{
global $userID, $MY_SERVER;
$month_val = $_SESSION['month'] . $_SESSION['year'];
for ($i = 0; $i < $whichOne; $i++)
$month_val = get_back_month($month_val);
echo '<div class="nav">';
echo '<div style="margin-left: auto; margin-right: auto; width: 40px;">';
echo round_table('<a href="javascript:nav_up();"><img style="border-style: none; margin: 0px; padding: 0px;" src="images/up_arrow.png" alt="up" /></a>',
1) . '</div>';
echo spacer(1, 10);
for ($i = 0; $i < 5; $i++)
{
$month_cap = date('F', mktime(1, 1, 1, substr($month_val, 0, 2))) .
', ' . substr($month_val, 2, 4);
echo round_table(
'<form id="nav' . $i . '" action="' . $_SERVER['PHP_SELF'] . '" method="post" >' .
'<input type="hidden" name="action" value="change_month" />' .
'<input type="hidden" name="nav_month" value="' . $i . '" />' .
'<input type="hidden" id="nav_month' . $i .
'" name="new_month" value="' . $month_val .
'" />' .
'<input type="hidden" id="nav_tips' . $i . '" name="show_tips" value="' . $showTips . '" />' .
'<input type="hidden" id="nav_stats' . $i . '" name="show_stats" value="' . $showStats . '" />' .
'<a id="nav_link' . $i . '" class="nav_link" href="javascript:document.getElementById(\'nav' . $i . '\').submit();">' .
(($i == $whichOne) ? "<b>$month_cap</b>" : $month_cap) .
'</a>' .
'</form>',
1);
echo spacer(1, 10);
$month_val = get_next_month($month_val);
}
echo '<div style="margin-left: auto; margin-right: auto; width: 40px;">';
echo round_table('<a href="javascript:nav_down();"><img style="border-style: none; margin: 0px; padding: 0px;" src="images/down_arrow.png" alt="up" /></a>', 1) . '</div>';
###################
# Show year total #
###################
$year_total = 0.0;
for ($i = 1; $i < 13; $i++)
{
$tips = get_tips($i, $_SESSION['year'], $userID);
for ($j = 0; $j < 31; $j++)
$year_total += $tips[$j];
$checks = get_checks($i, $_SESSION['year'], $userID);
$year_total += $checks['amount1'];
$year_total += $checks['amount2'];
}
$reportLink = sprintf('<br><a href="%s/report.php?year=%s">%s report</a>',
$MY_SERVER,
$_SESSION['year'],
$_SESSION['year']
);
echo spacer(1,20);
echo '<div style="margin-left: auto; margin-right: auto; width: 8em;">' .
round_table($_SESSION['year'] . ' total: $' . $year_total . $reportLink, 1) . '</div>';
echo spacer(1, 30);
echo '<div style="margin-left: auto; margin-right: auto; width: 6em;">' . round_table('<a class="nav_link" href="' . $MY_SERVER . '/login.php">Logout</a>', 1) . '</div>';
echo '</div>';
}
/* Returns calendar HTML */
function calendar($month, $year, $tipVals, $showStats)
{
$day_captions = array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
$dateStamp = mktime(0, 0, 0, $month, 1, $year);
$dow = date('w', $dateStamp); /* 0-6 */
$ret = '<hr /><br /><form action="' . $_SESSION['PHP_SELF'] . '" method="post">' .
'<input type="hidden" name="show_tips" value="1" />' .
'<input id="show_stats04" type="hidden" name="show_stats" value="' . $showStats . '" />' .
'<input type="hidden" name="action" value="save_tips" />' .
'<input type="hidden" name="month" value="' . $_SESSION['month'] . '" />' .
'<input type="hidden" name="year" value="' . $_SESSION['year'] . '" />' .
'<table class="cal" cellpadding="0" cellspacing="0"><tr>';
for ($i = 0; $i < 7; $i++)
$ret .= '<td class="cal">' . $day_captions[$i] . '</td>';
$ret .= '</tr><tr>';
for($i = 0; $i < $dow; $i++)
$ret .= '<td class="cal">&nbsp;</td>'; /* pad beginning of month */
$col = $dow;
$dim = date('t', $dateStamp);
for ($i = 0; $i < $dim; $i++)
{
if ($tipVals[$i] == 0)
$tipVals[$i] = '';
$io = $i + 1;
if ($col == 0)
$ret .= '<tr>';
$ret .= <<< EOE
<td class="cal">$io<br />
<input type="text" name="tip$i" value="$tipVals[$i]" size="5" />
</td>
EOE;
$col++;
if ($col == 7)
{
$ret .= "</tr>\n<tr>";
$col = 0;
}
}
if ($col > 0)
{
for (; $col < 7; $col++)
$ret .= '<td class="cal">&nbsp;</td>';
$ret .= '</tr>';
}
$ret .= '</table><br /><div style="text-align: center;"><input type="submit" value="Save Tips" /> &nbsp; <input type="reset" value="Clear Changes" /></div></form>';
return $ret;
}
/* Returns statistics HTML */
function stats($month, $year, $tipVals)
{
$MAX_HEIGHT = 100;
$day_captions = array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
$dayCounts = array();
$tipTotals = array();
for ($i = 0; $i < 7; $i++)
{
$dayCounts[] = 0;
$tipTotals[] = 0.0;
}
$dateStamp = mktime(0, 0, 0, $month, 1, $year);
$dow = date('w', $dateStamp); /* 0-6 */
for ($i = 0; $i < 31; $i++)
{
if (isset($tipVals[$i]))
{
$dayCounts[$dow]++;
$tipTotals[$dow] += $tipVals[$i];
}
$dow = ($dow + 1) % 7;
}
$tipAverages = array();
for ($i = 0; $i < 7; $i++)
$tipAverages[$i] = $tipTotals[$i] / $dayCounts[$dow];
$max = $tipAverages[0];
if ($max == 0)
$max = 1;
for ($i = 0; $i < 7; $i++)
if ($tipAverages[$i] > $max)
$max = $tipAverages[$i];
$ret = '<table class="stat" border="0" cellpadding="0" cellspacing="0"><tr>';
$ret .= '<td style="text-align: right; font-size: smaller;" valign="top">$' . ((int) $max) . ' - </td>';
for ($i = 0; $i < 7; $i++)
$ret .= '<td rowspan="2" class="statbar" valign="bottom"><img src="images/red.png" alt="bar" width="25" height="' .
((int)($tipAverages[$i] * $MAX_HEIGHT / $max)) . '" /></td>';
$ret .= '</tr><tr><td style="text-align: right; font-size: smaller;" valign="top">$' .
((int)($max / 2)) . ' - </td></tr><tr><td style="text-align: right; font-size: smaller;" valign="top">$0 - </td>';
for ($i = 0; $i < 7; $i++)
$ret .= '<td class="stat">' . $day_captions[$i] . '</td>';
$ret .= '</tr></table>';
return $ret;
}
/* Initialization */
if (!isset($_SESSION['year']))
$_SESSION['year'] = date('Y');
if (!isset($_SESSION['month']))
$_SESSION['month'] = date('m');
$userID = get_userID($_SESSION['user']);
/* Handle POST form input */
if ($_POST['action'] == 'change_month')
{
if (preg_match('/^\d{6}$/', $_POST['new_month']))
{
$_SESSION['month'] = substr($_POST['new_month'], 0, 2);
$_SESSION['year'] = substr($_POST['new_month'], 2, 4);
}
}
elseif ($_POST['action'] == 'save_tips')
{
$dbc = connect_db();
for ($i = 0; $i < 31; $i++)
{
if (isset($_POST['tip' . $i]))
{
$val = $_POST['tip' . $i];
if (preg_match('/^\d*\.?\d{0,2}$/', $val))
{
$day = $i + 1;
if (strlen($day) < 2)
$day = "0$day";
do_query("DELETE FROM `tips` WHERE `date` = '$_SESSION[year]-$_SESSION[month]-$day' AND `user` = '$userID'", $dbc);
if ($val != 0)
do_query("INSERT INTO `tips` VALUES('', $userID, '$_SESSION[year]-$_SESSION[month]-$day', '$val')", $dbc);
}
}
}
mysql_close($dbc);
}
elseif ($_POST['action'] == 'save_checks')
{
$dbc = connect_db();
$pattern = '/^\d*\.?\d{0,2}$/';
$h = $_POST['hours1'];
$a = $_POST['amount1'];
if (preg_match($pattern, $h) && preg_match($pattern, $a))
{
do_query("DELETE FROM `checks` WHERE `date` = '$_SESSION[year]-$_SESSION[month]-15' AND `user` = '$userID'", $dbc);
if ( ($h != 0) || ($a != 0) )
do_query("INSERT INTO `checks` VALUES('', '$userID', '$_SESSION[year]-$_SESSION[month]-15', '$h', '$a')", $dbc);
}
$h = $_POST['hours2'];
$a = $_POST['amount2'];
if (preg_match($pattern, $h) && preg_match($pattern, $a))
{
$dim = date('t', mktime(1, 1, 1, $_SESSION['month'], 1, $_SESSION['year']));
do_query("DELETE FROM `checks` WHERE `date` = '$_SESSION[year]-$_SESSION[month]-$dim' AND `user` = '$userID'", $dbc);
if ( ($h != 0) || ($a != 0) )
do_query("INSERT INTO `checks` VALUES('', '$userID', '$_SESSION[year]-$_SESSION[month]-$dim', '$h', '$a')", $dbc);
}
mysql_close($dbc);
}
/* Set us up some variables for user later on */
$thisMonth = date('F', mktime(0, 0, 0, $_SESSION['month']));
$thisYear = date('Y', mktime(0, 0, 0, 1, 1, $_SESSION['year']));
$nextMonth = sprintf('%02d', (($_SESSION['month'] % 12) + 1)) . (($_SESSION['month'] == 12) ? $thisYear + 1 : $thisYear);
$prevMonth = sprintf('%02d', ((($_SESSION['month'] + 10) % 12) + 1)) . (($_SESSION['month'] == 1) ? $thisYear - 1 : $thisYear);
$showTips = ( isset($_POST['show_tips']) && ($_POST['show_tips'] == 1) ) ? 1 : 0;
$showStats = ( isset($_POST['show_stats']) && ($_POST['show_stats'] == 1) ) ? 1 : 0;
$navMonth = $_POST['nav_month'];
$navMonth = ( isset($navMonth) && $navMonth >= 0 && $navMonth < 5 ) ? $navMonth : 3;
?>
<html>
<head>
<title><?php echo $APP_TITLE; ?></title>
<link rel="stylesheet" href="objects.css" />
<link rel="stylesheet" href="main.css" />
<script language="javascript" type="text/javascript" src="functions.js">
</script>
<script language="javascript" type="text/javascript">
<?php
$firstNavMonth = $_SESSION['month'] . $_SESSION['year'];
for ($i = 0; $i < $navMonth; $i++)
$firstNavMonth = get_back_month($firstNavMonth);
echo 'var navFirstMonth = new String("' . $firstNavMonth . '");' . "\n";
echo 'var navIndex = ' . $navMonth . ';' . "\n";
?>
</script>
<style type="text/css">
<?php
if ($showTips)
echo 'div#no-calendar { display: none; }';
else
echo 'div#calendar { display: none }';
if ($showStats)
echo 'div#no-stats { display: none; }';
else
echo 'div#stats { display: none }';
?>
</style>
</head>
<body>
<?php
/* Draw navigation bar */
draw_nav_bar($navMonth, $showTips, $showStats);
/* Draw title bar */
echo round_table( <<< EOE
<table width="100%">
<tr>
<td width="15">
<form action="$_SERVER[PHP_SELF]" method="post">
<input id="show_tips01" type="hidden" name="show_tips" value="$showTips" />
<input id="show_stats01" type="hidden" name="show_stats" value="$showStats" />
<input type="hidden" name="nav_month" value="$navMonth" />
<input type="hidden" name="action" value="change_month" />
<input type="hidden" name="new_month" value="$prevMonth" />
<input type="image" src="images/left_arrow.png" alt="backward_month" />
</form>
</td>
<td style="text-align: center;"><b>$thisMonth, $thisYear</b></td>
<td width="15">
<form action="$_SERVER[PHP_SELF]" method="post">
<input id="show_tips02" type="hidden" name="show_tips" value="$showTips" />
<input id="show_stats02" type="hidden" name="show_stats" value="$showStats" />
<input type="hidden" name="nav_month" value="$navMonth" />
<input type="hidden" name="action" value="change_month" />
<input type="hidden" name="new_month" value="$nextMonth" />
<input type="image" src="images/right_arrow.png" alt="forward_month" />
</form>
</td>
</tr>
</table>
EOE
);
?>
<br />
<?php
/* Draw main body */
$tips = get_tips($_SESSION['month'], $_SESSION['year'], $userID);
$tip1_total = 0.0;
$tip2_total = 0.0;
for ($i = 0; $i < 15; $i++)
if (isset($tips[$i]))
$tip1_total += $tips[$i];
for ($i = 15; $i < 31; $i++)
if (isset($tips[$i]))
$tip2_total += $tips[$i];
$checks = get_checks($_SESSION['month'], $_SESSION['year'], $userID);
echo round_table( <<< EOE
<div id="no-calendar">
<span class="smaller"><a class="switch" href="javascript:showTips()">Edit Tips >>></a></span>
</div>
<div id="calendar">
<span class="smaller"><a class="switch" href="javascript:hideTips()"><<< Hide Tips</a></span>
<br />
EOE
. calendar($_SESSION['month'], $_SESSION['year'], $tips, $showStats) . <<< EOE
</div>
<hr />
<div id="no-stats">
<span class="smaller"><a class="switch" href="javascript:showStats()">Show Stats >>></a></span>
</div>
<div id="stats">
<span class="smaller"><a class="switch" href="javascript:hideStats()"><<< Hide Stats</a></span>
<br />
EOE
. stats($_SESSION['month'], $_SESSION['year'], $tips) . <<< EOE
</div>
<hr />
EOE
. '<form action="' . $_SESSION['PHP_SELF'] . '" method="post" >' .
'<input type="hidden" name="action" value="save_checks" />' .
'<b>' . $thisMonth . ' 1 - 15</b><br />' .
'<br />Hours: ' .
'<input type="text" name="hours1" value="' . $checks['hours1'] . '" size="8" />' .
' &nbsp; Check Amount: ' .
'<input type="text" name="amount1" value="' . $checks['amount1'] . '" size="8" />' .
' &nbsp; Tips: $' . $tip1_total .
'<br /><br /><b>' . $thisMonth . ' 16 - ' .
date('t', mktime(1, 1, 1, $_SESSION['month'], 1, $_SESSION['year'])) . '</b><br />' .
'<br />Hours: ' .
'<input type="text" name="hours2" value="' . $checks['hours2'] . '" size="8" />' .
' &nbsp; Check Amount: ' .
'<input type="text" name="amount2" value="' . $checks['amount2'] . '" size="8" />' .
' &nbsp; Tips: $' . $tip2_total .
'<br /><br /><input type="submit" value="Save" /> &nbsp; <input type="reset" value="Clear Changes" />' .
'<input id="show_tips03" type="hidden" name="show_tips" value="' . $showTips . '" />' .
'<input id="show_stats03" type="hidden" name="show_stats" value="' . $showStats . '" />' .
'<input type="hidden" name="nav_month" value="' . $navMonth . '" />' .
'</form>' .
'<hr />' .
'<div style="text-align: right;">' .
"<b>$thisMonth Total: </b> $" . ($tip1_total + $tip2_total + $checks['amount1'] + $checks['amount2']) .
' &nbsp; </div>'
,
0);
echo '<br />';
echo footer();
?>
</body>
</html>