--- uc_discounts.admin.inc.bak	Tue May 26 01:45:13 2009
+++ uc_discounts.admin.inc	Fri Jul 24 18:11:38 2009
@@ -103,24 +103,89 @@
 		"#size" => 75,
 	);
 
-	$form["qualifying_type"] = array(
+	$form["can_be_combined_with_other_discounts"] = array(
+		"#type" => "checkbox",
+		"#title" => t("Can be combined with other discounts"),
+		"#description" => t("Whether or not this discount will be applied if other discounts are."),
+		"#default_value" => isset($form_state["values"]["can_be_combined_with_other_discounts"]) 
+			? $form_state["values"]["can_be_combined_with_other_discounts"] : CAN_BE_COMBINED_WITH_OTHER_DISCOUNTS_DEFAULT,
+	);
+
+	//Add custom weight range [-50, 50]
+	$options = array();
+	for ($i = -50; $i <= 50; $i++)
+		$options[$i] = $i;
+	$form["weight"] = array(
 		"#type" => "select",
-		"#title" => t("Qualifying type"),
-		"#description" => t("The amount used to determine if a cart qualifies for this discount."),
+		"#title" => t("Weight"),
+		"#description" => t("Lighter discounts are applied to an order first. This value is unimportant if there are no discounts on discount line items."),
+		"#options" => $options,
+		"#default_value" => isset($form_state["values"]["weight"]) ? $form_state["values"]["weight"] : 0,
+	);
+
+	$has_expiration = isset($form_state["values"]["has_expiration"]) ? $form_state["values"]["has_expiration"] : HAS_EXPIRATION_DEFAULT;
+	$display_string = ($has_expiration) ? "" : " style='display:none'";
+
+	$expiration_container_id = "expiration-container-" . $seed;
+	$expiration_prefix = sprintf("<div id='%s' onclick='if ( jQuery(\"#%s input[type=\\\"checkbox\\\"]\").attr(\"checked\") )"
+		. "{ jQuery(\"#%s .expiration-container\").slideDown(); }"
+		. "else { jQuery(\"#%s .expiration-container\").slideUp(); }'>", 
+		$expiration_container_id, $expiration_container_id, $expiration_container_id, $expiration_container_id, $expiration_container_id, $expiration_container_id);
+
+	$form["expiration_header"] = array(
+		"#type" => "hidden",
+		"#prefix" => $expiration_prefix,
+	);
+
+	$form["has_expiration"] = array(
+		"#type" => "checkbox",
+		"#title" => t("Discount expires"),
+		"#description" => t("Whether or not this discount rule will expire on a given date."),
+		"#default_value" => $has_expiration,
+	);
+
+	$expiration = isset($form_state["values"]["expiration"]) 
+		? $form_state["values"]["expiration"] : time();
+	$form["expiration"] = array(
+		"#type" => "date_popup",
+		"#date_type" => "DATE_UNIX",
+		"#title" => t("Discount expiration")
+			. sprintf("<span title='" . t("This field is required.") . "' class='form-required'>*</span>"),
+		"#description" => t("Date and time when the discount expires."),
+		"#default_value" => date("Y-m-d H:i:s", $expiration),
+		"#prefix" => sprintf("<div class='expiration-container'%s>", $display_string),
+		"#suffix" => "</div>",
+	);
+
+	$form["expiration_footer"] = array(
+		"#type" => "hidden",
+		"#suffix" => "</div>",
+	);
+
+	$form["qualifications"] = array(
+		"#type" => "fieldset",
+		"#title" => t("Conditions of Qualification"),
+		"#collapsible" => TRUE,
+		"#description" => t("Carts qualify for a discount based upon these conditions."),
+	);
+
+	$form["qualifications"]["qualifying_type"] = array(
+		"#type" => "select",
+		"#title" => t("Qualification type"),
+		"#description" => t("The type of qualification used to determine if a cart qualifies for this discount."),
 		"#options" => qualifying_type_options(),
 		"#default_value" => $form_state["values"]["qualifying_type"],
 	);
 
-	$form["qualifying_amount"] = array(
+	$form["qualifications"]["qualifying_amount"] = array(
 		"#type" => "textfield",
-		"#title" => t("Qualifying amount"),
-		"#description" => t("The amount required to qualify for the discount.	E.g. 50 (for $50), 5 (for 5 items).  Remember for a discount like 'buy 4 get 1 free' the qualifying amount is '5'."),
+		"#title" => t("Qualification amount"),
+		"#description" => t("The amount of qualification type required. E.g. 50 (for $50), 5 (for 5 items). Remember for a discount like 'buy 4 get 1 free' the qualifying amount is '5'."),
 		"#default_value" => $form_state["values"]["qualifying_amount"],
 		"#size" => 15,
 		"#required" => TRUE,
 	);
 
-
 	$has_qualifying_amount_max = isset($form_state["values"]["has_qualifying_amount_max"]) ? $form_state["values"]["has_qualifying_amount_max"] : HAS_QUALIFYING_AMOUNT_MAX_DEFAULT;
 	$display_string = ($has_qualifying_amount_max) ? "" : " style='display:none'";
 
@@ -132,12 +197,12 @@
 		$qualifying_amount_max_container_id, $qualifying_amount_max_container_id, 
 		$qualifying_amount_max_container_id, $qualifying_amount_max_container_id);
 
-	$form["qualifying_amount_max_header"] = array(
+	$form["discount_set"]["qualifying_amount_max_header"] = array(
 		"#type" => "hidden",
 		"#prefix" => $qualifying_amount_max_prefix,
 	);
 
-	$form["has_qualifying_amount_max"] = array(
+	$form["discount_set"]["has_qualifying_amount_max"] = array(
 		"#type" => "checkbox",
 		"#title" => t("Has max qualifying amount."),
 		"#description" => t("Whether or not discount contains maximum qualifying amount."),
@@ -148,7 +213,7 @@
 	$qualifying_amount_max = $form_state["values"]["role_ids"];
 
 	//Create qualifying_amount_max form element
-	$form["qualifying_amount_max"] = array(
+	$form["discount_set"]["qualifying_amount_max"] = array(
 		"#type" => "textfield",
 		"#title" => t("Maximum qualifying amount"),
 		"#description" => t("Maximum amount to NOT exceed to qualify for the discount.  E.g. 50 (for $50), 5 (for 5 items)."),
@@ -159,13 +224,19 @@
 		"#suffix" => "</div>",
 	);
 
-	$form["qualifying_amount_max_footer"] = array(
+	$form["discount_set"]["qualifying_amount_max_footer"] = array(
 		"#type" => "hidden",
 		"#suffix" => "</div>",
 	);
 
+	$form["discount_set"] = array(
+		"#type" => "fieldset",
+		"#title" => t("Discount Application"),
+		"#description" => t("Details of the discount to apply"),
+		"#collapsible" => TRUE,
+	);
 
-	$form["discount_type"] = array(
+	$form["discount_set"]["discount_type"] = array(
 		"#type" => "select",
 		"#title" => t("Discount type"),
 		"#description" => t("Type of discount to apply."),
@@ -173,7 +244,7 @@
 		"#default_value" => $form_state["values"]["discount_type"],
 	);
 
-	$form["discount_amount"] = array(
+	$form["discount_set"]["discount_amount"] = array(
 		"#type" => "textfield",
 		"#title" => t("Discount amount"),
 		"#description" => t("The amount of discount.	E.g. 50 (for $50), 5 (for 5 items), or 0.05 (for 5%)"),
@@ -191,14 +262,14 @@
 		. "else { jQuery(\"#%s .codes-container\").slideUp(); }'>", 
 		$codes_container_id, $codes_container_id, $codes_container_id, $codes_container_id, $codes_container_id, $codes_container_id);
 
-	$form["codes_header"] = array(
+	$form["qualifications"]["codes_header"] = array(
 		"#type" => "hidden",
 		"#prefix" => $codes_prefix,
 	);
 
-	$form["requires_code"] = array(
+	$form["qualifications"]["requires_code"] = array(
 		"#type" => "checkbox",
-		"#title" => t("Require(s) code to activate discount."),
+		"#title" => t("Require code to activate discount."),
 		"#default_value" => $requires_code,
 	);
 
@@ -214,7 +285,7 @@
 		$codes_string = create_codes_string($codes);
 	}
 
-	$form["codes"] = array(
+	$form["qualifications"]["codes"] = array(
 		"#type" => "textarea",
 		"#title" => t("Discount codes")
 			. sprintf("<span title='" . t("This field is required.") . "' class='form-required'>*</span>"),
@@ -225,7 +296,7 @@
 		"#suffix" => "</div></div>",
 	);
 
-	$form["codes_footer"] = array(
+	$form["qualifications"]["codes_footer"] = array(
 		"#type" => "hidden",
 		"#suffix" => "</div>",
 	);
@@ -246,7 +317,7 @@
 		FILTER_TYPE_TERMS,  $filter_type_container_id, $filter_type_container_id, $filter_type_container_id, 
 		FILTER_TYPE_SKUS,  $filter_type_container_id, $filter_type_container_id, $filter_type_container_id);
 
-	$form["filter_type_header"] = array(
+	$form["discount_set"]["filter_type_header"] = array(
 		"#type" => "hidden",
 		"#prefix" => $filter_type_prefix,
 	);
@@ -256,10 +327,10 @@
 	$options[FILTER_TYPE_PRODUCTS] = t("Filter By Products");
 	$options[FILTER_TYPE_TERMS] = t("Filter By Terms");
 	$options[FILTER_TYPE_SKUS] = t("Filter By SKUs");
-	$form["filter_type"] = array(
+	$form["discount_set"]["filter_type"] = array(
 		"#type" => "select",
 		"#title" => t("Filter Type"),
-		"#description" => t("Determine whether to filter by products, terms or SKUs."),
+		"#description" => t("Determine whether to filter by products, terms or SKUs. Discount is applied to only these products."),
 		"#options" => $options,
 		"#default_value" => $filter_type,
 		"#required" => TRUE,
@@ -279,7 +350,7 @@
 	$options[ALL_PRODUCTS] = t("<All Products>");
 	while ( $row = db_fetch_object($result) )
 		$options[$row->nid] = $row->title . " (" . $row->model . ")";
-	$form["product_ids"] = array(
+	$form["discount_set"]["product_ids"] = array(
 		"#type" => "select",
 		"#title" => t("Products")
 			. sprintf("<span title='" . t("This field is required.") . "' class='form-required'>*</span>"),
@@ -304,7 +375,7 @@
 	$options[ALL_TERMS] = t("<All Terms>");
 	while ( $row = db_fetch_object($result) )
 		$options[$row->tid] = $row->name;
-	$form["term_ids"] = array(
+	$form["discount_set"]["term_ids"] = array(
 		"#type" => "select",
 		"#title" => t("Terms")
 			. sprintf("<span title='" . t("This field is required.") . "' class='form-required'>*</span>"),
@@ -329,7 +400,7 @@
 	$options[ALL_SKUS] = t("<All SKUs>");
 	while ( $row = db_fetch_object($result) )
 		$options[$row->model] = $row->model . " (" . $row->title . ")";
-	$form["skus"] = array(
+	$form["discount_set"]["skus"] = array(
 		"#type" => "select",
 		"#title" => t("SKUs")
 			. sprintf("<span title='" . t("This field is required.") . "' class='form-required'>*</span>"),
@@ -341,7 +412,7 @@
 		"#suffix" => "</div>",
 	);
 
-	$form["filter_type_footer"] = array(
+	$form["discount_set"]["filter_type_footer"] = array(
 		"#type" => "hidden",
 		"#suffix" => "</div>",
 	);
@@ -355,14 +426,14 @@
 		. "else { jQuery(\"#%s .roles-container\").slideUp(); }'>", 
 		$roles_container_id, $roles_container_id, $roles_container_id, $roles_container_id, $roles_container_id, $roles_container_id);
 
-	$form["roles_header"] = array(
+	$form["qualifications"]["roles_header"] = array(
 		"#type" => "hidden",
 		"#prefix" => $roles_prefix,
 	);
 
-	$form["has_role_filter"] = array(
+	$form["qualifications"]["has_role_filter"] = array(
 		"#type" => "checkbox",
-		"#title" => t("Filter based on roles."),
+		"#title" => t("Qualification by Role(s)."),
 		"#default_value" => $has_role_filter,
 	);
 
@@ -380,11 +451,11 @@
 	$options[ALL_ROLES] = t("<All Roles>");
 	while ( $row = db_fetch_object($result) )
 		$options[$row->rid] = $row->name;
-	$form["role_ids"] = array(
+	$form["qualifications"]["role_ids"] = array(
 		"#type" => "select",
 		"#title" => t("Roles")
 			. sprintf("<span title='" . t("This field is required.") . "' class='form-required'>*</span>"),
-		"#description" => t("Select all roles this discount applies to or &lt;All Roles&gt; to apply to all roles."),
+		"#description" => t("Select all roles that this discount applies to or &lt;All Roles&gt; to apply to all roles."),
 		"#options" => $options,
 		"#default_value" => $role_ids,
 		"#multiple" => TRUE,
@@ -392,19 +463,35 @@
 		"#suffix" => "</div>",
 	);
 
-	$form["roles_footer"] = array(
+	$form["qualifications"]["roles_footer"] = array(
 		"#type" => "hidden",
 		"#suffix" => "</div>",
 	);
 
-	$form["requires_single_product_to_qualify"] = array(
+	$form["qualifications"]["requires_single_product_to_qualify"] = array(
 		"#type" => "checkbox",
 		"#title" => t("Require single product to qualify."),
 		"#description" => t("Requires a single product to meet the qualifying requirements to receive discount.	Otherwise qualifying requirements may be met by the combination of selected products."),
 		"#default_value" => isset($form_state["values"]["requires_single_product_to_qualify"]) ? $form_state["values"]["requires_single_product_to_qualify"] : REQUIRES_SINGLE_PRODUCT_TO_QUALIFY_DEFAULT,
 	);
 
-	$form["max_times_applied"] = array(
+	//Create SKUs form element
+	$options = array();
+	$result = db_query("SELECT p.model, n.title FROM {uc_products} p, {node} n WHERE p.nid=n.nid ORDER BY p.model");
+	$options[NO_SKUS] = t("Select a single SKU...");
+	while ( $row = db_fetch_object($result) )
+		$options[$row->model] = $row->model . " (" . $row->title . ")";
+
+		$form["qualifications"]["required_product"] = array(
+		"#type" => "select",
+		"#title" => t("Required product"),
+		"#description" => t("Select a single product that the cart must contain."),
+		"#options" => $options,
+		"#default_value" => $form_state["values"]["required_product"],
+	);
+
+
+	$form["discount_set"]["max_times_applied"] = array(
 		"#type" => "textfield",
 		"#title" => t("Maximum times applied"),
 		"#description" => t("Number of times this discount can be applied to a single cart (0 for unlimited).  Does not apply to 'Percent off' discounts."),
@@ -414,15 +501,7 @@
 		"#required" => TRUE,
 	);
 
-	$form["can_be_combined_with_other_discounts"] = array(
-		"#type" => "checkbox",
-		"#title" => t("Can be combined with other discounts"),
-		"#description" => t("Whether or not this discount will be applied if other discounts are."),
-		"#default_value" => isset($form_state["values"]["can_be_combined_with_other_discounts"]) 
-			? $form_state["values"]["can_be_combined_with_other_discounts"] : CAN_BE_COMBINED_WITH_OTHER_DISCOUNTS_DEFAULT,
-	);
-
-	$form["max_uses"] = array(
+	$form["discount_set"]["max_uses"] = array(
 		"#type" => "textfield",
 		"#title" => t("Max uses"),
 		"#description" => t("Number of times this discount can be applied (0 for unlimited)."),
@@ -432,7 +511,7 @@
 		"#required" => TRUE,
 	);
 
-	$form["max_uses_per_user"] = array(
+	$form["discount_set"]["max_uses_per_user"] = array(
 		"#type" => "textfield",
 		"#title" => t("Max uses per user"),
 		"#description" => t("Number of times this discount can be applied to a particular user (0 for unlimited)."),
@@ -442,7 +521,7 @@
 		"#required" => TRUE,
 	);
 
-	$form["max_uses_per_code"] = array(
+	$form["discount_set"]["max_uses_per_code"] = array(
 		"#type" => "textfield",
 		"#title" => t("Max uses per code"),
 		"#description" => t("Number of times this discount can be applied to a particular code (0 for unlimited).  Note: if 'Max uses' (if set) still applies as overall maximum number of uses for this discount."),
@@ -452,45 +531,6 @@
 		"#required" => TRUE,
 	);
 
-	$has_expiration = isset($form_state["values"]["has_expiration"]) ? $form_state["values"]["has_expiration"] : HAS_EXPIRATION_DEFAULT;
-	$display_string = ($has_expiration) ? "" : " style='display:none'";
-
-	$expiration_container_id = "expiration-container-" . $seed;
-	$expiration_prefix = sprintf("<div id='%s' onclick='if ( jQuery(\"#%s input[type=\\\"checkbox\\\"]\").attr(\"checked\") )"
-		. "{ jQuery(\"#%s .expiration-container\").slideDown(); }"
-		. "else { jQuery(\"#%s .expiration-container\").slideUp(); }'>", 
-		$expiration_container_id, $expiration_container_id, $expiration_container_id, $expiration_container_id, $expiration_container_id, $expiration_container_id);
-
-	$form["expiration_header"] = array(
-		"#type" => "hidden",
-		"#prefix" => $expiration_prefix,
-	);
-
-	$form["has_expiration"] = array(
-		"#type" => "checkbox",
-		"#title" => t("Discount expires"),
-		"#description" => t("Whether or not this discount will be applied if other discounts are."),
-		"#default_value" => $has_expiration,
-	);
-
-	$expiration = isset($form_state["values"]["expiration"]) 
-		? $form_state["values"]["expiration"] : time();
-	$form["expiration"] = array(
-		"#type" => "date_popup",
-		"#date_type" => "DATE_UNIX",
-		"#title" => t("Discount expiration")
-			. sprintf("<span title='" . t("This field is required.") . "' class='form-required'>*</span>"),
-		"#description" => t("Date and time when the discount expires."),
-		"#default_value" => date("Y-m-d H:i:s", $expiration),
-		"#prefix" => sprintf("<div class='expiration-container'%s>", $display_string),
-		"#suffix" => "</div>",
-	);
-
-	$form["expiration_footer"] = array(
-		"#type" => "hidden",
-		"#suffix" => "</div>",
-	);
-
 	$is_published = isset($form_state["values"]["is_published"]) 
 		? $form_state["values"]["is_published"] : IS_PUBLISHED_DEFAULT;
 	$form["is_published"] = array(
@@ -500,17 +540,6 @@
 		"#default_value" => $is_published,
 	);
 
-	//Add custom weight range [-50, 50]
-	$options = array();
-	for ($i = -50; $i <= 50; $i++)
-		$options[$i] = $i;
-	$form["weight"] = array(
-		"#type" => "select",
-		"#title" => t("Weight"),
-		"#description" => t("Lighter discounts are applied to an order first. This value is unimportant if there are no discounts on discount line items."),
-		"#options" => $options,
-		"#default_value" => isset($form_state["values"]["weight"]) ? $form_state["values"]["weight"] : 0,
-	);
 
 	$form["submit"] = array( "#type" => "submit", "#value" => t("Submit") );
 
@@ -531,6 +560,12 @@
 	    $has_errors = TRUE;
 	}
 
+	//Check for required_product if requires_single checked
+	if ($values["requires_single_product_to_qualify"] && empty($values["required_product"]))
+	{
+		form_set_error( "required_product", t("Please select a single SKU for 'Require single product to qualify'.") );
+		$has_errors = TRUE;
+	}
 
 	//Check qualifying_amount_max (if necessary)
 	if ($values["has_qualifying_amount_max"])
@@ -675,6 +710,7 @@
 				$form_state["values"]["filter_type"],
 				$form_state["values"]["has_role_filter"],
 				$form_state["values"]["requires_single_product_to_qualify"],
+				$form_state["values"]["required_product"],
 				$form_state["values"]["max_times_applied"],
 				$form_state["values"]["can_be_combined_with_other_discounts"],
 				$form_state["values"]["max_uses"],
@@ -706,6 +742,7 @@
 				$form_state["values"]["filter_type"],
 				$form_state["values"]["has_role_filter"],
 				$form_state["values"]["requires_single_product_to_qualify"],
+				$form_state["values"]["required_product"],
 				$form_state["values"]["max_times_applied"],
 				$form_state["values"]["can_be_combined_with_other_discounts"],
 				$form_state["values"]["max_uses"],
@@ -813,6 +850,7 @@
 		$discount->filter_type,
 		$discount->has_role_filter,
 		$discount->requires_single_product_to_qualify,
+		$discount->required_product,
 		$discount->max_times_applied,
 		$discount->can_be_combined_with_other_discounts,
 		$discount->max_uses,
