--- modulos-nuevos/uc_discounts_alt/uc_discounts/uc_discounts.module 
+++ modulos_patched/uc_discounts_alt/uc_discounts/uc_discounts.module 
@@ -49,7 +49,8 @@
 define("CALCULATE_DISCOUNT_RESPONSE_LINE_ITEMS_KEY", "line_items");
 define("CALCULATE_DISCOUNT_RESPONSE_ERRORS_KEY", "errors");
 define("CALCULATE_DISCOUNT_RESPONSE_MESSAGES_KEY", "messages");
-
+// Tax rates ids
+define("DISCOUNT_RATES_IDS_KEY", "discount_rates_ids");
 
 ///////////////////////////////////////////////////////////////////
 //Drupal Hooks
@@ -171,7 +172,8 @@
 
         //Delete existing discount line items
         foreach ($existing_line_items as $line_item) {
-          if ($line_item["type"] == LINE_ITEM_KEY_NAME) {
+          // Allow LINE_ITEM_KEY_NAME suffixes
+          if (strpos($line_item["type"], LINE_ITEM_KEY_NAME) !== false) {
             uc_order_delete_line_item($line_item["line_item_id"]);
           }
           //Otherwise store non-discount line item
@@ -273,19 +275,38 @@
  * Implementation of Ubercart's hook_line_item().
  *
  * Displays all discounts as a single line item
- *
+ * If uc_vat and uc_taxes modules exist and enabled: display a discount line item per VAT type
+ * 
  * @see hook_line_item()
  */
 function uc_discounts_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,
-  );
+  // 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,
+    );
+  }
   return $line_items;
 }
 
@@ -412,6 +433,8 @@
             "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"),
@@ -467,7 +490,8 @@
   $line_items = array();
   foreach ($discounts as $discount) {
     $line_item = array(
-      "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,
       "title" => $discount->short_description,
       "amount" => -$discount->amount,
       "weight" => LINE_ITEM_WEIGHT,
@@ -486,12 +510,15 @@
   if (is_array($order->line_items)) {
     $existing_line_items = $order->line_items;
   }
-  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);
+  }
 
   $line_items = array();
   foreach ($existing_line_items as $line_item) {
     //If line item type is LINE_ITEM_KEY_NAME, add it to array
-    if ($line_item["type"] == LINE_ITEM_KEY_NAME) {
+    // Allow LINE_ITEM_KEY_NAME suffixes
+    if (strpos($line_item["type"],LINE_ITEM_KEY_NAME) !== false) {
       $line_items[] = $line_item;
     }
   }
@@ -534,14 +561,24 @@
   $discounts = get_discounts_for_order($order, $errors, $warnings, $messages);
 
   $i = 0;
+  // Hold the rates ids involved in discounts
+  $discount_rates_ids = array();
   foreach ($discounts as $discount) {
     $line_item           = array();
-    $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;
   }
 
   if (!empty($warnings)) {
@@ -553,6 +590,8 @@
   $calculate_discount_response = array(CALCULATE_DISCOUNT_RESPONSE_LINE_ITEMS_KEY => $line_items,
     CALCULATE_DISCOUNT_RESPONSE_ERRORS_KEY => $errors,
     CALCULATE_DISCOUNT_RESPONSE_MESSAGES_KEY => $messages,
+    // Pass the discount rates ids
+    DISCOUNT_RATES_IDS_KEY => $discount_rates_ids,
   );
   drupal_json($calculate_discount_response);
   exit();
@@ -1393,6 +1432,11 @@
       }
     }
 
+    // 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);
+    }
+
     switch ($discount->discount_type) {
       case DISCOUNT_TYPE_PERCENTAGE_OFF_PER_QUALIFYING_ITEM:
         $order_and_discount_product_ids = array_intersect($discount_product_ids, $order_product_ids);
@@ -1834,4 +1878,62 @@
   //	error_log($s);
 }
 
-
+/**
+ * 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"];
+      }
+    }
+  }
+}
+
