# --------------------------------------------------------------------
#                    Order Form Input Fields                  
# -------------------------------------------------------------------

 %sc_order_form_array =('fname', 'First Name',
                        'lname', 'Last Name',
                        'email', 'Email',
                        'address', 'Address',
                        'city', 'City',
                        'state', 'State',
                        'zip', 'Postal ZIP Code');


  @sc_order_form_required_fields = ("fname",
                                    "lname",
                                    "address",
                                    "city",
                                    "state",        
                                    "zip");


# -------------------------------------------------------------------------------
#           process_order_form subroutine
# -------------------------------------------------------------------------------
sub process_order_form 
      {

             


local($required_fields_filled_in);
      

print qq!

<HTML>
<HEAD>
<TITLE>Processing The Order Form</TITLE>
</HEAD>
</BODY>!;

    &make_top_controls;
    &StoreHeader;
 
    # Assume that all filelds are filled
    $required_fields_filled_in = "yes";

    # some required field is not set
    foreach $required_field (@sc_order_form_required_fields ) 
    {
      if ($form_values{$required_field} eq "") 
      {
           $required_fields_filled_in = "";
      }
    }

    if ($required_fields_filled_in eq "")
    {
         
       print qq!

       <HTML>
       <HEAD>
          <TITLE> Ordering Form Error" </TITLE>
       </HEAD>
       <BODY>

       <CENTER>
       <TABLE BORDER="0" CELLSPACING="0" WIDTH="400" BGCOLOR="#fdfded">
       <FONT COLOR="black" FACE="GENEVA,ARIAL,HELVETICA,SANS-SERIF"   
       SIZE="2"><TR>
       <TR> <TD COLSPAN="3">&nbsp;</TD> </TR>
       <TR><TD ALIGN="center"> <H1><FONT COLOR="red"> Submission error  
       </FONT> </H1></TD></TR>
       <BR><BR>
 
       <TR><TD ALIGN="center"> 
          <A>
       <FONT COLOR="navy" SIZE="+1"> The following form information is missing:</FONT> 
           </A>
       </TD></TR>

       </FONT>
       </TR>
       </CENTER>
       </TABLE>
       <BR>!;
      
    
       foreach $required_field (@sc_order_form_required_fields ) {
       if ($form_values{$required_field} eq "") 
       {
       
         print qq~      
                 
         <CENTER>
            <A><FONT COLOR = "navy" size ="3"> $sc_order_form_array{$required_field} </FONT></A><BR>
         </CENTER>~;

       }
      }

     print qq!

      <CENTER>
      <TABLE BORDER="0" CELLSPACING="0" WIDTH="400" BGCOLOR="#fdfded">
      <TR><TD bgcolor="#fdfded">
        <FONT COLOR="navy" FACE="GENEVA,ARIAL,HELVETICA,SANS-SERIF" SIZE="2">
        <BR>
        <A>  We are unable to process your order. Please use the browsers Back button and fill the forms required fields or click the CHECKOUT button 
             to restart the checkout process. </a><BR>
       </TR></TD>
       </TABLE>
       <BR><BR>
       </CENTER>!;

   
      print qq~

      </BODY>
      </HTML>~;

  
       
  } # End of checking required fields
  else {  
         &display_cart_text;
         &shipping_cart_page;
       }

  
     print qq!

     </BODY>
     </HTML>!;
}
  
# ---------------------------------------------------------------------
#   display_cart_text
# ---------------------------------------------------------------------
sub display_cart_text
     {              
               #
               # displays, in text format, the contents of the cart. 
               #

local($subtotal, 
      $total_quantity,
      $total_measured_quantity,
      $text_of_cart);
      
               

 ($subtotal, 
   $total_quantity,
   $total_measured_quantity,
   $text_of_cart) = 
   &display_order_cart_table;      
 
            
   $text_of_cart =
     &display_calculations($subtotal,"at",
       $total_measured_quantity,$text_of_cart);

              


    foreach $form_field (sort(keys(%sc_order_form_array))) {
      $text_of_cart .= 
        &format_text_field($sc_order_form_array{$form_field})
        . "= $form_values{$form_field}\n";
     }
     $text_of_cart .= "\n";

               

     if ($sc_use_pgp =~ /yes/i) {
       &require_supporting_libraries(__FILE__, __LINE__,
        "$sc_pgp_lib_path");
     $text_of_cart = &make_pgp_file($text_of_cart,
                  "$sc_pgp_temp_file_path/$$.pgp");
     $text_of_cart = "\n" . $text_of_cart . "\n";
     }
               
 
     if ($sc_send_order_to_log =~ /yes/i) {

       open (ORDERLOG, "$sc_order_log_file");
       print ORDERLOG "-" x 40;
       print ORDERLOG $text_of_cart;
       print ORDERLOG "-" x 40 . "\n";
       close (ORDERLOG);
     
               
    }
    else 
    {

         
     } 
} # end of display cart text





