#!/usr/bin/perl

use DBI;

$reqmethod = $ENV{'REQUEST_METHOD'};
$RemoteAddr = $ENV{'REMOTE_ADDR'};
$DeviceType = $ENV{'HTTP_USER_AGENT'};

if ( ${reqmethod} eq 'POST' ) {
   read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
   @pairs = split(/&/, $buffer);
}
else {
   @pairs = split(/&/, $ENV{'QUERY_STRING'});
}
foreach $pair (@pairs) {
   local($name, $value) = split(/=/, $pair);
   $name =~ tr/+/ /;
   $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
   $value =~ tr/+/ /;
   $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
   $value =~ s/<!--(.|\n)*-->//g;
   chomp(${value});
   $Form{$name} = "$value";
}

$Domain = "dma.magster.org";
$ThisScriptURL = "http://$Domain/dma_absence_form.pl";
$HomeDIR = "/home4/magstero/public_html/dma";
$TEACHERtable = "dma_teachers";
$Title = "DMA Student Absence Form";

my $dbh = DBI->connect("DBI:mysql:magstero_dma",'magstero_dma','Dm@202O!');

@Months = (
          'January',
          'February',
          'March',
          'April',
          'May',
          'June',
          'July',
          'August',
          'September',
          'October',
          'November',
          'December',
         );
$CurrMonth = `TZ='America/New_York' date '+%B'`; chomp($CurrMonth);
$CurrDay = `TZ='America/New_York' date '+%e'`; chomp($CurrDay);
$CurrYear = `TZ='America/New_York' date '+%Y'`; chomp($CurrYear);
$NextYear = $CurrYear+1;

my $sth = $dbh->prepare("SELECT `title`,`firstname`,`lastname`,`grade`,`email` FROM $TEACHERtable ORDER BY `listorder`");
$sth->execute();
while (my $ref = $sth->fetchrow_hashref()) {
 $TTitle = $ref->{'title'};
 $TFirstname = $ref->{'firstname'};
 $TLastname = $ref->{'lastname'};
  $TeacherFullName = "$TTitle $TFirstname $TLastname";
  push(@Teachers,"$TeacherFullName");
 $TGrade = $ref->{'grade'};
  $TeacherGrade{$TeacherFullName} = $TGrade;
 $TEmail = $ref->{'email'};
  $TeacherEmail{$TeacherFullName} = $TEmail;
}
$sth->finish();

$Option = $Form{'option'};
(($Option eq '') && (&Main())) || &$Option();
exit;

sub Main {
print <<endofprint;
Content-type: text/html

<html>
<head>
<title>$Title</title>
<link href="http://magster.org/dma/dma_absence_form.css" rel="stylesheet" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div class="pagetitle"></div>
<div class="pagebody">
<div class="formname">DMA Student Absence Form</div>
<div class="a">Please enter ALL of the following information</div>
<br><br>
<form method=post action="$ThisScriptURL">
<table align=center>
<tr>
<td>Student Name (First & Last):</td>
<td width=10></td>
<td class="formanswers"><input type=text name="studentname" size=30>
</tr>
<tr>
<td>Parent/Guardian Name (First & Last):</td>
<td width=10></td>
<td class="formanswers"><input type=text name="parentname" size=30>
</tr>
<tr>
<td>Parent/Guardian E-Mail Address:</td>
<td width=10></td>
<td class="formanswers"><input type=text name="parentemail" size=30>
</tr>
<tr>
<td>First Date of Absence:</td>
<td width=10></td>
<td class="formanswers">
endofprint
# FIRST DATE OF ABSENCE
 print "<select name=\"firstmonth\">\n";
 foreach $z (@Months) {
  if ($z eq $CurrMonth) {
   print "<option selected>$z</option>\n";
  }
  else {
   print "<option>$z</option>\n";
  }
 }
 print "</select>\n";
 print "<select name=\"firstday\">\n";
 for ($i=1;$i<=31;$i++) {
  if ($i == $CurrDay) {
   print "<option selected>$i</option>\n";
  }
  else {
   print "<option>$i</option>\n";
  }
 }
 print "</select>\n";
 print "<select name=\"firstyear\">\n";
 print "<option selected>$CurrYear</option>\n";
 print "<option>$NextYear</option>\n";
 print "</select>\n";
print <<endofprint;
</td>
</tr>
<tr><td class="formanswers2" colspan=3>Must be consecutive days; Same as First Date of Absence if single day</td></tr>
<tr>
<td>Last Date of Absence:</td>
<td width=10></td>
<td class="formanswers">
endofprint
# LAST DATE OF ABSENCE
 print "<select name=\"lastmonth\">\n";
 foreach $z (@Months) {
  if ($z eq $CurrMonth) {
   print "<option selected>$z</option>\n";
  }
  else {
   print "<option>$z</option>\n";
  }
 }
 print "</select>\n";
 print "<select name=\"lastday\">\n";
 for ($i=1;$i<=31;$i++) {
  if ($i == $CurrDay) {
   print "<option selected>$i</option>\n";
  }
  else {
   print "<option>$i</option>\n";
  }
 }
 print "</select>\n";
 print "<select name=\"lastyear\">\n";
 print "<option selected>$CurrYear</option>\n";
 print "<option>$NextYear</option>\n";
 print "</select>\n";
print <<endofprint;
</td>
</tr>
<tr>
<td>Reason for Absence:</td>
<td width=10></td>
<td class="formanswers"><input type=text name="reason" size=40>
</tr>
<tr>
<td>Request homework to be sent home?:</td>
<td width=10></td>
<td class="formanswers3">
<input class="bigradio" type="radio" name="homework" value="yes"> Yes
&nbsp;&nbsp;&nbsp;&nbsp;
<input class="bigradio" type="radio" name="homework" value="no"> No
</td>
</tr>
<tr><td class="formanswers2" colspan=3>Homework can only be requested for absences of 2 or more consecutive days</td></tr>
<td>Homeroom Teacher:</td>
<td width=10></td>
<td class="formanswers">
endofprint
 print "<select name=\"teacher\">\n";
 print "<option selected>PLEASE SELECT</option>\n";
 foreach $z (@Teachers) {
  print "<option>$z ($TeacherGrade{$z})</option>\n";
 }
 print "</select>\n";
print <<endofprint;
</td>
</tr>
</table>
<br>
<center><input class="bigsubmit" type=submit value="Submit Absence Form"></center>
</form>
</div>
</body>
</html>
endofprint
}

