Index: uc_userpoints_discount.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/userpoints_ubercart/uc_userpoints_discount.module,v
retrieving revision 1.4.2.1
diff -u -p -r1.4.2.1 uc_userpoints_discount.module
--- uc_userpoints_discount.module	14 May 2009 02:47:13 -0000	1.4.2.1
+++ uc_userpoints_discount.module	14 May 2009 04:06:46 -0000
@@ -6,6 +6,7 @@ define('USERPOINTS_UC_DISC',   'userpoin
 define('USERPOINTS_DISCOUNT',     'userpoints_discount');
 define('UC_USERPOINTS_DISCOUNT_MODERATE',      'uc_userpoints_discount_moderate');
 define('UC_USERPOINTS_DISCOUNT_MAXP',      'uc_userpoints_discount_maxp');
+define('USERPOINTS_DISCOUNT_CATEGORY',	'userpoints_discount_category');
 
 function uc_userpoints_discount_help($path, $arg) {
   switch ($path) {
@@ -47,6 +48,13 @@ function uc_userpoints_discount_userpoin
           '#default_value' =>  variable_get(USERPOINTS_DISCOUNT,1),
           '#options' => array(t('No'), t('Yes')),
         );
+		$form[$group][USERPOINTS_DISCOUNT_CATEGORY] = array(
+		  '#type' => 'select',
+		  '#title' => t('Discount Category'),
+		  '#description' => t('Category to assign awarded !points.', userpoints_translation()),
+		  '#default_value' => variable_get(USERPOINTS_DISCOUNT_CATEGORY, NULL),
+		  '#options' => userpoints_get_categories(),
+		);
         $form[$group][UC_USERPOINTS_DISCOUNT_MODERATE] = array(
           '#type' => 'radios',
           '#title' => t('Moderate userpoints transaction'),
@@ -70,7 +78,7 @@ function uc_userpoints_discount_checkout
     
   if ($discMethod != 0 && ($curUserPoints > 0 || request_uri() != '/cart/checkout')) {
     $panes[] = array(
-      'id' => 'uc_up_discounts',
+      'id' => 'up_uc_discounts',
       'title' => t('!Points Discounts', userpoints_translation()),
       'desc' => t('Show !Points discounts for the order', userpoints_translation()),
       'callback' => 'uc_checkout_pane_uc_userpoints_discount',
@@ -192,7 +200,7 @@ function uc_userpoints_discount_order($o
 						$points = intval(($ptamt) * $multiplier);
 						$points = -$points;
 						$params = array (
-						'tid' => 0,
+						'tid' => variable_get(USERPOINTS_DISCOUNT_CATEGORY, 0),
 						'uid' => $curUserId,
 						'points' => $points,
 						'operation' => 'delete',
@@ -203,7 +211,6 @@ function uc_userpoints_discount_order($o
 						);
 						userpoints_userpointsapi($params);
 					db_query('INSERT INTO {uc_updiscounts} (uid, oid, ptamt, points) VALUES (%d, %d, \'%f\', %d)', $curUserId, $arg1->order_id, $ptamt, $points);
-					// db_query("insert into {uc_uplog} (uid, oid, points, referrer, discount) values(%d, %d, %d, 0, 1)",$curUserId, $order->order_id, $points);
 					}
 
 				}
@@ -222,14 +229,13 @@ function uc_userpoints_discount_order($o
 				$curUserId	= $user->uid;
 				$oid			= $order->order_id;
 				$result 		= db_query("select updlog_id, points from {uc_updiscounts} where oid = %d and uid = %d and referrer = 0 and discount =1", $oid, $curUserId);
-				
-				if ($result != FALSE) {
-					$pointinfo = db_fetch_object($result);
+
+				if ($result != FALSE && $pointinfo = db_fetch_object($result) != FALSE) {
 					$points = intval(($pointinfo->points) * (-1));
 					$description = 'User Discount (Ubercart Order ' . $oid . ') that has been deleted.';
 					
 					$params = array (
-						'tid' => 0,
+						'tid' => variable_get(USERPOINTS_DISCOUNT_CATEGORY, 0),
 						'uid' => $curUserId,
 						'points' => $points,
 						'operation' => 'delete',
Index: uc_userpoints_payment.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/userpoints_ubercart/uc_userpoints_payment.module,v
retrieving revision 1.6.2.1
diff -u -p -r1.6.2.1 uc_userpoints_payment.module
--- uc_userpoints_payment.module	14 May 2009 02:47:13 -0000	1.6.2.1
+++ uc_userpoints_payment.module	14 May 2009 04:06:46 -0000
@@ -5,6 +5,7 @@
 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');
 
 function uc_userpoints_payment_help($path, $arg) {
   switch ($path) {
@@ -49,6 +50,13 @@ 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()),
+		);
       }
       return $form;
       break;
@@ -65,9 +73,16 @@ function uc_userpoints_payment_nodeapi(&
 		if ($node->type=='product') {
 			$dispStyle = variable_get(USERPOINTS_DISPLAY,2);
 			//disp 1=dollars, 2=both, 3=points
+
+			if (module_exists('uc_userpoints_product')) {
+			  $points = db_result(db_query("SELECT points FROM {uc_userpoints_products} WHERE nid = %d", $node->nid));
+			  if ($points > 0) {
+			    $dispStyle = 1; // you can't buy a point product with points
+			  }
+			}
 	
 			$pointExchange = intval(variable_get(USERPOINTS_UC_PAYMENT, 1));
-			$pointCost = intval($node->sell_price * $pointExchange);
+			$pointCost = intval(round($node->sell_price) * $pointExchange);
 			switch($dispStyle) {
 				case 1: 
 					//do nothing since we only want dollars
@@ -75,11 +90,11 @@ function uc_userpoints_payment_nodeapi(&
 					//$node->content['display_price']['#value'] = '$$';
 					break;
 				case 2: 
-					$node->content['sell_price']['#value'] .= t('!Points: ' . $pointCost, userpoints_translation());
+					$node->content['sell_price']['#value'] .= '<div class="sell_price">' . t('!Points: ' . $pointCost, userpoints_translation()) . '</div>';
 					$node->content['display_price']['#value'] .= '<div class="display_price">' . t('!Points: ' . $pointCost, userpoints_translation()) . '</div>';
 					break;
 				case 3: 
-					$node->content['sell_price']['#value'] = t('!Points: ' . $pointCost, userpoints_translation());
+					$node->content['sell_price']['#value'] = '<div class="sell_price">' . t('!Points: ' . $pointCost, userpoints_translation()) . '</div>';
 					$node->content['display_price']['#value'] = '<div class="display_price">' . t('!Points: ' . $pointCost, userpoints_translation()) . '</div>';
 					break;
 			}
@@ -97,7 +112,7 @@ function uc_userpoints_payment_payment_m
   $curUserId = $user->uid;
 
   //how many points does this user have?
-  $curUserPoints = userpoints_get_current_points($uid = $curUserId, $tid = NULL);
+  $curUserPoints = userpoints_get_current_points($uid = $curUserId, $tid = variable_get(USERPOINTS_PAY_CATEGORY, NULL));
   $maxdisc = $curUserPoints / intval(variable_get(USERPOINTS_UC_PAYMENT, 1));
 
   // make sure they have points in the first place
@@ -157,7 +172,7 @@ function uc_userpoints_payment_order($op
 function uc_payment_method_points($op, &$arg1) {
   global $user;
   $curUserId	 = $user->uid;
-  $curUserPoints = intval(userpoints_get_current_points($uid = $curUserId, $tid = NULL));
+  $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) {
@@ -180,7 +195,7 @@ function uc_payment_method_points($op, &
 			$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(($orderTotal * $pointExchange));
+			$pointsNeeded = intval((round($orderTotal) * $pointExchange));
 
 			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');
@@ -208,7 +223,7 @@ function uc_userpoints_payment_payment($
   $curUserId	= $user->uid;
   $pointExch	= intval(variable_get(USERPOINTS_UC_PAYMENT, 1));
   $orderTotal	= $order->order_total;
-  $points		= intval((($orderTotal * $pointExch) * (-1)));
+  $points		= intval(((round($orderTotal) * $pointExch) * (-1)));
   $description	= 'User made a purchase and paid with points (Ubercart Order ' . $order->order_id . ')';
 
   if ($order === FALSE || uc_order_status_data($order->order_status, 'state') != 'in_checkout') {
@@ -218,7 +233,7 @@ function uc_userpoints_payment_payment($
 
   if ($order->payment_method == 'points') {  
 		$params = array (
-			'tid' => 0,
+			'tid' => variable_get(USERPOINTS_PAY_CATEGORY, NULL),
 			'uid' => $curUserId,
 			'points' => $points,
 			'operation' => 'delete',
@@ -250,7 +265,7 @@ function uc_userpoints_payment_refund($o
 		db_query("delete from {uc_up_payment_log} where uplog_id = %d", $pointinfo->uplog_id);
 		
 		$params = array (
-			'tid' => 0,
+			'tid' => variable_get('USERPOINTS_PAY_CATEGORY', NULL),
 			'uid' => $curUserId,
 			'points' => $points,
 			'operation' => 'insert',
Index: uc_userpoints_product.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/userpoints_ubercart/uc_userpoints_product.module,v
retrieving revision 1.5.2.1
diff -u -p -r1.5.2.1 uc_userpoints_product.module
--- uc_userpoints_product.module	14 May 2009 02:47:13 -0000	1.5.2.1
+++ uc_userpoints_product.module	14 May 2009 04:06:46 -0000
@@ -3,6 +3,51 @@
 /**
 * Implementation of hook_product_feature().
 */
+
+define('USERPOINTS_PRODUCT_MODERATE',	'userpoints_product_moderate');
+define('USERPOINTS_PRODUCT_CATEGORY',	'userpoints_product_category');
+
+function uc_userpoints_product_help($path, $arg) {
+    switch ($path) {
+      case 'admin/modules#description':
+        $output = t('<strong>Ubercart Userpoints Product:</strong> Interfaces userpoints with Ubercart, so users can purchase points with products.');
+      break;
+    }
+    return $output;
+}
+
+
+function uc_userpoints_product_userpoints($op, $params = array()) {
+	switch($op) {
+	  case 'setting':
+	    if (module_exists('uc_cart') && module_exists('uc_payment')) {
+		  $group = 'uc_userpoints_product';
+		  $form[$group] = array(
+		    '#type' => 'fieldset',
+			'#collapsible' => TRUE,
+			'#collapsed' => TRUE,
+			'#title' => t('Ubercart Product Options'),
+		  );
+		  $form[$group][USERPOINTS_PRODUCT_MODERATE] = array(
+		    '#type' => 'radios',
+			'#title' => t('Moderate userpoints transaction'),
+			'#default_value' => variable_get(USERPOINTS_PRODUCT_MODERATE, 0), 
+			'#options' => array(t('No'), t('Yes')),
+		  );
+		  $form[$group][USERPOINTS_PRODUCT_CATEGORY] = array(
+		    '#type' => 'select',
+			'#title' => t('Product Category'),
+			'#default_value' => variable_get(USERPOINTS_PRODUCT_CATEGORY, NULL),
+			'#options' => userpoints_get_categories(),
+			'#description' => t('Category to assign awarded !points.', userpoints_translation()),
+	      );
+		}
+		return $form;
+		break;
+	}
+}
+
+
 function uc_userpoints_product_product_feature() {
 	$features[] = array(
 		'id' => 'userpoints',
@@ -29,7 +74,7 @@ function uc_userpoints_product_feature_f
 
 	$form['nid'] = array(
 		'#type' => 'value',
-		'#value' => $nid,
+		'#value' => $node->nid,
 	);
 	$form['points'] = array(
 		'#type' => 'textfield',
@@ -42,8 +87,8 @@ function uc_userpoints_product_feature_f
 
 
 function uc_userpoints_product_feature_form_validate($form, &$form_state) {
-  if (!is_numeric($form_state['values']['points'])) {
-    form_set_error('points','You must enter a numeric value for this field.');
+  if (!$form_state['values']['points'] || !is_numeric($form_state['values']['points']) || $form_state['values']['points'] < 1) {
+    form_set_error('points','You must enter a numeric value, greater than zero for points');
   }
 }
 
@@ -55,20 +100,20 @@ function uc_userpoints_product_feature_f
 	$data = array(
 		'pfid' => $form_state['values']['pfid'],
 		'nid' => $form_state['values']['nid'],
+		'points' => $form_state['values']['points'],
 		'fid' => 'userpoints',
 		'description' => 'Product is now worth '.$form_state['values']['points'].' '.variable_get(USERPOINTS_TRANS_LCPOINTS, 'points'),
 	);
 	
 	$ret = uc_product_feature_save($data);
-	//Note: Ubercart Developers are the worst. This function should really pass $data as a reference so I can reference the newly created pfid. UGH! See: http://drupal.org/node/434676
 	if (empty($data['pfid'])) {
 		$pf = db_fetch_object(db_query("select pfid from {uc_userpoints_products} where nid = %d and fid = %d",$data['nid'],$data['fid']));
 		$data['pfid'] = $pf->pfid;
 
-		db_query("INSERT INTO {uc_userpoints_products} (pfid,nid,points) VALUES (%d,%d,%d)",$data['pfid'],$data['nid'],$form_state['values']['points']);
+		db_query("INSERT INTO {uc_userpoints_products} (pfid,nid,points) VALUES (%d,%d,%d)",$data['pfid'],$data['nid'],$data['points']);
 	}
 	else {
-		db_query("UPDATE {uc_userpoints_products} SET points = %d WHERE nid = %d AND pfid = %d",$form_state['values']['points'],$data['nid'],$data['pfid']);
+		db_query("UPDATE {uc_userpoints_products} SET points = %d WHERE nid = %d AND pfid = %d",$data['points'],$data['nid'],$data['pfid']);
 	}
 
 	$form_state['redirect'] = $ret;
@@ -101,22 +146,24 @@ function uc_userpoints_product_order($op
 		case 'update':
 		// We might want to add the "Order Status" to the Userpoints Ubercart options...
 			if (($status == 'completed' && $order->uid > 0) && ($order_user = user_load(array('uid' => $order->uid))) !== FALSE) {
+				$totalPoints = 0;
 				foreach ($order->products as $product) {
-					$points = db_result(db_query("SELECT points FROM {uc_userpoints_products} WHERE `nid`=%d", $product->nid));
+					$points = db_result(db_query("SELECT sum(points) FROM {uc_userpoints_products} WHERE nid = %d", $product->nid));
 					if ($points > 0) {
 						//drupal_set_message($product->title.' has '.$points.' '.variable_get(USERPOINTS_TRANS_LCPOINTS, 'points').' attached');
-						$totalPoints .= ($product->qty * $points); // Make sure we account for multiple qty of the Points product
+						$totalPoints += ($product->qty * $points); // Make sure we account for multiple qty of the Points product
 					}
 				}
 
 			$params = array (
-				'tid' => 0,
+				'tid' => variable_get(USERPOINTS_PRODUCT_CATEGORY, 0),
 				'uid' => $order->uid,
 				'points' => $totalPoints,
 				'operation' => 'add',
 				'description' => t('A user purchased a product containing points for a total of ' .$totalPoints.' '. variable_get(USERPOINTS_TRANS_LCPOINTS, 'points')),
 				'entity_id' => $order->oid,
-				'entity_type' => 'Ubercart Transaction'
+				'entity_type' => 'Ubercart Transaction',
+				'moderate' => variable_get(USERPOINTS_PRODUCT_MODERATE, 0),
 			);
 
 		 userpoints_userpointsapi($params);
@@ -130,7 +177,7 @@ function uc_userpoints_product_order($op
 * Check and make sure the user isn't trying to buy points with points.
 *  @todo: Make this configurable?
 */
-function uc_userpoints_product_form_alter(&$form, $form_state, $form_id) {
+function uc_userpoints_product_form_alter(&$form, &$form_state, $form_id) {
   // We may need to alter the checkout form to remove invalid payment methods.
   if ($form_id == 'uc_cart_checkout_form' && isset($form['panes']['payment'])) {
            
@@ -139,10 +186,10 @@ function uc_userpoints_product_form_alte
     // Make no changes if no products containing Userpoints are found.
     if (uc_userpoints_find_points_products($order) == array()) {
       return;
-    } else {
-            unset($form['panes']['payment']['payment_method']['#options']['points']);
-            drupal_set_message(t('You cannot purchase !Points with !Points, so that payment option has been removed.', userpoints_translation()),'error');
-        }
+    } else if (isset($form['panes']['payment']['payment_method']['#options']['points'])) {
+      unset($form['panes']['payment']['payment_method']['#options']['points']);
+      drupal_set_message(t('You cannot purchase !Points with !Points, so that payment option has been removed.', userpoints_translation()),'error');
+    }
        
     $count = count($form['panes']['payment']['payment_method']['#options']);
     if ($count == 0) {
@@ -177,7 +224,7 @@ function uc_userpoints_find_points_produ
   }
 
   $pointsproducts = array();
-  $result = db_query("SELECT * FROM {uc_userpoints_products} WHERE nid IN ('". implode("', '", $nids) ."')");
+  $result = db_query("SELECT * FROM {uc_userpoints_products} WHERE nid IN (". implode(", ", $nids) .")");
   while ($pntproduct = db_fetch_object($result)) {
     $pointsproducts[] = $pntproduct;
         //dsm('Matched nid: '.$pntproduct->nid);
