#!/usr/bin/perl -w # # File: Project3A.pl # # A program that collects and stores a donut order # and returns a confirmation page to the client. # # D. Hildebrandt # A340 - Fall, 2002 # Use the GGI.pm Perl module to simplify this program use CGI qw(:standard); # Extract the form data $name = param("name"); $email = param("email"); $phone = param("phone"); $delivery = param("delivery"); $plain = param("qtyP"); $choco = param("qtyCh"); $vanilla = param("qtyV"); $coconut = param("qtyCo"); $maple = param("qtyM"); # Check for missing entries if( ($name eq "") | ($email eq "") | ($phone eq "") | ($delivery eq "") ) { # Send missing form data page print header(); print start_html("Missing Form Data"); print "

Click the Back button and complete the form!

"; print end_html(); exit; } #----------------------------------------------- # Validate the phone number $result = ($phone =~ /\d{3}-\d{3}-\d{4}/ ); if( ! $result ) { # Send back an error page print header(); print start_html("Error"); print "

"; print "Error - Invalid Phone Number
"; print "You entered $phone
"; print "Enter the phone number as ddd-ddd-dddd
"; print "Click the Back button and try again.
"; print "

"; print end_html(); exit; } # Accumulate the total number of donuts ordered $totalDonuts = $plain + $choco + $vanilla + $coconut + $maple; # Determine the packaging, convert to whole numbers $dozens = int($totalDonuts / 12); $remainder = int($totalDonuts % 12); $halfDozens = int($remainder / 6); $singles = int($remainder % 6); # Determine the total price $deliveryFee = 1; $price = 0.49 * $singles + 2.7 * $halfDozens + 4.8 * $dozens; $totalPrice = $price + $deliveryFee; # Open a file for writing/appending $theLocation = "localhost"; # So, specify the complete path to the data file $result = open(OUTDAT,">>e:\\WebWeaver\\scripts\\data\\Project3A.txt") or error(); # Get the current date and time ($second,$minute,$hour,$dayOfMonth,$month,$year,$weekDay,$dayOfYear,$isDst) = localtime(time); # Remember to add 1 to $month later # Assemble one line of data $oneLine = join( "|", ($name,$email,$phone,$delivery,$plain,$choco,$vanilla,$coconut,$maple,$totalDonuts,$singles, $halfDozens,$dozens,$price,$deliveryFee,$totalPrice, $second,$minute,$hour,$dayOfMonth,$month,$year,$weekDay,$dayOfYear,$isDst) ); # Append that line to the output file print OUTDAT $oneLine . "\n"; # Close the output file close(OUTDAT); # Return a confimation page print "Content-type: text/html\n\n"; print ""; print ""; print "Digital Donuts Order Confirmation"; print ""; print ""; print ""; print "  "; print "

"; # Display the order data print ""; print ""; print "
"; $realMonth = $month + 1; $realYear = $year + 1900; print ""; print ""; print ""; print ""; print ""; @delivery = ( "Today", "Next Day" ); print ""; print ""; print ""; print ""; print ""; print "
"; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print "
Digital Donuts® Order Confirmation
Date$realMonth/$dayOfMonth/$realYear
Time$hour:$minute:$second
Name$name
Email$email
Phone No.$phone
Delivery Day$delivery[$delivery]
Plain$plain
Chocolate Frosted$choco
Vanilla Frosted$vanilla
Coconut Topped$coconut
Maple Frosted$maple
Total Donuts$totalDonuts
Singles$singles
Half Dozens$halfDozens
Dozens$dozens
Price" . toCurrency($price) . "
Delivery Fee" . toCurrency($deliveryFee) . "
Total Due" . toCurrency($totalPrice) . "
"; print "

Thanks for Ordering Digital Donuts®

"; print end_html(); # Terminate this program exit(0); sub error() { print header(); print start_html("Open File Error"); print "Unable to open the file data/Project3A.txt"; print end_html(); exit(0); } sub toCurrency() { ##Converts numbers to currency my $x = $_[0]; ## convert to cents (shift decimal point 2 places to the right) $x = $x * 100; ## truncate any fractional cents $x = int($x / 1); ## get dollars my $dollars = int($x / 100); ## and cents my $cents = $x - ($dollars * 100); ## put the pieces together my $result = "\$" . $dollars; if( $cents < 10 ){ $cents = "0" . $cents;} $result = $result . "." . $cents; return $result; }