#!c:\perl\bin\perl
#be sure this above line points to your perl interpreter

##
## A script that, given a SDNumber, provides a view of the corresponding
## order in the SnarkDreams database.
## Author: Zach Tomaszewski
## Date: 30 April 2003
##

use Win32::ODBC;
use CGI ":cgi";
$cgi = new CGI;
$sdno = $cgi->param('sdno');  #the user-requested order number

############################################# start CUT
$DSN="SnarkDreams";
$username="";
$password="";

#Provide the three needed sql statements below.
$SQLforCustomer ="";
$SQLforOrder_Prod = "";
$SQLforOrder = "";
############################################# end CUT

##main program

printHeader();
if ($cgi->param('sdno')) { #then a specific order number has been requested
  printOrderView();
  printSystemInfo();
}
printFooter();

###END main program



##
# Prints the beginning of the html page
##
sub printHeader(){
 print "Content-Type: text/html\n\n";  #html header
 print qq^
   <html><head><title>SnarkDreams Order View</title>
    <style type="text/css"><!--
      P, HR, ADDRESS {margin-top: 0; margin-bottom: 0; 
             margin-left: 12%; margin-right: 12%;}
      h3 {margin-left: 5%;}
    --></style></head>
    <body>
    <h3>SnarkDreams Order View</h3>
    <form action="orderview.cgi" method="get">
    <p>
    Enter SD Order Number:
    <input name="sdno" type="text" size="4" maxlength="4">
    </form>
    </p>
 ^;
}


##
# Prints the order view information
# after querying the database for the needed info
##

sub printOrderView(){
  %customerData = ();
  %orderData = ();
  %itemData = ();
  
  #open connect to DB and catch errors
  if (!($db = new Win32::ODBC("DSN=$DSN;UID=$username;PWD=$password;"))){
      print "<p><b>Error connecting to database.<br>\n";
      print "Error: " . Win32::ODBC::Error() . "</b></p>\n";
      return;
  }

  checkOrderExistance($sdno);  #for a friendly message when the order doesn't exist

  loadSQLResults($SQLforCustomer);  #$db now contains the table resulting 
                                    #from the customer sql query 
  $db->FetchRow();                 #Point cursor to the first (and only) row
  %customerData = $db->DataHash();  #And then store that row in the %customerData hash
                                    #where the key is the column name 
                                    #and the value is value in that column
 
  #repeat the process for %orderData
  loadSQLResults($SQLforOrder);
  $db->FetchRow();
  %orderData = $db->DataHash();

  #prep $db for %itemData
  loadSQLResults($SQLforOrder_Prod);
 
  #start printing 
  print qq^
    <hr><p>
    SD No: <b>$sdno</b><br>
    Date: $orderData{ORDERDATE}<br>
    </p>
    <p align="right">Kagi: $orderData{KAGI_NO}<br></p>\n
    <p>
    $customerData{FIRSTNAME} $customerData{LASTNAME}<br>
    $customerData{ADDRESS}<br>
    $customerData{COUNTRY} -- $customerData{STATE}<br>
    Tel: $customerData{TELEPHONE}<br>
    Email: $customerData{EMAIL}<br>
    </p>
    <p><br>
  ^;
    
  #cycle through all ordered items
  while ($db->FetchRow()){
    %itemData = $db->DataHash();
    print "$itemData{QUANTITY} x $itemData{DESCRIPTION}<br>\n";
    undef %itemData;
  }
  $costtotal = $orderData{COST} + $orderData{KAGI_PERCENT};
  
  #continue printing
  print qq^
    </p>
    <p><br>
    Comments: $orderData{COMMENTS}<br>
    <p>
    <br>
    Total Price: $orderData{TOTAL_PRICE}
    <p align="right">
    Kagi Percent: $orderData{KAGI_PERCENT}<br>
    Cost: $orderData{COST}<br>
    Cost Total: $costtotal
    <p align="center">
    Profit: $orderData{PROFIT}<b></b>
    </p><hr>
  ^;
}


##
# Prints the end of the html page
##
sub printFooter(){
  print "</body></html>\n\n";
}


##
# Prints a little bit of system info
##
sub printSystemInfo(){
  print "<p><br><small>";
  my @keys = ();
  foreach $key (HTTP_HOST, REMOTE_ADDR, HTTP_USER_AGENT, SERVER_SIGNATURE){
    print "$key:  $ENV{$key} <br>\n";
  }
  print "</small></p>";
}


##
# Given an SQL statement, runs it on the global $db ODBC
# $db then contains the results
# Exits if there is an error, printing the error on the way out.
##
sub loadSQLResults {  #($SQL)
  ($SQL) = @_; 
  if ($db->Sql($SQL)){
      print "<p><b>SQL failed.<br>\n Error: " . $db->Error() . "</b><br>\n";
      print "<br><b>Statement:</b><br> $SQL</p>\n";
      $db->Close();
      printFooter();
      exit();
  }
}


##
# Given an sdno, checks to see if that order exists in the database.
# Upsets the current contents of $db to do this.
# If order doesn't exist, warns and exits.
##
sub checkOrderExistance{ #($sdno)
  ($sdno) = @_;
  loadSQLResults("select sdnumber from sdorder where sdnumber = $sdno");
  $db->FetchRow();
  %data = $db->DataHash();
  if ($data{SDNUMBER} eq "") { 
    print "<p><b>There is no order with SDNumber \"$sdno\" in database.</b></p>";
    printFooter();
    exit();
  }
}