sub Main2 {
 $CustName = $Form{'f_custname'};
 $CarColor = $Form{'f_carcolor'};
 $CarType = $Form{'f_cartype'};
 foreach $z (@FoodOrder) {
  $zlc = lc($z);
  $numName = "f_num_" . "$zlc";
  $priceName = "f_price_" . "$zlc";
  $OrderedNum{$z} = $Form{$numName};
  $OrderedPrice{$z} = $Form{$priceName};
 }
 $TotalItems = $Form{'f_total_items'};
 $TotalPrice = $Form{'f_total_price'};
 $OrderInDay = `TZ='America/New_York' date '+%Y-%m-%d'`; chomp($OrderInDay);
 $OrderInTime = `TZ='America/New_York' date '+%H:%M:%S'`; chomp($OrderInTime);

 my $random_number1 = int(rand(1000)); my $random_number2 = int(rand(1000));
 my $RecordKey = "${random_number1}_${random_number2}";

 my $dbh = DBI->connect("DBI:mysql:magstero_sbfestival:localhost",
                        "magstero_sbfest","SBmda2009");

 my $insertstatement = "INSERT INTO $BBtable (custname,carcolor,cartype,total_items,total_price,order_in_day,order_in_time,recordkey) VALUES (?,?,?,?,?,?,?,?)";
 my $sth = $dbh->prepare($insertstatement);
 $sth->execute($CustName,$CarColor,$CarType,$TotalItems,$TotalPrice,$OrderInDay,$OrderInTime,$RecordKey);
 foreach $z (@FoodOrder) {
  $updatestatement = "UPDATE $BBtable SET `num_$z`=?,`price_$z`=? WHERE `recordkey`='$RecordKey'";
  $sth = $dbh->prepare($updatestatement);
  $sth->execute($OrderedNum{$z},$OrderedPrice{$z});
 }

 my $sth = $dbh->prepare("SELECT `rownum`,`order_in_day`,`order_in_time` FROM $BBtable WHERE `recordkey`='$RecordKey'");
 $sth->execute();
 while (my $ref = $sth->fetchrow_hashref()) {
  $Rownum = $ref->{'rownum'};
  $OrderInDay = $ref->{'order_in_day'};
  $OrderInTime = $ref->{'order_in_time'};
 }
 $sth->finish();

 $dbh->disconnect();

##########################################################
# CREATE PRINTABLE RECEIPT
##########################################################
 $ReceiptName = "$ReceiptDir/bborder_${Rownum}.html";
 $OrderSubmitted = "$OrderInDay $OrderInTime";
 open(RN,">$ReceiptName");
print RN <<endofprint;
<html>
<head>
<title>$Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.receipt_pagebody {
  width: 100%;
  height: 99%;
  margin: auto;
  position: absolute;
  left: 0%;
  top: 0%;
  padding-top: .25%;
  font-weight: bold;
  font-size: .8em;
  text-align: center;
  background-color: #FFFFFF;
  color: #000000;
}
.pagebody_receipt td {
  font-weight: bold;
  font-size: .8em;
  color: #000000;
}
</style>
</head>
<body OnLoad="javascript:window.print()">
<div class="receipt_pagebody">
<table class="pagebody_receipt" align=center>
 <tr>
  <td align=right>Order Submitted:</td>
  <td width=15></td>
  <td align=left>$OrderSubmitted</td>
 </tr>
 <tr>
  <td align=right>Order #:</td>
  <td width=15></td>
  <td align=left>$Rownum</td>
 </tr>
 <tr>
  <td align=right>Customer Name:</td>
  <td width=15></td>
  <td align=left>$CustName</td>
 </tr>
 <tr>
  <td align=right>Vehicle:</td>
  <td></td>
  <td align=left>$CarColor $CarType</td>
 </tr>
</table>
<br>
<table class="pagebody_receipt" align=center>
endofprint
foreach $z (@FoodOrder) {
 if ($OrderedNum{$z} > 0) {
  (($OrderedNum{$z}>1) && ($FoodItemName="$PluralFoodName{$z}")) || ($FoodItemName="$SingularFoodName{$z}");
print RN <<endofprint;
 <tr>
  <td align=right>$OrderedNum{$z}</td>
  <td width=10></td>
  <td align=left>$FoodItemName</td>
  <td width=10></td>
  <td align=right>\$</td>
  <td align=right>$OrderedPrice{$z}</td>
 </tr>
endofprint
 }
}
print RN <<endofprint;
 <tr><td colspan=6 align=center><hr width=100%></td></tr>
 <tr>
  <td align=right>$TotalItems</td>
  <td width=10></td>
  <td align=left>TOTAL ITEMS</td>
  <td width=10></td>
  <td align=right>\$</td>
  <td align=right>$TotalPrice</td>
 </tr>
</table>
<br><br>
<b><i>Thank You For Supporting<br>Christ The Divine Shepherd Parish<br>$Year Burger Bash</i></b>
</div>
</body>
</html>
endofprint
 close(RN);
##########################################################

##########################################################
# PRINT SUMMARY PAGE
##########################################################
 $OnlineReceiptName = "$ReceiptDir/bborder_${Rownum}-online.html";
 $OnlineReceiptURL = "$ReceiptURL/bborder_${Rownum}-online.html";
 open(ORN,">$OnlineReceiptName");

print ORN <<endofprint;
Content-type: text/html

<html>
<head>
<title>$Title</title>
<link href="http://cdsfishfry.org/burgerbash/bbof.css" rel="stylesheet" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div class="pagetitle">
Christ the Divine Shepherd<br>
$Title
</div>
<div class="ordersubmit-left">
<b>TOTAL AMOUNT DUE: \$$TotalPrice</b>
</div>
<div class="ordersubmit-right">
<b>ORDER SUBMITTED</b>
</div>
<div class="summary_pagebody">
<table class="pagebody_summary" align=center>
 <tr>
  <td align=right>Order #:</td>
  <td width=15></td>
  <td align=left>$Rownum</td>
 </tr>
 <tr>
  <td align=right>Customer Name:</td>
  <td width=15></td>
  <td align=left>$CustName</td>
 </tr>
 <tr>
  <td align=right>Vehicle:</td>
  <td></td>
  <td align=left>$CarColor $CarType</td>
 </tr>
</table>
<br>
<table class="pagebody_summary" align=center>
endofprint
foreach $z (@FoodOrder) {
 if ($OrderedNum{$z} > 0) {
  (($OrderedNum{$z}>1) && ($FoodItemName="$PluralFoodName{$z}")) || ($FoodItemName="$SingularFoodName{$z}");
print ORN <<endofprint;
 <tr>
  <td align=right>$OrderedNum{$z}</td>
  <td width=10></td>
  <td align=left>$FoodItemName</td>
  <td width=10></td>
  <td align=right>\$</td>
  <td align=right>$OrderedPrice{$z}</td>
 </tr>
endofprint
 }
}
print ORN <<endofprint;
 <tr><td colspan=6 align=center><hr width=100%></td></tr>
 <tr>
  <td align=right>$TotalItems</td>
  <td width=10></td>
  <td align=left>TOTAL ITEMS</td>
  <td width=10></td>
  <td align=right>\$</td>
  <td align=right>$TotalPrice</td>
 </tr>
</table>
<br><br>
<a href="$ThisScriptURL">START NEW ORDER</a>
</div>
</body>
</html>
endofprint
 close(ORN);

 print "Location: $OnlineReceiptURL\n\n";
}

$dbh->disconnect();
