#!/usr/bin/perl -w
# http://sandiegogeologists.org/cgi-bin/results.cgi: reports meeting reservations and food choice totals
# Carolyn Glockhoff  12 March 2002   this version sorts and displays reservations by last name   REV 5/16/05
# revised 11/12/2007 for PayPal processing      08/14/2008 all reservations (including PayPal on one list)

use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);

print <<END_TOP;
Content-type: text/html

<html>
<head>
<title>Meeting and Food Reservation Results</title>
<style type="text/css">
<!--
body, td, h3 { font-family: Arial, Helvetica, sans-serif; color:#000000}
h1 {  font-family: Lucida Calligraphy, chancery, cursive, script, Legrand, fantasy, arial, verdana}
.red { color:#ff0000};

-->
</style>
</head>
<body BGCOLOR="#CCCCCC" background="../images/Granite.gif" TEXT="#000000"> 
<h1><IMG SRC="../images/Food.gif" alt="dinner"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Meeting and Food Reservation Results</h1>
<br>
<table SUMMARY="reservation table" WIDTH="100%" CELLSPACING="1" CELLPADDING="2">
	<tr>
		<td> <b>Pre-<br>Paid</b></td>
		<td> <b>Saving a place for: </b></td>
		<td> <b>Company/School: </b></td>
		<td> <b>Contact Info: </b></td>
		<td> <b>Food Choice: </b></td>
		<td> <b>Cost: </b></td>
		<td> <b>Date: </b></td>
	</tr>
END_TOP

no strict;

$headCount = 0;
$studentval = 0;  #number in headcount paying student rate

open (FOODHASH, "<../foodhash.txt") || dienice ("Can't open foodhash.txt for reading: $!");
#read in food choices and initialize each choice at zero quantity
close(FOODHASH);

open (RESERVATIONS, "<../reservations.txt") || dienice ("Can't open reservations.txt for reading: $!");
@reslist = <RESERVATIONS>;
close(RESERVATIONS);

@shuffled = ();   #@shuffled is the alphabetically sorted array of last names and starts empty

$i = 0;
foreach $a (@reslist) {
	@shuffled = sort { $a cmp $b } @reslist;
	$i++;
}

foreach $line (@shuffled) {
	($lname, $name, $student, $member, $from, $contact, $food, $cost, $pp, $date) = split /\|/, $line, 10;
	if ($student) {
		$printstudent = "<b> - student</b>";
	}
	else {
		$printstudent = "";
	}
	if ($member) {
		$printmember = "<b> - member</b>";
	}
	else {
		$printmember = "";
	}
	if ($pp>0){
		$printpp = "X";
	}else{
		$printpp = "";
	}
	print "<tr><td class='red'>" . $printpp . "</td><td>" . $name . $printstudent . "</td><td>" . $from . "</td><td>" . $contact . "</td><td>" . $food . "</td><td>" . $cost .  "</td><td>" . $date . "</td></tr>" . "\n";
	$studentval = $studentval + $student;
	$memberval = $memberval + $member;
	$headCount++;
	$count{$food}++;         #match food requested to food hash and increment
}

print "</table> \n";

print "<br><br> \n";

print "<table width='50\%'> \n";
print "<tr><td valign=\"top\">";
print "Head Count = " . $headCount . "<br><br>";
print "Students included = " . $studentval . "<br>";
#print "Member discounts = " . $memberval;
print "</td><td valign=\"top\">\n";

foreach $food (keys %count)  {
	print "$food = $count{$food}<br>\n";
}
print "</td></tr> \n";
print "</table> \n";


print <<END_BOTTOM;
<h3>
<a href="../maint/rsvpResults.html">Back to Maintenance Page</a> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
<a href="../index.html">SDAG home</a>
</h3>
<br><br>
</body>
</html>
END_BOTTOM

sub dienice {
	my ($msg) = @_;
	print "<h2>Error</h2>\n";
	print "$msg";
	exit;
}
