### Eclipse Workspace Patch 1.0
#P ecommerce_47
Index: cart/cart.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/ecommerce/cart/cart.module,v
retrieving revision 1.133.2.6
diff -u -r1.133.2.6 cart.module
--- cart/cart.module	9 Sep 2006 03:18:14 -0000	1.133.2.6
+++ cart/cart.module	19 Sep 2006 03:17:23 -0000
@@ -690,12 +690,12 @@
       if (is_array($txn->misc)) {
         usort($txn->misc, 'store_transaction_misc_sort');
       }
-
+
       foreach ((array)$txn->items as $product) {
         $price = store_adjust_misc($txn, $product);
         $node = node_load($product->nid);
         $subtotal = product_has_quantity($node) ? ($price * $product->qty) : $price;
-
+
         $form['items'][$product->nid] = array(
           'qty' => array('#value' => product_has_quantity($node) ? $product->qty : ''),
           'item' => array('#type' => 'value', '#value' => $product), // allow more info to be displayed by the theme
@@ -708,18 +708,54 @@
       }

       $form['totals'] = array();
+
+      // Since we may remove items, keep a track of the current key.
+      $line = 0;
+      // Keep track of which lines are subtotals.
+      $st = array('#row_type' => 'ST');
+
+      // Create a subtotal line.
+      // If the Total comes immediately afterwards, then it will be repressed later.
+      $form['totals'][$line++] = array('#title' => t('Subtotal'), '#value' => $txn->subtotal, 'info' => $st);
+
       if ($txn->misc) {
         foreach ($txn->misc as $misc) {
           if (!$misc->seen) {
-            // Only add the subtotal line if there are other items in misc.
-            if ($form['totals']) {
-              $form['totals'][] = array('#title' => t('Subtotal'), '#value' => $txn->subtotal);
+            // Subtotal
+            if ($misc->subtotal_before && ($form['totals'][$line-1]['info']['#row_type'] != $st['#row_type'])) {
+              $form['totals'][$line++] = array('#title' => t('Subtotal'), '#value' => $txn->subtotal, 'info' => $st);
+            }
+          }
+
+          // Do this just like store_transaction_calc_gross
+          if (function_exists($misc->callback)) {
+            $f = $misc->callback;
+            $amount = $f($txn, $misc, $txn->subtotal);
+          }
+          elseif ($misc->qty) {
+            $amount = ($misc->price * $misc->qty);
+          }
+          else {
+            $amount = $misc->price;
+          }
+          if(!$misc->already_added) { $txn->subtotal+= $amount; }
+
+           if (!$misc->seen) {
+            // Misc Item
+            $form['totals'][$line++] = array('#title' => t($misc->description), '#value' => $amount);
+            // Subtotal
+            if ($misc->subtotal_after) {
+              $form['totals'][$line++] = array('#title' => t('Subtotal'), '#value' => $txn->subtotal, 'info' => $st);
             }
-            $form['totals'][] = array('#title' => t($misc->description), '#value' => $misc->price);
           }
         }
       }
-
+
+      // Remove any subtotal occurring before the total.
+      if ($form['totals'][$line-1]['info']['#row_type'] == $st['#row_type']) {
+        unset($form['totals'][$line-1]);
+      }
+      // Grand total
       $form['totals'][] = array('#title' => t('Total'), '#value' => $txn->gross);

       $form = array('cart' => $form);
Index: store/store.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/ecommerce/store/store.module,v
retrieving revision 1.125.2.13
diff -u -r1.125.2.13 store.module
--- store/store.module	9 Sep 2006 03:18:14 -0000	1.125.2.13
+++ store/store.module	19 Sep 2006 03:17:25 -0000
@@ -2508,13 +2508,18 @@
   foreach ((array)$txn->items as $item) {
     $total+= product_has_quantity($item) ? $item->price * $item->qty : $item-> price;
   }
-  foreach ((array)$txn->misc as $misc) {
-    if ($misc->qty) {
-      $total+= ($misc->price * $misc->qty);
+  foreach ((array)$txn->misc as $key => $misc) {
+    if (function_exists($misc->callback)) {
+      $f = $misc->callback;
+      $amount = $f($txn, $misc, $total);
     }
-    else {
-      $total+= $misc->price;
+    elseif ($misc->qty) {
+      $amount = ($misc->price * $misc->qty);
     }
+    else {
+      $amount = $misc->price;
+     }
+    if(!$misc->already_added) {$total+= $amount;};
   }
   return $total;
 }
