Index: payment/payment.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/ecommerce/payment/payment.module,v
retrieving revision 1.52
diff -u -r1.52 payment.module
--- payment/payment.module	26 Apr 2006 07:40:39 -0000	1.52
+++ payment/payment.module	5 May 2006 22:03:53 -0000
@@ -21,9 +21,6 @@
     case 'Processing error':
       return t("Dear Customer\n\nIt seems there was a problem while processing your order (%txnid). Please contact us at %email for further details.\n\nRegards,\n%site team\n%uri");
 
-    case 'admin/store/settings/payment/adjust':
-      return t("<p>You may define a price adjustment for each role. This adjustment can be a simple price addition, subtraction, or a percentage multiplier. For example, to add 5.00 to every price, enter +5.00. To multiply every price times 75%, enter 75%. If no operator is given, addition is assumed.</p>");
-
     case 'recurring payment':
       return t("Dear %billing_firstname %billing_lastname,\n\nThis message is to inform you that the following item will expire in %time_to_expire.\n\n%renewal_item\n\nTo prevent this from happening, please renew the item as soon as possible.\n\nClick the link below to renew:\n%renewal_link\n\nThanks,\n\n%site\n%uri");
 
@@ -44,11 +41,6 @@
       'access' => user_access('administer payment')
     );
     $items[] = array(
-      'path' => 'admin/store/settings/payment/adjust', 
-      'title' => t('adjustments'),
-      'access' => user_access('administer store')
-    );
-    $items[] = array(
       'path' => 'store/payment',
       'title' => t('Payments'),
       'callback' => 'payment_process_payment',
@@ -416,96 +408,6 @@
   drupal_set_title($title);
   return $output;
 }
-/** 
- * Outputs html form that allows user to set role-based price adjustements.
- * Called from: admin/store/settings/payment/adjust
- * Creates a html output of a form to display. Allows admin to 
- * gives each user type a discount (or price adjustment)
- *  @returns string HTML
- */
-function payment_adjust() {
-
-  $result = db_query('SELECT * FROM {ec_payment_adjust}');
-  while ($data = db_fetch_object($result)) {
-    $adjust[$data->rid] = $data;
-  }
-
-  foreach (user_roles() as $rid => $value) {
-    if ($adjust[$rid]) {
-      if ($adjust[$rid]->operator == '-' || $adjust[$rid]->operator == '+') {
-        $v = $adjust[$rid]->operator . $adjust[$rid]->operand;
-      }
-      else {
-        $v = $adjust[$rid]->operand . '%';
-      }
-    }
-    else {
-      $v = '';
-    }
-    $form['role_' .$rid] = array(
-      '#type' => 'textfield', 
-      '#title' =>  t('Adjustment for the <em>%role_name</em> role',array('%role_name' => $value)), 
-      '#default_value' => $v, 
-      '#size' => 10, 
-      '#maxlength' => 15, 
-      '#desciption' => t('')
-    );
-  }
-  $form['submit'] = array('#type' => 'submit', '#value' => t('Save price adjustments'));
-
-  return drupal_get_form('payment_adjust', $form);
-}
-
-/** 
- * Save contents of payment_adjust.
- * @return string HTML
- */
-function payment_adjust_save($edit) {
-  $error = false;
-
-  foreach ($edit as $key => $value) {
-
-    $rid = substr($key, strrpos($key, '_') + 1); // trim 'role_'
-
-    if (preg_match("/^role_/i", $key) && !empty($value)) {
-      $value = trim($value);
-      $pos1 =  substr($value, 0, 1);
-      if (strstr($pos1, '+')) {
-        $operator = '+';
-        $operand = (int) substr($value, 1, strlen($value));
-      }
-      elseif (strstr($pos1, '-')) {
-        $operator = '-';
-        $operand = (int) substr($value, 1, strlen($value));
-      }
-      elseif (substr($value, strlen($value) - 1) == '%') {
-        $operator = '*';
-        $operand = (int) substr($value, 0, strlen($value) - 1);
-      }
-      else {
-        $operator = '+'; //Default is addition
-        if (!is_numeric($value)) {
-          $error = true;
-        }
-        else {
-          $operand = (int) $value;
-        }
-      }
-
-      db_query('DELETE FROM {ec_payment_adjust} WHERE rid = %d', $rid);
-      db_query("INSERT INTO {ec_payment_adjust} (rid, operator, operand) VALUES (%d, '%s', %f)", $rid, $operator, $operand);
-    }
-  }
-
-  if ($error) {
-    drupal_set_message(t('One of the fields contains non arithmatic information.'), 'error');
-  }
-  else {
-    drupal_set_message(t('Price adjustments have been updated.'));
-  }
-
-  return payment_adjust();
-}
 
 /**
  * Displays the default configuration screen for managing payment information.
@@ -641,60 +543,6 @@
 }
 
 /**
- * Adjust the price if necessary. Make sure a price is always returned.
- */
-function payment_productapi(&$node, $op, $arg) {
-  if (user_access('access content')) {
-
-    switch ($op) {
-
-      case 'adjust_price':
-        /* Role based price adjustment */
-        static $adjust;
-
-        $price = $node->price;
-        $has_adjustment = false;
-        $new_price = $node->price;
-        global $user;
-
-        if ($user->uid > 0) {
-          foreach ($user->roles as $rid => $role) {
-            if (!isset($adjust[$rid])) {
-              $adjust[$rid] = db_fetch_object(db_query('SELECT * FROM {ec_payment_adjust} WHERE rid = %d', $rid));
-            }
-
-            if ($adjust[$rid]) {
-              $has_adjustment = true;
-              switch ($adjust[$rid]->operator) {
-                case '+':
-                  $new_price += $adjust[$rid]->operand;
-                  break;
-                case '-':
-                  $new_price -= $adjust[$rid]->operand;
-                  break;
-                case '*':
-                  $new_price *= $adjust[$rid]->operand /100;
-                  break;
-              }
-            }
-          }
-        }
-
-        if ($has_adjustment) {
-          $new_price = round($new_price, 2);
-          /* Currently we say NO to negative values */
-          return (($new_price >= 0) ? $new_price : 0);
-        }
-        else {
-          return $price;
-        }
-
-        break;
-    }
-  }
-}
-
-/**
  * Implementation of hook_user().
  */
 function payment_user($type, &$edit, &$user) {
Index: product/product.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/ecommerce/product/product.module,v
retrieving revision 1.104
diff -u -r1.104 product.module
--- product/product.module	29 Apr 2006 06:59:06 -0000	1.104
+++ product/product.module	5 May 2006 22:03:56 -0000
@@ -227,6 +227,13 @@
       }
     }
 