sub display_calculations {
  local($subtotal,
        $are_we_before_or_at_process_form,
        $total_measured_quantity,
        $text_of_cart) = @_;
               
  local  ($final_shipping,
          $final_discount,
          $final_sales_tax,$grand_total) =
    &calculate_final_values($subtotal,
                       $total_quantity,
                       $total_measured_quantity,
                       $are_we_before_or_at_process_form);
                
  if ($final_shipping > 0) {
    $final_shipping = &display_price($final_shipping);
    print "Shipping: $final_shipping<P>";
    $text_of_cart .= &format_text_field("Shipping:") . 
      "= $final_shipping\n\n";
  };

  if ($final_discount > 0) {
    $final_discount = &display_price($final_discount);
    print "Discount: $final_discount<P>";
    $text_of_cart .= &format_text_field("Discount:") . 
      "= $final_discount\n\n";
  }
               
  if ($final_sales_tax > 0) {
    $final_sales_tax = &display_price($final_sales_tax);
    print "Sales Tax: $final_sales_tax<P>";
    $text_of_cart .= &format_text_field("Sales Tax:") . 
      "= $final_sales_tax\n\n";
  }

               
  $grand_total = &display_price($grand_total);
  print "Grand Total: $grand_total<P>";
  $text_of_cart .= &format_text_field("Grand Total:") . 
   "= $grand_total\n\n";

               
  return ($text_of_cart);
}

sub calculate_final_values {
  local($subtotal,
        $total_quantity,
        $total_measured_quantity,
        $are_we_before_or_at_process_form) = @_;
  local($temp_total) = 0;
  local($grand_total) = 0;
  local($final_shipping, $shipping);
  local($final_discount, $discount);
  local($final_sales_tax, $sales_tax);
  local($calc_loop) = 0;

               
  $temp_total = $subtotal;
               
  for (1..3) {
               
    $shipping = 0;
    $discount = 0;
    $sales_tax = 0;
    $calc_loop = $_;
                
    if ($are_we_before_or_at_process_form =~
         /before/i) {
               
      if ($sc_calculate_discount_at_display_form ==
          $calc_loop) {
        $discount = 
          &calculate_discount($temp_total,
            $total_quantity,
            $total_measured_quantity);
      } # End of if discount gets calculated here
      if ($sc_calculate_shipping_at_display_form ==
          $calc_loop) {
        $shipping = 
          &calculate_shipping($temp_total,
            $total_quantity,
            $total_measured_quantity);
      } # End of shipping calculations
      if ($sc_calculate_sales_tax_at_display_form ==
          $calc_loop) {
        $sales_tax = 
          &calculate_sales_tax($temp_total);
      } # End of sales tax calculations
               
    } else {
      if ($sc_calculate_discount_at_process_form ==
          $calc_loop) {
        $discount = 
          &calculate_discount($temp_total,
            $total_quantity,
            $total_measured_quantity);
      } # End of if discount gets calculated here
      if ($sc_calculate_shipping_at_process_form ==
          $calc_loop) {
        $shipping = 
          &calculate_shipping($temp_total,
            $total_quantity,
            $total_measured_quantity);
      } # End of shipping calculations
      if ($sc_calculate_sales_tax_at_process_form ==
          $calc_loop) {
        $sales_tax = 
          &calculate_sales_tax($temp_total);
      } # End of sales tax calculations
    } # End of if we are before or at process order form
               
    $final_discount = $discount if ($discount > 0);
    $final_shipping = $shipping if ($shipping > 0);
    $final_sales_tax = $sales_tax if ($sales_tax > 0);
    $temp_total = $temp_total - $discount
                    + $shipping + $sales_tax;
  } # End of $calc_loop
                
  $grand_total = $temp_total;
               

  return ($final_shipping,
          $final_discount,
          $final_sales_tax,
          &format_price($grand_total));

} # calculate_totals


