Userpoints Discounts: Interfaces userpoints with Ubercart, so users can use points as a discount.'); break; } return $output; } function uc_userpoints_discount_userpoints($op, $points = 0, $uid = 0, $event = '') { switch($op) { case 'setting': if (module_exists('uc_cart') && module_exists('uc_payment')) { $group = 'uc_userpoints_discount'; $form[$group] = array( '#type' => 'fieldset', '#collapsible' => TRUE, '#collapsed' => TRUE, '#title' => t('Ubercart Discount Options'), ); $form[$group][USERPOINTS_UC_DISC] = array( '#type' => 'textfield', '#title' => t('!Points used in discount (for every dollar)', userpoints_translation()), '#default_value' => variable_get(USERPOINTS_UC_DISC, 1), '#size' => 5, '#maxlength' => 5, ); $form[$group][USERPOINTS_DISCOUNT] = array( '#type' => 'radios', '#title' => t('Enable !Points as a discount method', userpoints_translation()), '#default_value' => variable_get(USERPOINTS_DISCOUNT,1), '#options' => array(t('No'), t('Yes')), ); $form[$group][UC_USERPOINTS_DISCOUNT_MODERATE] = array( '#type' => 'radios', '#title' => t('Moderate userpoints transaction'), '#default_value' => variable_get(UC_USERPOINTS_DISCOUNT_MODERATE, 0), '#options' => array(t('No'), t('Yes')), ); $form[$group][USERPOINTS_DISCOUNT_CATEGORY] = array( '#type' => 'select', '#title' => t('Discount Category'), '#default_value' => variable_get(USERPOINTS_DISCOUNT_CATEGORY, NULL), '#options' => userpoints_get_categories(), '#description' => t('Category to assign awarded !points.', userpoints_translation()), ); } return $form; break; } } /** * Implementation of hook_checkout_pane */ function uc_userpoints_discount_checkout_pane() { global $user; $curUserId = $user->uid; $curUserPoints = intval(userpoints_get_current_points($uid = $curUserId, $tid = NULL)); $discMethod = variable_get(USERPOINTS_DISCOUNT,1); if ($discMethod != 0 && ($curUserPoints > 0 || request_uri() != '/cart/checkout')) { $panes[] = array( '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', 'weight' => 7, ); return $panes; } } function uc_checkout_pane_uc_userpoints_discount($op, &$arg1, $arg2) { // get point discounts to apply to this order global $user; $curUserId = $user->uid; switch ($op) { case 'view': $curUserPoints = intval(userpoints_get_current_points($uid = $curUserId, $tid = NULL)); $maxdisc = round($curUserPoints / intval(variable_get(USERPOINTS_UC_DISC, 1))); $description = t('Enter the amount to discount this order with !Points by (Total available via !Points: ' . uc_currency_format($maxdisc) . ') This field will be ignored if paying with !Points.', userpoints_translation()); $contents['ptamt'] = array( '#type' => 'textfield', '#title' => t('Discount Amount'), '#description' => t(''), '#default_value' => $arg1->ptdiscount['amt'], ); return array('description' => $description, 'contents' => $contents); break; case 'process': $arg1->ptdiscount['amt'] = $arg2['ptamt']; return TRUE; break; } } /** * Implementation of hook_order(). * This is where all the important stuff happens like taking the points... */ function uc_userpoints_discount_order($op, &$arg1, $arg2) { $order = $arg1; $paymethod = strtolower($order->payment_method); switch ($op) { case 'total': /** * If there is a discount apply it to the order total */ if (variable_get(USERPOINTS_DISCOUNT,1) != 0 && strtolower($order->payment_method) != 'points') { $result = db_query("SELECT * FROM {uc_updiscounts} WHERE oid = %d", $arg1->order_id); if ($data = db_fetch_object($result)) { return -$data->ptamt; } } break; case 'save': if (variable_get(USERPOINTS_DISCOUNT,1) != 0 && strtolower($order->payment_method) != 'points') { global $user; $curUserId = $user->uid; $ptamt = $arg1->ptdiscount['amt']; $multiplier = intval(variable_get(USERPOINTS_UC_DISC, 0)); $points = -intval(round($ptamt) * $multiplier); db_query("DELETE FROM {uc_order_line_items} WHERE order_id=%d AND type='ptdiscount'", $arg1->order_id); if ((!empty($ptamt) || $ptamt != '') && $arg1->payment_method != 'points') { db_query('INSERT INTO {uc_updiscounts} (uid, oid, ptamt, points) VALUES (%d, %d, \'%f\', %d)', $curUserId, $arg1->order_id, $ptamt, $points); uc_order_line_item_add($arg1->order_id, 'ptdiscount', t('Discount order using !Points',userpoints_translation()), -$ptamt, 1); } } break; case 'submit': // fires when the order is submitted and adds/subtracts thier points if ($paymethod != 'points') { if (variable_get(USERPOINTS_DISCOUNT,1) != 0) { global $user; $curUserId = $user->uid; $ptamt = $arg1->ptdiscount['amt']; $multiplier = intval(variable_get(USERPOINTS_UC_DISC, 0)); // check thier points and make sure they have enough to pay for this purchase $points = intval(round($ptamt) * $multiplier); $curUserPoints = intval(userpoints_get_current_points($uid = $curUserId, $tid = NULL)); if (($curUserPoints < $pointsNeeded && $curUserId != 0)) { return array(array('pass' => FALSE, 'message' => t('You do not have enough points for the discount ammount you entered. You have ' . $curUserPoints . ' but you need ' . $pointsNeeded . '.'))); break; } else { // Payment completed if ($curUserId != 0) { // User id from the transaction $points = -$points; $params = array ( 'tid' => variable_get(USERPOINTS_DISCOUNT_CATEGORY, 0), 'uid' => $curUserId, 'points' => $points, 'operation' => 'delete', 'description' => 'User Discount, taking poings (Ubercart Order ID ' . $order->order_id . ')', 'entity_id' => $order->order_id, 'entity_type' => 'Ubercart Transaction', 'moderate' => variable_get(UC_USERPOINTS_DISCOUNT_MODERATE, 0), ); userpoints_userpointsapi($params); db_query("insert into {uc_uplog} (uid, oid, points, referrer, discount) values(%d, %d, %d, 0, 1)",$curUserId, $order->order_id, $points); } return TRUE; break; } } } break; case 'load': $arg1->ptdiscount['amt'] = ''; $result = db_query("SELECT * FROM {uc_updiscounts} WHERE oid = %d", $arg1->order_id); if ($data = db_fetch_object($result)) { $arg1->ptdiscount['amt'] = $data->ptamt; } break; case 'delete': if (variable_get(USERPOINTS_DISCOUNT,1) != 0 && strtolower($order->payment_method) != 'points') { global $user; $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); $points = intval(($pointinfo->points) * (-1)); $description = 'User Discount (Ubercart Order ' . $oid . ') that has been deleted.'; $params = array ( 'tid' => variable_get(USERPOINTS_DISCOUNT_CATEGORY, 0), 'uid' => $curUserId, 'points' => $points, 'operation' => 'delete', 'description' => $description, 'entity_id' => $oid, 'entity_type' => 'Ubercart Transaction', 'moderate' => variable_get(UC_USERPOINTS_DISCOUNT_MODERATE, 0), ); userpoints_userpointsapi($params); db_query("delete from {uc_updiscounts} where uplog_id = %d", $pointinfo->uplog_id); db_query("DELETE FROM {uc_order_line_items} WHERE order_id=%d AND type='ptdiscount'", $arg1->order_id); } } break; case 'can_delete'; return FALSE; break; } }