Index: uc_discounts.js
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/uc_discounts_alt/uc_discounts/uc_discounts.js,v
retrieving revision 1.5.2.3
diff -r1.5.2.3 uc_discounts.js
105a106
>         var discount_rates_ids = null;
116c117
<         uc_discountsRenderLineItems(line_items, true);
---
>         uc_discountsRenderLineItems(line_items, true, discount_rates_ids);
143c144
< function uc_discountsRenderLineItems(line_items, show_message)
---
> function uc_discountsRenderLineItems(line_items, show_message, discount_rates_ids)
150a152,162
>     // Calculate totals per discount rate id
> 
>     var total_amount = [];
>     var total_amount_names = [];
> 
>     // Reset totals
>     for (i = 0; i < line_items.length; i) {
>       var line_item = line_items[i];
>       total_amount[parseInt(line_item["rate_type"])] = 0;
>       total_amount_names[parseInt(line_item["rate_type"])] = "";
>     }
154c166,169
<         total_amount += parseFloat(line_item["amount"]);
---
>         total_amount[parseInt(line_item["rate_type"])] += parseFloat(line_item["amount"]);
>         if (line_item["rate_name"] != "") {
>           total_amount_names[parseInt(line_item["rate_type"])] = " - " + line_item["rate_name"];
>         }
157c172
<     //Add total discount line item
---
>     //Add a total discount line item per rate type
162a178,189
>         var index = 0;
>         for (i = 0; i < discount_rates_ids.length; i) {
>           // Extract the index from discount key name
>           suffix = discount_rates_ids[i].substr(12);
>           if (suffix == "") {
>             suffix = 0;
>           }
>           index = parseInt(suffix);
>           set_line_item(discount_rates_ids[i], 
>               Drupal.settings.uc_discounts.total_discount_text  total_amount_names[index], total_amount[index], 
>               parseFloat(Drupal.settings.uc_discounts.line_item_weight)  0.5, 1, false);
>         }
180a208,211
>     //Remove total discount line items
>     for (i = 0; i < discount_rates_ids.length; i) {
>       remove_line_item(discount_rates_ids[i]);
>     }
Index: uc_discounts.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/uc_discounts_alt/uc_discounts/uc_discounts.module,v
retrieving revision 1.29.2.13
diff -r1.29.2.13 uc_discounts.module
174c174,175
<           if ($line_item["type"] == LINE_ITEM_KEY_NAME) {
---
>           // Allow LINE_ITEM_KEY_NAME suffixes
>           if (strpos($line_item["type"], LINE_ITEM_KEY_NAME) !== false) {
276a278,279
>  * If uc_vat and uc_taxes modules exist and enabled: display a discount line item per VAT type
>  * 
280,288c283,309
<   $line_items[] = array(
<     "id" => LINE_ITEM_KEY_NAME,
<     "title" => t("Discount"),
<     "weight" => LINE_ITEM_WEIGHT,
<     "stored" => TRUE,
<     // Added to total
<     "calculated" => TRUE,
<     "display_only" => FALSE,
<   );
---
>   // If uc_vat module exists and enabled, add a line item per VAT type
>   if (module_exists('uc_vat') && module_exists('uc_taxes')) {
>     $taxes = uc_taxes_rate_load();
>     foreach ($taxes as $tax) {
>       $line_items[] = array(
>         "id" => LINE_ITEM_KEY_NAME . $tax->id,
>         "title" => t("Discount") . ' ' . check_plain($tax->name),
>         "weight" => LINE_ITEM_WEIGHT,
>         "stored" => TRUE,
>         // Added to total
>         "calculated" => TRUE,
>         "display_only" => FALSE,
>       );
>     }
>   }
>   else {
>     // If not, single line item
>     $line_items[] = array(
>       "id" => LINE_ITEM_KEY_NAME,
>       "title" => t("Discount"),
>       "weight" => LINE_ITEM_WEIGHT,
>       "stored" => TRUE,
>       // Added to total
>       "calculated" => TRUE,
>       "display_only" => FALSE,
>     );
>   }
380d400
< 
387,427c407,456
< 
<       $contents["uc-discounts-codes"] = array(
<         "#type" => "textarea",
<         "#default_value" => $codes_string,
<         "#rows" => 5,
<         "#prefix" => "<div class='discount-codes-wrapper'>",
<         "#suffix" => "</div>",
<       );
< 
<       $contents["uc-discounts-placeholder"] = array(
<         "#type" => "hidden",
<         "#prefix" => "<div class='uc-discounts-messages-container'>",
<         "#suffix" => "</div>",
<       );
< 
<       $contents["uc-discounts-button"] = array(
<         "#type" => "button",
<         "#value" => t("Click to calculate discounts"),
<       );
< 
<       drupal_add_js(array(
<           "uc_discounts" => array(
<             "line_item_key_name" => LINE_ITEM_KEY_NAME,
<             "line_item_weight" => LINE_ITEM_WEIGHT,
<             "total_discount_text" => t("Total discount"),
<             "calculate_discount_response_line_items_key" => CALCULATE_DISCOUNT_RESPONSE_LINE_ITEMS_KEY,
<             "calculate_discount_response_errors_key" => CALCULATE_DISCOUNT_RESPONSE_ERRORS_KEY,
<             "calculate_discount_response_messages_key" => CALCULATE_DISCOUNT_RESPONSE_MESSAGES_KEY,
<             "progress_msg" => t("Calculating discounts..."),
<             "no_codes_entered" => t("Please enter at least one code"),
<             "no_applicable_discounts" => t("No applicable discounts"),
<             "err_msg" => t("There were problems determining if any discounts apply.  Please try again shortly.\nIf this does not resolve the issue, please call @phone to complete your order.",
<               array("@phone" => variable_get("uc_store_phone", NULL))
<             ),
<             "response_parse_err_msg" => t("Unable to parse response text: "),
<           ),
<         ), "setting");
< 
<       drupal_add_js("$(document).ready(function() { uc_discountsOnLoad(); });", "inline");
<       drupal_add_js("misc/progress.js");
<       drupal_add_js(drupal_get_path("module", "uc_discounts") ."/uc_discounts.js");
---
>       
>       //Check to see if any codes require textfield input
>       foreach ($codes_string as $code) {
>         $display = db_result(db_query("SELECT requires_code FROM {uc_discounts} WHERE discount_id=%d", $code));
>         if ($display == 1) {      
>           $contents["uc-discounts-codes"] = array(
>             "#type" => "textarea",
>             "#default_value" => $codes_string,
>             "#rows" => 5,
>             "#prefix" => "<div class='discount-codes-wrapper'>",
>             "#suffix" => "</div>",
>           );
>     
>           $contents["uc-discounts-placeholder"] = array(
>             "#type" => "hidden",
>             "#prefix" => "<div class='uc-discounts-messages-container'>",
>             "#suffix" => "</div>",
>           );
>     
>           $contents["uc-discounts-button"] = array(
>             "#type" => "button",
>             "#value" => t("Click to calculate discounts"),
>           );
>     
>           drupal_add_js(array(
>               "uc_discounts" => array(
>                 "line_item_key_name" => LINE_ITEM_KEY_NAME,
>                 "line_item_weight" => LINE_ITEM_WEIGHT,
>                 "total_discount_text" => t("Total discount"),
>                 "calculate_discount_response_line_items_key" => CALCULATE_DISCOUNT_RESPONSE_LINE_ITEMS_KEY,
>                 "calculate_discount_response_errors_key" => CALCULATE_DISCOUNT_RESPONSE_ERRORS_KEY,
>                 "calculate_discount_response_messages_key" => CALCULATE_DISCOUNT_RESPONSE_MESSAGES_KEY,
>                 // discount_rates_ids are used to distinguish the different discount line items
>                 "discount_rates_ids" => DISCOUNT_RATES_IDS_KEY,
>                 "progress_msg" => t("Calculating discounts..."),
>                 "no_codes_entered" => t("Please enter at least one code"),
>                 "no_applicable_discounts" => t("No applicable discounts"),
>                 "err_msg" => t("There were problems determining if any discounts apply.  Please try again shortly.\nIf this does not resolve the issue, please call @phone to complete your order.",
>                   array("@phone" => variable_get("uc_store_phone", NULL))
>                 ),
>                 "response_parse_err_msg" => t("Unable to parse response text: "),
>               ),
>             ), "setting");
>     
>           drupal_add_js("$(document).ready(function() { uc_discountsOnLoad(); });", "inline");
>           drupal_add_js("misc/progress.js");
>           drupal_add_js(drupal_get_path("module", "uc_discounts") ."/uc_discounts.js");        
>           break;
>         }
>       }
470c499,500
<       "type" => LINE_ITEM_KEY_NAME,
---
>     // If exists the rate_type, use it
>       "type" => empty($discount->rate_type) ? LINE_ITEM_KEY_NAME : LINE_ITEM_KEY_NAME . $discount->rate_type,
489c519,521
<   else $existing_line_items = uc_order_load_line_items($order->order_id, TRUE);
---
>   else {
>     $existing_line_items = uc_order_load_line_items($order->order_id, TRUE);  
>   }
494c526,527
<     if ($line_item["type"] == LINE_ITEM_KEY_NAME) {
---
>     // Allow LINE_ITEM_KEY_NAME suffixes
>     if (strpos($line_item["type"],LINE_ITEM_KEY_NAME) !== false) {
536a570,571
>   // Hold the rates ids involved in discounts
>   $discount_rates_ids = array();
539,544c574,587
<     $line_item["id"]     = LINE_ITEM_KEY_NAME . $i++;
<     $line_item["type"]   = $discount->type;
<     $line_item["title"]  = $discount->title;
<     $line_item["amount"] = -$discount->amount;
<     $line_item["weight"] = $discount->weight;
<     $line_items[]        = $line_item;
---
>     // Add rate type suffix if exist
>     $line_item["id"]        = LINE_ITEM_KEY_NAME . (empty($discount->rate_type) ? '' : $discount->rate_type);
>     // If line item id doesn't exists in array, add it'
>     if (!in_array($line_item["id"],$discount_rates_ids)) {
>       $discount_rates_ids[] = $line_item["id"];
>     }
>     $line_item["type"]      = $discount->type;
>     $line_item["title"]     = $discount->title;
>     $line_item["amount"]    = -$discount->amount;
>     $line_item["weight"]    = $discount->weight;
>     // Add the rate type and rate name if not empty
>     $line_item["rate_type"] = empty($discount->rate_type) ? 0 : $discount->rate_type;
>     $line_item["rate_name"] = empty($discount->rate_name) ? "" : $discount->rate_name;
>     $line_items[]           = $line_item;
555a599,600
>     // Pass the discount rates ids
>     DISCOUNT_RATES_IDS_KEY => $discount_rates_ids,
1395a1441,1445
>     // If uc_vat module exists, add the rate type and rate name to discount
>     if (module_exists('uc_vat') && module_exists('uc_taxes')) {
>       uc_discounts_add_discount_rate_id($discount, $order_and_discount_product_ids);
>     }
> 
1836a1887,1944
> /**
>  * Returns an array with taxed product types as keys and
>  * an array with tax rate id and tax rate name as values
>  */
> function uc_discounts_get_taxes_array() {
>   $rates = uc_taxes_rate_load();
>   $taxes = array();
>   foreach ($rates as $rate) {
>     foreach ($rate->taxed_product_types as $type) {
>       // The tax type per product type (class) must be unique!
>       $taxes[$type] = array(
>         "id" => $rate->id,
>         "name" => $rate->name,
>       );
>     }
>   }
>   return $taxes;
> }
> 
> /**
>  * Return an array with the cart products nids as keys and the product type (class) as values
>  */
> function uc_discounts_get_cart_nids() {
>   // Get the cart_id
>   $cid = uc_cart_get_id(FALSE);
>   // If we didn't get a cid, return empty.
>   if (!$cid) {
>     return array();
>   }
>   $result = db_query("SELECT n.nid, n.type FROM {node} n INNER JOIN {uc_cart_products} c ON n.nid = c.nid WHERE c.cart_id = '%s'", $cid);
>   $cart_products_nids = array();
>   while ($item = db_fetch_object($result)) {
>     $cart_products_nids[$item->nid] = $item->type;
>   }
>   return $cart_products_nids;
> }
> 
> /**
>  * Add rate type and rate name to discount
>  * Warning: Products that qualifying for this discount must have the same tax rate,
>  * if mixed the rate applied to discount will be random and the calculations will be wrong
>  */
> function uc_discounts_add_discount_rate_id(&$discount, $products_with_discount_ids) {
>   $taxes = uc_discounts_get_taxes_array();
>   $products_in_cart = uc_discounts_get_cart_nids();
>   foreach ($products_with_discount_ids as $product) {
>     // Check if product nid exists in cart
>     if (array_key_exists($product, $products_in_cart)) {
>       // Check if product type (class) exists in taxes
>       if (array_key_exists($products_in_cart[$product],$taxes)) {
>         // Get the tax rate type
>         $discount->rate_type = $taxes[$products_in_cart[$product]]["id"];
>         // and the tax rate name
>         $discount->rate_name = $taxes[$products_in_cart[$product]]["name"];
>       }
>     }
>   }
> }
\ No newline at end of file