sub calculate_shipping {
  local($subtotal,
        $total_quantity,
        $total_measured_quantity) = @_;
                # 
                # This routine calls the calculate
                # general logic subroutine
                # by passing it a reference to the
                # shipping logic and order form
                # shipping related fields variable
                # 
  return(&calculate_general_logic(
           $subtotal,
           $total_quantity,
           $total_measured_quantity,
           *sc_shipping_logic,
           *sc_order_form_shipping_related_fields));

} # End of calculate_shipping

sub calculate_discount {
  local($subtotal,
        $total_quantity,
        $total_measured_quantity) = @_;
                # 
                # This routine calls the calculate
                # general logic subroutine
                # by passing it a reference to the
                # discount logic and order form
                # discount related fields variable
                # 
  return(&calculate_general_logic(
           $subtotal,
           $total_quantity,
           $total_measured_quantity,
           *sc_discount_logic,
           *sc_order_form_discount_related_fields));

} # End of calculate_discount

sub calculate_general_logic {
  local($subtotal,
        $total_quantity,
        $total_measured_quantity,
	*general_logic,
        *general_related_form_fields) = @_;

  local($general_value);

  local($x, $count);
  local($logic);
  local($criteria_satisfied);
  local(@fields);

                # The @related_form_values
                # array contains the values of the
                # form fields specified in the
                # @general_related_form_fields
                # array.
  local(@related_form_values) = ();
  
                # The form values are assigned
  $count = 0;
  foreach $x (@general_related_form_fields) {
    $related_form_values [$count] = $form_values{$x};
    $count++;
  }

               
  foreach $logic (@general_logic) {
               
    $criteria_satisfied = "yes";
              
    @fields = split(/\|/, $logic);  
		
    for (1..@related_form_values) {
      if (!(&compare_logic_values(
            $related_form_values[$_ - 1],
            $fields[$_ - 1]))) {
         $criteria_satisfied = "no";
      }
    } # End of loop through form values

		
    for (1..@related_form_values) {
      shift(@fields);
    }compare against
		#
    if (!(&compare_logic_values(
          $subtotal,
          $fields[0]))) {
       $criteria_satisfied = "no";
    }
                # Shift off the subtotal
    shift (@fields);
               
    if (!(&compare_logic_values(
          $total_quantity,
          $fields[0]))) {
       $criteria_satisfied = "no";
    }
                # Shift off the quantity
    shift (@fields);
               
    if (!(&compare_logic_values(
          $total_measured_quantity,
          $fields[0]))) {
       $criteria_satisfied = "no";
    }
                # Shift off the fields
    shift (@fields);
		
    if ($criteria_satisfied eq "yes") {

if ($fields[0] =~ /%/) {
        $fields[0] =~ s/%//;
        $general_value = $subtotal * $fields[0] / 100;
      } else {
        $general_value = $fields[0];
      }
    }

    
  } # End of foreach loop through shipping logic
               
  return(&format_price($general_value));

} # End of calculate_general_logic


sub calculate_sales_tax {
  local($subtotal) = @_;
  local($sales_tax) = 0;

               
  if ($sc_sales_tax_form_variable ne "") {
    foreach $value (@sc_sales_tax_form_values) {
      if (($value =~ 
          /^$form_values{$sc_sales_tax_form_variable}$/i) &&
         ($form_values{$sc_sales_tax_form_variable} ne ""))  {
        $sales_tax = $subtotal * $sc_sales_tax;
      }
    }
                
  } else {
    $sales_tax = $subtotal * $sc_sales_tax;
  }
                
  return (&format_price($sales_tax));

} # End of calculate sales tax


sub compare_logic_values {
  local($input_value, $value_to_compare) = @_;
  local($lowrange, $highrange);

               
  if ($value_to_compare =~ /-/) {

               
    ($lowrange, $highrange) = split(/-/, $value_to_compare);
                
    if ($lowrange eq "") {
      if ($input_value <= $highrange) {
        return(1);
      } else {
        return(0);
      }
               
    } elsif ($highrange eq "") {
      if ($input_value >= $lowrange) {
        return(1);
      } else {
        return(0);
      }
               
    } else {
      if (($input_value >= $lowrange) &&
         ($input_value <= $highrange)) {
        return(1);
      } else {
        return(0);
      }
    }
  } else {
    if (($input_value =~ /$value_to_compare/i) ||
        ($value_to_compare eq "")) {
      return(1);
    } else {
      return(0);
    }
  }
} # End of compare_logic_values

1; # Libraries always end this way so that they return true to require