+    // Invoke price adjustments for products of this type.
+    $new_price = module_invoke($node->ptype, 'productapi', $node, 'adjust_price', $product->price);
+    $product->price = (($new_price > 0) ? $new_price : $product->price);
+
+    // Get global discounts for this product
+    product_set_discounts($product, $new_price);
+
     // Handle shipping.
     // Don't test for if it *is* 'per_product', test for if it *supports* per_product
     if (function_exists(variable_get('shipping_method', 'none').'_shippingapi')) {
@@ -582,7 +589,37 @@
   }
   return $return;
 }
-/*
+
+/**
+ * The controller for the discount API hooks. It iterates through the enabled
+ * plugins and calls their *_discountapi hook if one exists, passing it the
+ * action (via $op) to execute.
+ *
+ * @param &$node
+ *   Either a node object, node array, or a string containing the node type.
+ * @param $op
+ *   A string containing the name of the discountapi operation.
+ * @param $a3, $a4
+ *   Arguments to pass on to the hook, after the $node and $op arguments.
+ * @return
+ *   The returned value of the invoked hooks.
+ */
+function product_invoke_discountapi(&$node, $op, $a3 = null, $a4 = null) {
+
+  $return = array();
+  foreach (module_list() as $name) {
+    $function = $name ."_discountapi";
+    if (function_exists($function)) {
+      $result = $function($node, $op, $a3, $a4);
+      if (isset($result)) {
+        $return = array_merge($return, $result);
+      }
+    }
+  }
+  return $return;
+}
+
+/**
  *  Helper function to list available product types
  *  In normal use this should not be empty, so the watchdog warning is added.
  *  This warning will occur when someone enables the product module but no modules
@@ -937,31 +974,18 @@
 }
 
 function product_adjust_price($node) {
-  global $user;
-  $on_payment_roles = array_flip((array) variable_get('on_payment_roles', ''));
-
-  $old_price  = $node->price;
-  $new_price = module_invoke($node->ptype, 'productapi', $node, 'adjust_price', $old_price);
-  $new_price = $new_price > 0 ? $new_price: $old_price;
-
-  // User's already in the paid member role should not receive discounts on other
-  // subscriptions.
-  $role_discount = TRUE;
-  if ($node->is_recurring) {
-    foreach($user->roles as $rid => $rname) {
-      if (isset($on_payment_roles[$rid])) {
-        $role_discount = FALSE;
-      }
+  $new_price = $node->price;
+  if (is_array($node->discounts)) {
+    foreach ($node->discounts as $amount) {
+      $new_price += $amount;
     }
   }
 
-  /* Invoke the global role-based price adjustments. */
-  if ($role_discount) {
-    $global_price = module_invoke('payment', 'productapi', $node, 'adjust_price', $new_price);
-    $new_price = $global_price > 0 ? $global_price: $new_price;
-  }
+  return $new_price;
+}
 
-  return (($new_price > 0) ? $new_price : $old_price);
+function product_set_discounts(&$product, $new_price) {
+  $product->discounts = product_invoke_discountapi($product, 'amount', $new_price);
 }
 
 /**
