#!/usr/bin/perl -w
######################################################################
# shop.pl
######################################################################
# Perl Power! - Michael Schilli 1998
######################################################################

use CGI qw/:standard/;                           # CGI functions
use Fcntl qw/:flock/;                            # LOCK_EX

print header();                                  # output header

%products = (1 => ['Perl for Women, O\'Reilly', 39.95], # products
             2 => ['Perl in 3 Days, SamsNet', 11.50], 
             3 => ['Go To Perl5, AWL', 38.90]);

%labels = map { ($_, $products{$_}->[0]) } keys %products;

eval {                                           # intercept errors

  if(!defined param('customer_number')) {        # no customer number?
      print start_html('-title', 'Welcome'),     # -> start page
            h1('Welcome to the Perl bookstore!'),
            start_form(), "Your customer number:", 
            textfield(-name => 'customer_number'),
            submit(-value => "Go shopping!"),
            end_form(), end_html();

  } elsif(!defined param('order')) {             # no order?
      print start_html('-title', 'Order'),       # -> ordering page
            h1("Our assortment:"), start_form(),
            checkbox_group(
                '-name'      => 'order',
                '-values'    =>  [keys %labels],
                '-linebreak' => 'true',          # underneath each other
                '-labels'    => \%labels),       # products
            p(), submit(-value => 'Order'),      # order button
            hidden(-name  => 'customer_number'), # forward
            end_form(), end_html();

  } else {                                       # store order
      @order = param('order');

      open(ORDER, ">>orders.txt") || die "Cannot open orders.txt";
      flock(ORDER, LOCK_EX);                     # set lock
      print ORDER "Customer number: ", param('customer_number'),
                  " Order: @order\n";
      close(ORDER);
  
      print start_html('-title', 'Thank you!'),  # thank you page
            h1("Your order: "); 
      
      $sum = 0;

      foreach $order (@order) {
            $sum += $products{$order}->[1];
            print pre(sprintf "%-40s US\$ %6.2f", 
                              $products{$order}->[0],
                              $products{$order}->[1]);
      }
      print pre("-" x 60);
      print pre(sprintf "%-40s US\$ %6.2f", "Total", $sum);

      print "The books will be sent to you in the next few days. " .
            "The amount to be paid is charged to customer number ", 
            param('customer_number'), 
            ". Thank you for your order!",
            start_form(), submit(-value => "Back to entry"),
            end_form(), end_html();
  }
};

if ($@) {                                        # error?
    print "Our system can currently not accept your order. " .
          "Please try again later.\n";
    open(ERRORLOG, ">>/tmp/errorlog");           # log error in file
    print ERRORLOG scalar localtime, "> $@";     # for analysis
    close(ERRORLOG);
}

