Index: uc_userpoints_payment.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/userpoints_ubercart/uc_userpoints_payment.module,v
retrieving revision 1.6.2.4
diff -u -p -r1.6.2.4 uc_userpoints_payment.module
--- uc_userpoints_payment.module	5 Jan 2010 08:32:07 -0000	1.6.2.4
+++ uc_userpoints_payment.module	29 Jan 2010 11:24:20 -0000
@@ -3,9 +3,10 @@
 //$Id: uc_userpoints_payment.module,v 1.6.2.4 2010/01/05 08:32:07 betz Exp $
 
 define('USERPOINTS_UC_PAYMENT',   'userpoints_ubercart_payment');
-define('USERPOINTS_DISPLAY',	    'userpoints_display');
-define('USERPOINTS_PAY_MODERATE',	'userpoints_pay_moderate');
-define('USERPOINTS_PAY_CATEGORY',	'userpoints_pay_category');
+define('USERPOINTS_DISPLAY',      'userpoints_display');
+define('USERPOINTS_PAY_MODERATE', 'userpoints_pay_moderate');
+define('USERPOINTS_PAY_DISPLAY',  'userpoints_pay_display');
+define('USERPOINTS_PAY_CATEGORY', 'userpoints_pay_category');
 
 function uc_userpoints_payment_help($path, $arg) {
   switch ($path) {
@@ -20,7 +21,7 @@ function uc_userpoints_payment_help($pat
  * Implementation of hook_userpoints api().
  */
 
-function uc_userpoints_payment_userpoints($op, $points = 0, $uid = 0, $event = '') {
+function uc_userpoints_payment_userpoints($op, $data = NULL) {
   switch($op) {
     case 'setting':
       if (module_exists('uc_cart') && module_exists('uc_payment')) {
@@ -33,16 +34,17 @@ function uc_userpoints_payment_userpoint
         );
         $form[$group][USERPOINTS_UC_PAYMENT] = array(
           '#type' => 'textfield',
-          '#title' => t('!Points used in payment (for every dollar)', userpoints_translation()),
+          '#title' => t('!Points per currency unit', userpoints_translation()),
           '#default_value' => variable_get(USERPOINTS_UC_PAYMENT, 1),
+          '#description' => t('For example, 1 is $1/point, or 100 is $0.01 per point.', userpoints_translation()),
           '#size' => 5,
           '#maxlength' => 5,
         );
         $form[$group][USERPOINTS_DISPLAY] = array(
           '#type' => 'select',
-          '#title' => t('Display Dollars, !points, or both.', userpoints_translation()),
+          '#title' => t('Display currency, !points, or both', userpoints_translation()),
           '#default_value' =>  variable_get(USERPOINTS_DISPLAY,2),
-          '#options' => array(1 => t('Dollars'), 2 => t('Both'), 3 => t('Points')),
+          '#options' => array(1 => t('Currency'), 3 => t('Points'), 2 => t('Both')),
         );
         $form[$group][USERPOINTS_PAY_MODERATE] = array(
           '#type' => 'radios',
@@ -50,16 +52,58 @@ function uc_userpoints_payment_userpoint
           '#default_value' => variable_get(USERPOINTS_PAY_MODERATE, 0),
 					'#options' => array(t('No'), t('Yes')),
         );
-		$form[$group][USERPOINTS_PAY_CATEGORY] = array(
-		  '#type' => 'select',
-		  '#title' => t('Payment Category'),
-		  '#default_value' => variable_get(USERPOINTS_PAY_CATEGORY, NULL),
-		  '#options' => userpoints_get_categories(),
-		  '#description' => t('Category to assign awarded !points.', userpoints_translation()),
-		);
+        $form[$group][USERPOINTS_PAY_DISPLAY] = array(
+          '#type' => 'radios',
+          '#title' => t('Display !points details during checkout', userpoints_translation()),
+          '#default_value' => variable_get(USERPOINTS_PAY_DISPLAY, 1),
+          '#options' => array(t('No'), t('Yes')),
+        );
+        $form[$group][USERPOINTS_PAY_CATEGORY] = array(
+          '#type' => 'select',
+          '#title' => t('Payment Category'),
+          '#default_value' => variable_get(USERPOINTS_PAY_CATEGORY, NULL),
+          '#options' => userpoints_get_categories(),
+          '#description' => t('Select which category of !points to use when payment is made.', userpoints_translation()),
+        );
+
+        $form[$group]['renaming'] = array(
+          '#type' => 'fieldset',
+          '#collapsible' => TRUE,
+          '#collapsed' => TRUE,
+          '#title' => t('Payment method branding'),
+        );
+        $form[$group]['renaming'][UC_USERPOINTS_TRANS_UCPOINTS] = array(
+          '#type' => 'textfield',
+          '#title' => t('Name to use during checkout for the upper case version of !Points', userpoints_translation()),
+          '#description' => t('For example, "Points" or "Store credit".'),
+          '#default_value' => variable_get(UC_USERPOINTS_TRANS_UCPOINTS, 'Points'),
+          '#size' => 20,
+          '#maxlength' => 20,
+        );
+        $form[$group]['renaming'][UC_USERPOINTS_TRANS_LCPOINTS] = array(
+          '#type' => 'textfield',
+          '#title' => t('Name to use during checkout for the lower case version of !points', userpoints_translation()),
+          '#default_value' => variable_get(UC_USERPOINTS_TRANS_LCPOINTS, 'points'),
+          '#size' => 20,
+          '#maxlength' => 20,
+        );
       }
       return $form;
       break;
+
+    case 'entity_type':
+      if ($data->entity_type == 'uc_order') {
+        $order = uc_order_load($data->entity_id);
+        if ($order) {
+          if (user_access('administer orders')) {
+            return l($data->operation, 'admin/store/orders/'. $order->order_id);
+          }
+          else {
+            return l($data->operation, 'user/'. $order->uid. '/orders/'. $order->order_id);
+          }
+        }
+      }
+      break;
   }
 }
 
@@ -81,8 +125,7 @@ function uc_userpoints_payment_nodeapi(&
 			  }
 			}
 	
-			$pointExchange = intval(variable_get(USERPOINTS_UC_PAYMENT, 1));
-			$pointCost = intval(round($node->sell_price) * $pointExchange);
+      $pointCost = ceil($node->sell_price * variable_get(USERPOINTS_UC_PAYMENT, 1));
 			switch($dispStyle) {
 				case 1: 
 					//do nothing since we only want dollars
@@ -109,23 +152,28 @@ function uc_userpoints_payment_nodeapi(&
  
 function uc_userpoints_payment_payment_method() {
   global $user;
-  $curUserId = $user->uid;
 
-  //how many points does this user have?
-  $curUserPoints = userpoints_get_current_points($uid = $curUserId, $tid = variable_get(USERPOINTS_PAY_CATEGORY, NULL));
-  $maxdisc = $curUserPoints / intval(variable_get(USERPOINTS_UC_PAYMENT, 1));
+  $amount = userpoints_get_current_points($user->uid, variable_get(USERPOINTS_PAY_CATEGORY, NULL));
+  if ($amount > 0 || request_uri() != '/cart/checkout') {
+    $title = t('!Points', uc_userpoints_translation());
+    if (variable_get(USERPOINTS_PAY_DISPLAY, 1)) {
+      // Display point amounts
+      if (variable_get(USERPOINTS_DISPLAY, 2) == 1) {
+        // Display currency instead of points
+        $amount = uc_currency_format($amount / variable_get(USERPOINTS_UC_PAYMENT, 1));
+      }
+      $title .= ' '. t('(total available: !amount)', array('!amount' => $amount));
+    }
 
-  // make sure they have points in the first place
-  if ($curUserPoints > 0 || request_uri() != '/cart/checkout') {
     $methods[]	= array(
       'id'	=> 'points',
-      'name'	=> t('!Points', userpoints_translation()),
-      'title'	=> t('!Points (Total available via !points: ' . uc_currency_format($maxdisc) . ')', userpoints_translation()),
-      'desc'	=> t('Pay by !points.', userpoints_translation()),
+      'name'	=> t('!Points', uc_userpoints_translation()),
+      'title' => $title,
+      'desc'	=> t('Pay by !points.', uc_userpoints_translation()),
       'callback'=> 'uc_payment_method_points',
       'weight'	=> 4,
       'checkout'=> TRUE,
-      'backend'	=> TRUE,
+      'no_gateway' => TRUE,
     );
 
     return $methods;
@@ -165,16 +213,26 @@ function uc_userpoints_payment_order($op
 	  }
 }
 
+/**
+ * Override userpoints_translation()
+ */
+function uc_userpoints_translation() {
+  static $trans;
+
+  if (!isset($trans)) {
+    $trans = array(
+      '!Points' => variable_get(UC_USERPOINTS_TRANS_UCPOINTS, 'Points'),
+      '!points' => variable_get(UC_USERPOINTS_TRANS_LCPOINTS, 'points'),
+    );
+  }
+  return $trans;
+}
+
 /*******************************************************************************
  * Callback Functions, Forms, and Tables
  ******************************************************************************/
 
 function uc_payment_method_points($op, &$arg1) {
-  global $user;
-  $curUserId	 = $user->uid;
-  $curUserPoints = intval(userpoints_get_current_points($uid = $curUserId, $tid = variable_get(USERPOINTS_PAY_CATEGORY, NULL)));
-  $pointExchange = intval(variable_get(USERPOINTS_UC_PAYMENT, 1));
-
   switch ($op) {
 		case 'cart-details':
 			/**
@@ -183,22 +241,23 @@ function uc_payment_method_points($op, &
 			return;
 			break;
 		case 'cart-process':
-			//this would be verification that should be covered else where if possible
-			// check thier points and make sure they have enough to pay for this purchase
-			//order information
-			$order_id 	  = $arg1->order_id;
-			
-			$items = uc_cart_get_contents();
-			foreach ($items as $cartitem) {
-			  $cartbalance += $cartitem->price*$cartitem->qty;
-			}
-			$shipresult		= db_query("select amount from {uc_order_line_items} where `type`='shipping'and  order_id = %d", $order_id);
-			$shipitem 	  = db_fetch_array($shipresult);
-			$orderTotal   = $shipitem['amount']+$cartbalance;
-			$pointsNeeded = intval((round($orderTotal) * $pointExchange));
+      $user_points = userpoints_get_current_points($arg1->uid, variable_get(USERPOINTS_PAY_CATEGORY, NULL));
+      $order_points = ceil($arg1->order_total * variable_get(USERPOINTS_UC_PAYMENT, 1));
 
-			if (($curUserPoints < $pointsNeeded && $curUserId != 0)) {
-				drupal_set_message('You do not have enough points to complete this purchase.  You have ' . $curUserPoints . ' but you need ' . $pointsNeeded . '.  Please select another payment method.', 'error');
+			if ($user_points < $order_points) {
+        if (variable_get(USERPOINTS_PAY_DISPLAY, 1)) {
+          // Display point amounts
+          if (variable_get(USERPOINTS_DISPLAY, 2) == 1) {
+            // Display currency instead of points
+            $user_points = uc_currency_format($user_points / variable_get(USERPOINTS_UC_PAYMENT, 1));
+            $order_points = uc_currency_format($arg1->order_total);
+          }
+          drupal_set_message(t('You do not have enough !points to complete this purchase. You have @user_points, but you need @order_points. Please select another payment method.', array_merge(uc_userpoints_translation(), array('@user_points' => $user_points, '@order_points' => $order_points))), 'error');
+        }
+        else {
+          // Hide point amounts
+          drupal_set_message(t('You do not have enough !points to complete this purchase. Please select another payment method.', array_merge(uc_userpoints_translation(), array('@user_points' => $user_points, '@order_points' => $order_points))), 'error');
+        }
 				return FALSE;
 				break;
 			}
@@ -220,11 +279,9 @@ function uc_payment_method_points($op, &
 
 function uc_userpoints_payment_payment($order) {
   global $user;
-  $curUserId	= $user->uid;
-  $pointExch	= intval(variable_get(USERPOINTS_UC_PAYMENT, 1));
-  $orderTotal	= $order->order_total;
-  $points		= intval(((round($orderTotal) * $pointExch) * (-1)));
-  $description = t('User made a purchase and paid with !points (Ubercart Order #!order_id)', array_merge(userpoints_translation(), array('!order_id' => $order->order_id)));
+
+  $points = ceil($order->order_total * variable_get(USERPOINTS_UC_PAYMENT, 1));
+  $description = t('Order #!order_id paid with !points', array_merge(uc_userpoints_translation(), array('!order_id' => $order->order_id)));
 
   if ($order === FALSE || uc_order_status_data($order->order_status, 'state') != 'in_checkout') {
 		print t('An error has occurred during payment.  Please contact us to ensure your order has submitted.');
@@ -234,18 +291,18 @@ function uc_userpoints_payment_payment($
   if ($order->payment_method == 'points') {  
 		$params = array (
 			'tid' => variable_get(USERPOINTS_PAY_CATEGORY, NULL),
-			'uid' => $curUserId,
-			'points' => $points,
-			'operation' => 'delete',
+			'uid' => $order->uid,
+			'points' => -$points,
+			'operation' => 'purchase',
 			'description' => $description,
 			'entity_id' => $order->order_id,
-			'entity_type' => 'Ubercart Payment',
+			'entity_type' => 'uc_order',
 			'moderate' => variable_get(USERPOINTS_PAY_MODERATE, 0),
+      'display' => variable_get(USERPOINTS_PAY_DISPLAY, 1),
 		);
 		userpoints_userpointsapi($params);
-		uc_payment_enter($order->order_id, 'points', $orderTotal, $curUserId, NULL, $description);
-		db_query("insert into {uc_up_payment_log} (uid, oid, points) values(%d, %d, %d)",$curUserId, $order->order_id, $points);
-		uc_order_update_status($order->order_id, 'payment_received');
+		uc_payment_enter($order->order_id, 'points', $order->order_total, $user->uid, NULL, $description);
+		db_query("insert into {uc_up_payment_log} (uid, oid, points) values(%d, %d, %d)", $order->uid, $order->order_id, $points);
   }
 }
 
@@ -268,11 +325,12 @@ function uc_userpoints_payment_refund($o
 			'tid' => variable_get('USERPOINTS_PAY_CATEGORY', NULL),
 			'uid' => $curUserId,
 			'points' => $points,
-			'operation' => 'insert',
+			'operation' => 'refund',
 			'description' => $description,
 			'entity_id' => $oid,
-			'entity_type' => 'Ubercart Refund',
+			'entity_type' => 'uc_order',
 			'moderate' => variable_get(USERPOINTS_PAY_MODERATE, 0),
+      'display' => variable_get(USERPOINTS_PAY_DISPLAY, 1),
 		);
 		userpoints_userpointsapi($params);
   }
