Index: uc_multiprice.admin.inc
===================================================================
--- uc_multiprice.admin.inc	(revision 29)
+++ uc_multiprice.admin.inc	(working copy)
@@ -2,15 +2,22 @@
 // $Id: uc_multiprice.admin.inc,v 1.2.2.2 2010/10/22 09:39:38 docc Exp $
 
 /**
- * @file Provides Admin Settings 
-*/
+ * @file
+ * Provides Multiprice Admin options
+ *
+ * Module developed by Docc | Optixdesigns.nl
+ * @author Docc 
+ * Module extended by Bartezz | Intrige.nl
+ * @author Bartezz <bartezz@gmail.com>
+ *
+ */
 
 function uc_multiprice_admin_default($form) {
   $form = array();
 
-  $countries = variable_get('uc_multiprice_default', array());
+  $multiprices = variable_get('uc_multiprice_default', array());
 
-  $form += uc_multiprice_form($countries, TRUE);
+  $form += uc_multiprice_form($multiprices, TRUE);
   $form['buttons']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
   $form['#pre_render'][] = 'uc_multiprice_add_js';
   
@@ -18,16 +25,131 @@
 }
 
 function uc_multiprice_admin_default_submit($form, &$form_state) {
-  foreach($form_state['values']['multiprice']['countries'] as $key => $country) {
-    if(!$country['delete']) {
-      $countries[$key] = new stdClass();
-      $countries[$key]->country_name  = $country['country'];
-      $countries[$key]->country_id     = $country['country_id'];
-      $countries[$key]->list_price     = $country['list_price'];
-      $countries[$key]->cost           = $country['cost'];
-      $countries[$key]->sell_price     = $country['sell_price'];
-      $countries[$key]->data = $country['dynamic'];
+  foreach ((array)$form_state['values']['multiprice']['multiprices']['country'] as $country_id => $country) {
+    if (!$country['delete']) {
+      $multiprice['country'][$country_id] = new stdClass();
+      $multiprice['country'][$country_id]->country_name  = $country['title'];
+      $multiprice['country'][$country_id]->country_id     = $country['country_id'];
+      $multiprice['country'][$country_id]->list_price     = $country['list_price'];
+      $multiprice['country'][$country_id]->cost           = $country['cost'];
+      $multiprice['country'][$country_id]->sell_price     = $country['sell_price'];
+      $multiprice['country'][$country_id]->data = $country['dynamic'];
     }
   }
-  variable_set('uc_multiprice_default', $countries);
+  foreach ((array)$form_state['values']['multiprice']['multiprices']['role'] as $role_id => $role) {
+    if (!$role['delete']) {
+      $multiprice['role'][$role_id] = new stdClass();
+      $multiprice['role'][$role_id]->role_name  = $role['title'];
+      $multiprice['role'][$role_id]->role_id     = $role['role_id'];
+      $multiprice['role'][$role_id]->list_price     = $role['list_price'];
+      $multiprice['role'][$role_id]->cost           = $role['cost'];
+      $multiprice['role'][$role_id]->sell_price     = $role['sell_price'];
+      $multiprice['role'][$role_id]->data = $role['dynamic'];
+    }
+  }
+  variable_set('uc_multiprice_default', $multiprice);
 }
+
+/**
+ *  Function to generate the form
+ */
+function uc_multiprice_admin_roles() {
+  if (!user_access('administer multiprice roles', $account)) {
+    return FALSE;
+  }
+  
+  $form = array();
+  
+  $form['uc_multiprice_admin_roles'] = array(
+    '#type'       => 'fieldset',
+    '#title'       => t('Multiprice enabled roles'),
+    '#collapsible'    => TRUE, 
+    '#collapsed'    => FALSE,
+    '#description'     => t('Please select the roles for which you want to set multiprices. The roles are shown ordered by weight.')
+  );
+  if (module_exists('role_weights')) {
+    $user_roles = user_roles();
+    if (variable_get('role_weights_reorder_forms', FALSE)) {
+      uksort($user_roles, '_role_weights_rid_compare');
+    }
+      $form['uc_multiprice_admin_roles']['uc_multiprice_roles'] = array(
+        '#type'  => 'checkboxes',
+        '#title'       => t('Roles'),
+        '#default_value' => variable_get('uc_multiprice_roles', array()),
+        '#options' => $user_roles,
+      );
+  }
+  else {
+    $form['uc_multiprice_admin_roles']['#description'] = t('Please install, enable and configure the <a href="@roleweight" target="_blank">Role Weight module</a>.', array('@roleweight' => url('http://drupal.org/project/role_weights')));
+  }
+  $form['uc_multiprice_admin_roles_fallback'] = array(
+    '#type'       => 'fieldset',
+    '#title'       => t('Multiprice roles fallback'),
+    '#collapsible'    => TRUE, 
+    '#collapsed'    => FALSE,
+    '#description'     => t('Please configure the fallback procedure for role based pricing. If there is no role based price available should the module fall back to country based pricing (if available) or to the default product price? Probably best to enable this setting when you have set country prices in different currencies than the store default currency.')
+  );
+     $form['uc_multiprice_admin_roles_fallback']['uc_multiprice_roles_fallback'] = array(
+        '#type'  => 'checkbox',
+        '#title'       => t('Fall back to default product price'),
+        '#default_value' => variable_get('uc_multiprice_roles_fallback', NULL),
+      );
+    
+  return system_settings_form($form);
+}
+
+/**
+ *  Function to generate the form
+ */
+function uc_multiprice_admin_currencies() {
+  if (!user_access('administer multiprice', $account)) {
+    return FALSE;
+  }
+  
+  $form = array();
+  
+  $form['uc_multiprice_admin_currencies'] = array(
+    '#type'       => 'fieldset',
+    '#title'       => t('Disable unpriced product'),
+    '#collapsible'    => TRUE, 
+    '#collapsed'    => FALSE,
+    '#description'     => t('Disable the quantity field and add to cart button for products that do not have a multiprice set when the <a href="@pricehandler">Multiprice Currency Handler</a> is enabled and the users country currency differs from the default currency. This prevents a product falling back to the default amount but using the incorrect currency for it.', array('@pricehandler' => url('admin/store/settings/price-handlers')))
+  );
+      $form['uc_multiprice_admin_currencies']['uc_multiprice_currency_disable_unpriced_product'] = array(
+        '#type'  => 'checkbox',
+        '#title'       => t('Enabled'),
+        '#default_value' => variable_get('uc_multiprice_currency_disable_unpriced_product', NULL),
+      );
+    
+  return system_settings_form($form);
+}
+
+/**
+ * Implementation of hook_help().
+ */
+function uc_multiprice_help($path, $arg) {
+  global $user;
+
+  switch ($path) {
+    case 'admin/store/settings/multiprice':
+      return t('<p>Use this form to set default prices per country or role for new nodes.</p><p>Multiprice defaults will not change prices to the defaults set for a country or role in this form for existing nodes or nodes that are being edited.</p>');
+    case 'admin/store/settings/multiprice/currencies':
+      return t('<p>The <a href="@pricehandler">Multicurrency Currency Handler</a> allows you to use the currency set for a country in the <a href="@countryformats">country formats</a> with the displayed price. When this <a href="@pricehandler">handler</a> is enabled it is expected that pricing has been set for each product for each enabled country.</p>
+               <p><strong>Example</strong><br />
+                Your Ubercart store has USA as the default country. There are currently two products in the store; a laptop and a desktop computer. For the laptop the default sell price amount has been set at 1000, for buyers from India the sell price amount has been set at 35000. With the <a href="@pricehandler">Multicurrency Currency Handler</a> enabled and the
+                correct <a href="@countryformats">country formats</a> set the Ubercart store will show a sell price of $1,000.00 to USA buyers and a price of Rs. 35,000.00 to Indian buyers.</p>
+                <p>For the desktop the default sell price amount has been set to 500, but the admin has omitted the set a price for buyers from India. The Ubercart store will show a sell price of $500.00 to USA buyers, but the <strong>incorrect price</strong> of Rs. 500.00 to Indian buyers (which is less than $10).</p>
+                <p>Ofcourse this will dramatically increase sales to India but the stores profits will plumit. To prevent this the admin should always set a price per product per country if <a href="@pricehandler">Multicurrency Currency Handler</a> is enabled. By enabling the checkbox below the module will disable the quantity field
+                and the add to cart button for products which do not have a multiprice for the buyers country in case the admin omits to set a price per country for a product. Thereby preventing products being sold at the wrong sell price.</p>
+               ', array('@pricehandler' => url('admin/store/settings/price-handlers'), '@countryformats' => url('admin/store/settings/countries/edit/formats')));
+    case 'admin/store/settings/multiprice/roles':
+      return t('<p>Multiprice roles allows you to set different prices on a per role basis. Because a user can be assigned multiple roles and since roles are sorted alphabetically in core you will have to set a role weight via <a href="@roleweight" target="_blank">Role Weights module</a>. The lower the numeric weight the more priority the role will have.</p>
+               <ul>
+                 <lh><strong>Please note:</strong></lh>
+                <li>Role pricing overrides per country pricing</li>
+                <li>Role pricing will not use the <a href="@pricehandler">Multicurrency Currency Handler</a>, prices based on role will use the store default currency</li>
+                <li>When a user has multiple roles the prices set for the role with the lowest weight will be used</li>
+                <li>Make sure you have set different weights for different roles in <a href="@rolewadmin">role weight settings</a></li>
+               </ul>', array('@pricehandler' => url('admin/store/settings/price-handlers'), '@roleweight' => url('http://drupal.org/project/role_weights'), '@rolewadmin' => url('admin/settings/role_weights')));
+  }
+}
Index: uc_multiprice.install
===================================================================
--- uc_multiprice.install	(revision 29)
+++ uc_multiprice.install	(working copy)
@@ -53,6 +53,12 @@
         'not null' => TRUE,
         'size' => 'big',
       ),
+      'role_id' => array(
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0,
+      ),
     ),
   );
 
@@ -121,6 +127,15 @@
  */
 function uc_multiprice_uninstall() {
   drupal_uninstall_schema('uc_multiprice');
+  $variables = array(
+    'uc_multiprice_default',
+    'uc_multiprice_roles',
+    'uc_multiprice_roles_fallback',
+    'uc_multiprice_currency_disable_unpriced_product'
+  );
+  foreach ($variables as $variable) {
+    variable_del($variable);
+  }
 }
 
 function uc_multiprice_update_6001() {
@@ -191,4 +206,15 @@
         'size' => 'big',
       ));
   return $ret;
+}
+
+function uc_multiprice_update_6003() {
+  $ret = array();
+  db_add_field($ret, 'uc_multiprice', 'role_id', array(
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0,
+      ));
+  return $ret;
 }
\ No newline at end of file
Index: uc_multiprice.module
===================================================================
--- uc_multiprice.module	(revision 29)
+++ uc_multiprice.module	(working copy)
@@ -2,12 +2,18 @@
 // $Id: uc_multiprice.module,v 1.19.2.6 2010/10/22 09:39:38 docc Exp $
 
 /**
- * @file Provides Multiprice options
+ * Provides Multiprice options
+ *
+ * Module developed by Docc | Optixdesigns.nl
+ * @author Docc 
+ * Module extended by Bartezz | Intrige.nl
+ * @author Bartezz <bartezz@gmail.com>
+ *
  */
 
-/*******************************************************************************
+/**
  * Hook Functions (Drupal)
- ******************************************************************************/
+ */
 
 /**
  * Implementation of hook_init().
@@ -31,13 +37,39 @@
     'type' => MENU_CALLBACK,
   );
   $items['admin/store/settings/multiprice'] = array(
-    'title' => 'Multiprice Defaults',
+    'title' => 'Multiprice',
     'page callback' => 'drupal_get_form',
     'page arguments' => array('uc_multiprice_admin_default'),
     'access arguments' => array('administer multiprice'),
     'type' => MENU_NORMAL_ITEM,
     'file' => 'uc_multiprice.admin.inc',
   );
+  $items['admin/store/settings/multiprice/defaults'] = array(
+    'title' => 'Multiprice Defaults',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('uc_multiprice_admin_default'),
+    'access arguments' => array('administer multiprice'),
+    'type' => MENU_DEFAULT_LOCAL_TASK,
+    'file' => 'uc_multiprice.admin.inc',
+  );
+  $items['admin/store/settings/multiprice/currencies'] = array(
+    'title' => 'Multiprice Currencies',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('uc_multiprice_admin_currencies'),
+    'access arguments' => array('administer multiprice'),
+    'type' => MENU_LOCAL_TASK,
+    'file' => 'uc_multiprice.admin.inc',
+    'weight' => 1,
+  );
+  $items['admin/store/settings/multiprice/roles'] = array(
+    'title' => 'Multiprice Roles',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('uc_multiprice_admin_roles'),
+    'access arguments' => array('administer multiprice roles'),
+    'type' => MENU_LOCAL_TASK,
+    'file' => 'uc_multiprice.admin.inc',
+    'weight' => 2,
+  );
 
   return $items;
 }
@@ -46,7 +78,7 @@
  * Implementation of hook_perm().
  */
 function uc_multiprice_perm() {
-  return array('administer multiprice', 'create & edit multiprices');
+  return array('administer multiprice', 'create & edit multiprices', 'administer multiprice roles');
 }
 
 /**
@@ -67,8 +99,8 @@
   if (uc_product_is_product_form($form) && $form['type']['#value'] != "product_kit") {
     if (user_access('create & edit multiprices')) {
       // add multiprice form
-      $countries = $form['#node']->multiprice;
-      $form += uc_multiprice_form($countries);
+      $multiprices = $form['#node']->multiprice;
+      $form += uc_multiprice_form($multiprices, $form['#node']->nid);
       $form['#pre_render'][] = 'uc_multiprice_add_js';
       
       // take over the pricing
@@ -77,9 +109,9 @@
       $default->list_price  = $form['base']['prices']['list_price']['#default_value'];
       $default->country_id  = 0;
       $default->country_name  = 'Default';
-      $form['multiprice']['countries'][$default->country_id] = uc_multiprice_field_element($default);
-      $form['multiprice']['countries'][$default->country_id]['#weight'] = -1;
-      unset($form['base']['prices']);
+      $form['multiprice']['multiprices'][$default->country_id] = uc_multiprice_field_element($default);
+      $form['multiprice']['multiprices'][$default->country_id]['#weight'] = -1;
+      unset($form['base']['prices']); 
     }
   }
   
@@ -92,6 +124,7 @@
     $form['panes']['delivery']['delivery_country']['#default_value'] = $id;
     $form['#validate'][] = 'uc_multiprice_checkout_validate';
   }
+  
 }
 
 /**
@@ -99,7 +132,7 @@
  */
 function uc_multiprice_checkout_validate($form, &$form_state) {
   $delivery_country = check_plain($form_state['values']['panes']['delivery']['delivery_country']);
-  if($delivery_country != uc_multiprice_country_id()) {
+  if ($delivery_country != uc_multiprice_country_id()) {
     uc_multiprice_country_id($delivery_country);
     $_SESSION['messages'] = array();
     drupal_goto('cart/checkout');
@@ -207,69 +240,135 @@
     switch ($op) {
       case 'prepare';
         // New product? add defaults
-        if(!$node->nid && !$node->multiprice && !$node->translation_source ) {
+        if (!$node->nid && !$node->multiprice && !$node->translation_source ) {
           $node->multiprice = variable_get('uc_multiprice_default', array());
-        }elseif(!$node->nid && !$node->multiprice && $node->translation_source ) {
+        }
+        elseif (!$node->nid && !$node->multiprice && $node->translation_source ) {
           $node->multiprice = $node->translation_source->multiprice;
         }
       break;  
       case 'load':  
+        // load enabled roles
+        $enabled_roles = array_filter(variable_get('uc_multiprice_roles', array()));
+        $all_roles = user_roles();
+        foreach ($enabled_roles as $k => $v) {
+          $roles[$k] = $all_roles[$k];
+        }
+        
         // load uc_countries
         $result = db_query("SELECT country_id, country_name FROM {uc_countries} WHERE version > 0 ORDER BY country_name ASC");
         while ($country = db_fetch_object($result)) {
           $countries[$country->country_id] = $country->country_name;
         }
-        
+
         // Add to node object
-        $result = db_query("SELECT * FROM {uc_multiprice} WHERE nid = %d", $node->nid);  
+        $result = db_query("SELECT m.*, c.currency_code FROM {uc_multiprice} m LEFT OUTER JOIN {uc_multiprice_currencies} c ON m.country_id = c.country_id WHERE m.nid = %d", $node->nid);
+        //$result = db_query("SELECT * FROM {uc_multiprice} WHERE nid = %d", $node->nid);
         while ($row = db_fetch_object($result)) {
-          $row->data = unserialize($row->data);
-          $row->country_name = $countries[$row->country_id];	  
-		  $row->sell_price = uc_store_format_price_field_value($row->sell_price);
-		  $row->list_price = uc_store_format_price_field_value($row->list_price);
-		  $row->cost = uc_store_format_price_field_value($row->cost);
-          $node->multiprice[$row->country_id] = $row;
+          $row->currency_code = $row->currency_code ? $row->currency_code : variable_get('uc_currency_code', 'USD');
+          $row->data = unserialize($row->data);  
+          $row->sell_price = uc_store_format_price_field_value($row->sell_price);
+          $row->list_price = uc_store_format_price_field_value($row->list_price);
+          $row->cost = uc_store_format_price_field_value($row->cost);
+          if ($row->country_id) {
+            $row->country_name = $countries[$row->country_id] ? $countries[$row->country_id] : (module_exists('countries_api') ? countries_api_get_country($row->country_id) : $row->country_id) . ' - ' . t('disabled');
+            $node->multiprice['country'][$row->country_id] = $row;
+          }
+          else {
+            $row->role_name = $roles[$row->role_id] ? $roles[$row->role_id] : $all_roles[$row->role_id] . ' - ' . t('disabled');
+            $node->multiprice['role'][$row->role_id] = $row;
+          }
         }
-        
         // Replace product price with multiprice if it's not node form.
         // @todo: how about revisions?
         if ((arg(2) != 'edit') && (arg(2) != 'add') && (arg(2) != 'clone') && (arg(2) != 'convert')) {
-          $cid = uc_multiprice_country_id();
-          if ($node->multiprice[$cid]) {
-              $node->sell_price = check_plain($node->multiprice[$cid]->sell_price);
-              $node->list_price = check_plain($node->multiprice[$cid]->list_price);
-              $node->cost       = check_plain($node->multiprice[$cid]->cost);
+          
+          // get the current user's role
+          global $user;
+          $user_roles = $user->roles;
+          // sort the user's roles by weight
+          if (variable_get('role_weights_reorder_forms', FALSE)) {
+            uksort($user_roles, '_role_weights_rid_compare');
           }
+
+          foreach (array_keys($user_roles) as $k => $v) {
+            // loop thru each role id
+            if ($node->multiprice['role'][$v] && in_array($v, $enabled_roles)) {
+              // if there is a price set for this role then output it, if the role still is enabled
+              $node->sell_price = check_plain($node->multiprice['role'][$v]->sell_price);
+              $node->list_price = check_plain($node->multiprice['role'][$v]->list_price);
+              $node->cost       = check_plain($node->multiprice['role'][$v]->cost);
+              $rolebased = TRUE;
+              // and break if we output a price, since it's by role weight
+              break;
+            }
+          }
+          if (!$rolebased && !variable_get('uc_multiprice_roles_fallback', NULL) ||
+              !$rolebased && variable_get('uc_multiprice_roles_fallback', NULL) && !array_intersect(array_keys($user_roles), $enabled_roles)) {
+            $cid = uc_multiprice_country_id();
+            if ($node->multiprice['country'][$cid]) {
+              $node->sell_price = check_plain($node->multiprice['country'][$cid]->sell_price);
+              $node->list_price = check_plain($node->multiprice['country'][$cid]->list_price);
+              $node->cost       = check_plain($node->multiprice['country'][$cid]->cost);
+            }
+            // if 'disable unprice product' has been checked
+          elseif (variable_get('uc_multiprice_currency_disable_unpriced_product', NULL)) {
+              $currency = uc_multiprice_currency($cid);
+              // and if the current users country currency doesn't match store default currency
+              if ($currency != variable_get('uc_currency_code', 'USD')) {
+                $pricehandlers = _uc_price_get_handlers(array(FALSE, FALSE));
+                // and if multiprice currency price handler is enabled
+                if ($pricehandlers['hook_data']['uc_multiprice']) {
+                  $node->disabled_unpriced_product = TRUE;
+                }
+              }
+            }
+          }         
+          
         }
       break;
       case 'presave';
         // reset the default pricing 
         // only if we have values to reset it with
-        if($node->multiprice['countries'][0]) {
-          $node->sell_price = check_plain($node->multiprice['countries'][0]['sell_price']);
-          $node->list_price = check_plain($node->multiprice['countries'][0]['list_price']);
-          $node->cost       = check_plain($node->multiprice['countries'][0]['cost']);
-          unset($node->multiprice['countries'][0]);
+        if ($node->multiprice['multiprices'][0]) {
+          $node->sell_price = check_plain($node->multiprice['multiprices'][0]['sell_price']);
+          $node->list_price = check_plain($node->multiprice['multiprices'][0]['list_price']);
+          $node->cost       = check_plain($node->multiprice['multiprices'][0]['cost']);
+          unset($node->multiprice['multiprices'][0]);
         }
       break;
       case 'insert':
       case 'update':
-        if (count($node->multiprice['countries']) == 0)
+			drupal_set_message('<pre><strong>UPDATE</strong>'.print_r($node->multiprice, true).'</pre>');
+        if (count($node->multiprice['multiprices']) == 0)
           break;
-       
         db_query("DELETE FROM {uc_multiprice} WHERE nid = %d", $node->nid);
-        unset($node->multiprice['countries'][0]);
- 
-        foreach ($node->multiprice['countries'] as $country_id => $country) {
+        unset($node->multiprice['multiprices'][0]);
+        foreach ((array)$node->multiprice['multiprices']['country'] as $country_id => $country) {
           if (empty($country->delete)) {
-            db_query("INSERT INTO {uc_multiprice} (nid, country_id, list_price, cost, sell_price, data) VALUES (%d, %d, %f, %f, %f, '%s')",
-              $node->nid, $country_id, $country['list_price'], $country['cost'], $country['sell_price'], serialize($country['dynamic']));
+//						
+            db_query("INSERT INTO {uc_multiprice} (nid, country_id, list_price, cost, sell_price, data, role_id) VALUES (%d, %d, %f, %f, %f, '%s', %d)",
+              $node->nid, $country['country_id'], $country['list_price'], $country['cost'], $country['sell_price'], serialize($country['dynamic']), $country['role_id']);
           }
         }
+        foreach ((array)$node->multiprice['multiprices']['role'] as $role_id => $role) {
+          if (empty($role->delete)) {
+            db_query("INSERT INTO {uc_multiprice} (nid, country_id, list_price, cost, sell_price, data, role_id) VALUES (%d, %d, %f, %f, %f, '%s', %d)",
+              $node->nid, $role['country_id'], $role['list_price'], $role['cost'], $role['sell_price'], serialize($role['dynamic']), $role['role_id']);
+          }
+        }
       break;
       case 'delete':
-	    db_query("DELETE FROM {uc_multiprice} WHERE nid = %d", $node->nid);
+      db_query("DELETE FROM {uc_multiprice} WHERE nid = %d", $node->nid);
       break;
+      case 'view':
+        if ($node->disabled_unpriced_product) {
+          $node->content['display_price']['#access'] = FALSE;
+          // @todo probably this should be ran thru a theme function
+          $node->content['sell_price']['#value'] = '<div class="product-unavailable">' . t('This listing is currently not available.') . '</div>';
+          $node->content['add_to_cart']['#access'] = FALSE;
+        }
+      break;
     }
   }
 }
@@ -302,13 +401,13 @@
   }
 }
 
-/*******************************************************************************
+/**
  * Hook Functions (Ubercart)
- ******************************************************************************/
+ */
 
 /**
-* Implementation of hook_cart_pane().
-*/
+ * Implementation of hook_cart_pane().
+ */
 function uc_multiprice_cart_pane($items) {
   $panes[] = array(
     'id' => 'multiprice_country_cart_form',
@@ -321,8 +420,8 @@
 }
   
 /**
-* Implementation of hook_order().
-*/
+ * Implementation of hook_order().
+ */
 function uc_multiprice_order($op, &$arg1, $arg2) {
   switch ($op) {
     case 'save':
@@ -341,37 +440,46 @@
   );
 }
 
-function uc_multiprice_price_handler_alter(&$price, &$context, &$options) {
-  $cid = uc_multiprice_country_id();
-  $result = db_query("SELECT * FROM {uc_multiprice_currencies} WHERE country_id = %d", $cid);
-  if ($currency = db_fetch_array($result)) {
-    $currency['dec'] = $currency['deci'];
-    $options = $currency + $options;
-    unset($options['deci']);
+function uc_multiprice_price_handler_alter(&$price, &$context, &$options) {  
+  // skip the multiprice handler if there is a price based on role
+  // as currency should change on country, not on role
+  
+  global $user;
+  $user_roles = $user->roles;
+  $enabled_roles = array_filter(variable_get('uc_multiprice_roles', array()));
+  // if the user has a multiprice enabled role then don't change currency
+  // as role based pricing is in default currency
+  if (!array_intersect(array_keys($user_roles), $enabled_roles)) {
+    $cid = uc_multiprice_country_id();
+    $result = db_query("SELECT * FROM {uc_multiprice_currencies} WHERE country_id = %d", $cid);
+    if ($currency = db_fetch_array($result)) {
+      $currency['dec'] = $currency['deci'];
+      $options = $currency + $options;
+      unset($options['deci']);
+    }
   }
-
-  if($context['type'] == 'product') {
+  if ($context['type'] == 'product') {
     $price['price'] = 0;
     $node = $context['subject']['node'];
     $node = node_load($node->nid);
-
-    if($node->type == "product_kit") { // Product kit
-      foreach($node->products as $product) {
+    if ($node->type == "product_kit") { // Product kit
+      foreach ($node->products as $product) {
         $field = ($context['field']) ? $product->$context['field'] : $product->sell_price; // check if its there
         $price['price'] += $field + $product->discount;
       }
-    }else{
+    }
+    else{
       $field = ($context['field']) ? $node->$context['field'] : $node->sell_price; // check if its there
       $price['price'] = $field;
     }
   }
 }
 
-/*******************************************************************************
+/**
  * Helpers and Callbacks
- ******************************************************************************/
+ */
 
-function uc_multiprice_form($countries, $default = NULL) {
+function uc_multiprice_form($multiprices, $default = NULL) {
   $form['multiprice'] = array(
     '#type' => 'fieldset',
     '#title' => t('Pricing'),
@@ -380,7 +488,7 @@
     '#collapsible' => TRUE,
     '#collapsed' => FALSE,
   );
-  $form['multiprice']['countries'] = array(
+  $form['multiprice']['multiprices'] = array(
     '#type' => 'fieldset',
     '#tree' => TRUE,
     '#weight' => 0,
@@ -390,34 +498,54 @@
     '#suffix' => '</div>',
     '#theme' => (module_exists('uc_multiprice_dynamic')) ? 'uc_multiprice_dynamic_form' : 'uc_multiprice_form',
   );
-  
   $exist = array();
-  if(count($countries)!=0) {
-    foreach($countries as $country) {
+  if (count($multiprices)!=0) {
+    foreach ((array)$multiprices['country'] as $country) {
       if (module_exists('uc_multiprice_dynamic')) {
-        $form['multiprice']['countries'][$country->country_id] = uc_multiprice_dynamic_field_element($country);
-        $form['multiprice']['countries'][$country->country_id]['dynamic']['list_price'] = uc_multiprice_dynamic_element($country, 'list_price');
-        $form['multiprice']['countries'][$country->country_id]['dynamic']['cost']       = uc_multiprice_dynamic_element($country, 'cost');
-        $form['multiprice']['countries'][$country->country_id]['dynamic']['sell_price'] = uc_multiprice_dynamic_element($country, 'sell_price');
-      }else{
-        $form['multiprice']['countries'][$country->country_id] = uc_multiprice_field_element($country);
+        $form['multiprice']['multiprices']['country'][$country->country_id] = uc_multiprice_dynamic_field_element($country);
+        $form['multiprice']['multiprices']['country'][$country->country_id]['dynamic']['list_price'] = uc_multiprice_dynamic_element($country, 'list_price');
+        $form['multiprice']['multiprices']['country'][$country->country_id]['dynamic']['cost']       = uc_multiprice_dynamic_element($country, 'cost');
+        $form['multiprice']['multiprices']['country'][$country->country_id]['dynamic']['sell_price'] = uc_multiprice_dynamic_element($country, 'sell_price');
       }
+      else{
+        $form['multiprice']['multiprices']['country'][$country->country_id] = uc_multiprice_field_element($country);
+      }
       $exist[] = $country->country_id;
     }
+    foreach ((array)$multiprices['role'] as $role) {
+      if (module_exists('uc_multiprice_dynamic')) {
+        $form['multiprice']['multiprices']['role'][$role->role_id] = uc_multiprice_dynamic_field_element($role);
+        $form['multiprice']['multiprices']['role'][$role->role_id]['dynamic']['list_price'] = uc_multiprice_dynamic_element($role, 'list_price');
+        $form['multiprice']['multiprices']['role'][$role->role_id]['dynamic']['cost']       = uc_multiprice_dynamic_element($role, 'cost');
+        $form['multiprice']['multiprices']['role'][$role->role_id]['dynamic']['sell_price'] = uc_multiprice_dynamic_element($role, 'sell_price');
+      }
+      else{
+        $form['multiprice']['multiprices']['role'][$role->role_id] = uc_multiprice_field_element($role);
+      }
+      $exist[] = $role->role_id;
+    }
   }
+  // build country dropdown
+  $multiprices = array();
+  $multiprices[0] = 'Select country or role';
+  // load enabled profiles
+  $enabled_roles = array_filter(variable_get('uc_multiprice_roles', array()));
+  $roles = user_roles();
+  foreach ($enabled_roles as $k => $v) {
+    if (!in_array($k, $exist)) {
+      $multiprices['role|' . $k] = $roles[$k];
+    }
+  }
+  // load countries
+  $result = db_query("SELECT country_id, country_name FROM {uc_countries} WHERE country_id <> %d AND version > 0 ORDER BY country_name ASC", uc_store_default_country());
   
-   // build country dropdown
-  $result = db_query("SELECT country_id, country_name FROM {uc_countries} WHERE country_id != %d AND version > 0 ORDER BY country_name ASC", uc_store_default_country());
-  $countries = array();
-  $countries[0] = 'Select country';
   while ($country = db_fetch_object($result)) {
   if (!in_array($country->country_id, $exist))
-    $countries[$country->country_id] = $country->country_name; 
+    $multiprices['country|' . $country->country_id] = $country->country_name; 
   }
-  
   $form['multiprice']['more'] = array(
     '#type' => 'fieldset',
-    '#title' => t('Add a price override for a country'),
+    '#title' => t('Add a price override for a country or role'),
     '#tree' => TRUE,
     '#weight' => 1,
     '#collapsible' => TRUE,
@@ -426,8 +554,8 @@
   
   $form['multiprice']['more']['list'] = array(
   '#type' => 'select',
-  '#description' => t("Select the country/region to add a price overide."),
-  '#options' => $countries,
+  '#description' => t("Select the country/region/role to add a price override."),
+  '#options' => $multiprices,
   '#ahah' => array(
     'path' => ($default) ? 'uc_multiprice/js/default' : 'uc_multiprice/js',
     'wrapper' => 'multiprice-countries',
@@ -435,10 +563,17 @@
     'effect' => 'slide',
     ),
   );
-  
   return $form;
 }
 
+function uc_multiprice_currency($cid) {
+  $result = db_query("SELECT * FROM {uc_multiprice_currencies} WHERE country_id = %d", $cid);
+  if ($currency = db_fetch_array($result)) {
+    return $currency['currency_code'];
+  }
+  return;
+}
+
 /**
  * Sets or returns the country_id
  * Avaiable for other modules
@@ -451,21 +586,19 @@
       uc_multiprice_save_country_to_order($order_id);
     }
   }
-  
   // Get Country by IP if not set
   if (empty($_SESSION['country_id'])) {
-   // IP by ip2country
-   if (module_exists('ip2country') && module_exists('countries_api')) {
-     $country_code = ip2country_get_country(ip_address());
-     $country = countries_api_get_country($country_code);
-     $_SESSION['country_id'] = $country['numcode'];
-   }
-   // IP by ip2cc
-   elseif (module_exists('ip2cc')) { 
-     $country = ip2cc_get_country(ip_address());
-     $_SESSION['country_id'] = $country->country_number;
-   }
-    
+    // IP by ip2country
+    if (module_exists('ip2country') && module_exists('countries_api')) {
+      $country_code = ip2country_get_country(ip_address());
+      $country = countries_api_get_country($country_code);
+      $_SESSION['country_id'] = $country['numcode'];
+    }
+    // IP by ip2cc
+    elseif (module_exists('ip2cc')) { 
+      $country = ip2cc_get_country(ip_address());
+      $_SESSION['country_id'] = $country->country_number;
+    }    
     // Still nothing? use default country
     if (empty($country)) {
       $_SESSION['country_id'] = uc_store_default_country();
@@ -476,34 +609,52 @@
 
 function theme_uc_multiprice_form($form) {
   $output = '';
-  $header = array(t('Country'), t('List price'), t('Cost'), t('Sell price'), t('Delete'));
+  $header = array(t('Country/role'), t('List price'), t('Cost'), t('Sell price'), t('Delete'));
   $row = array();
-  foreach (element_children($form) as $country_id) {
+
+  foreach (element_children($form) as $multiprice_id) {
     $row = array();
-    $row[] = check_plain($form[$country_id]['country']['#value']);
-    $row[] = drupal_render($form[$country_id]['list_price']);
-    $row[] = drupal_render($form[$country_id]['cost']);
-    $row[] = drupal_render($form[$country_id]['sell_price']);
-    $row[] = drupal_render($form[$country_id]['delete']);
-    $rows[] = $row;
+    if ($multiprice_id == '0') {
+      $row[] = check_plain($form[$multiprice_id]['title']['#value']);
+      $row[] = drupal_render($form[$multiprice_id]['list_price']);
+      $row[] = drupal_render($form[$multiprice_id]['cost']);
+      $row[] = drupal_render($form[$multiprice_id]['sell_price']);
+      $row[] = drupal_render($form[$multiprice_id]['delete']);
+      $rows[] = $row;
+    }
+    else {
+      foreach (element_children($form[$multiprice_id]) as $mpid) {
+        $row = array();
+        $row[] = check_plain($form[$multiprice_id][$mpid]['title']['#value']);
+        $row[] = drupal_render($form[$multiprice_id][$mpid]['list_price']);
+        $row[] = drupal_render($form[$multiprice_id][$mpid]['cost']);
+        $row[] = drupal_render($form[$multiprice_id][$mpid]['sell_price']);
+        $row[] = drupal_render($form[$multiprice_id][$mpid]['delete']);
+        $rows[] = $row;
+      }
+    }
   }
   $output .= theme('table', $header, $rows, array('id' => 'uc_multiprice_table'));
   return $output;
 }
 
-function uc_multiprice_field_element($country) {
+function uc_multiprice_field_element($multiprice) {
   $field['country_id'] = array(
     '#type' => 'hidden',
-    '#value' => $country->country_id,
+    '#value' => $multiprice->country_id,
   );
-  $field['country'] = array(
+  $field['role_id'] = array(
     '#type' => 'hidden',
-    '#default_value' => $country->country_name,
+    '#value' => $multiprice->role_id,
   );
+  $field['title'] = array(
+    '#type' => 'hidden',
+    '#default_value' => $multiprice->country_name ? $multiprice->country_name : $multiprice->role_name,
+  );
   $field['list_price'] = array(
     '#type' => 'textfield',
     '#required' => FALSE,
-    '#default_value' => isset($country->list_price) ? check_plain($country->list_price) : 0.00,
+    '#default_value' => isset($multiprice->list_price) ? check_plain($multiprice->list_price) : 0.00,
     '#weight' => 0,
     '#size' => 20,
     '#maxlength' => 35,
@@ -511,7 +662,7 @@
   $field['cost'] = array(
     '#type' => 'textfield',
     '#required' => FALSE,
-    '#default_value' => isset($country->cost) ? check_plain($country->cost) : 0.00,
+    '#default_value' => isset($multiprice->cost) ? check_plain($multiprice->cost) : 0.00,
     '#weight' => 1,
     '#size' => 20,
     '#maxlength' => 35,
@@ -520,7 +671,7 @@
     '#type' => 'textfield',
     '#required' => FALSE,
     '#weight' => 2,
-    '#default_value' => isset($country->sell_price) ? check_plain($country->sell_price) : 0.00,
+    '#default_value' => isset($multiprice->sell_price) ? check_plain($multiprice->sell_price) : 0.00,
     '#size' => 20,
     '#maxlength' => 35,
   );
@@ -539,29 +690,52 @@
   $form_state = array('submitted' => FALSE);
   $form_build_id = $_POST['form_build_id'];
   $form = form_get_cache($form_build_id, $form_state);
-  $country_id = check_plain($_POST['multiprice']['more']['list']);
   
-  // new field
-  $country = db_fetch_object(db_query("SELECT * FROM {uc_countries} WHERE country_id = %d", $country_id));
-  if (module_exists('uc_multiprice_dynamic')) {
-    $form['multiprice']['countries'][$country->country_id] = uc_multiprice_dynamic_field_element($country);
-    $form['multiprice']['countries'][$country->country_id]['dynamic']['list_price'] = uc_multiprice_dynamic_element($country, 'list_price');
-    $form['multiprice']['countries'][$country->country_id]['dynamic']['cost']       = uc_multiprice_dynamic_element($country, 'cost');
-    $form['multiprice']['countries'][$country->country_id]['dynamic']['sell_price'] = uc_multiprice_dynamic_element($country, 'sell_price');
-  }else{
-    $form['multiprice']['countries'][$country->country_id] = uc_multiprice_field_element($country);
+  list($type, $id) = explode('|', check_plain($_POST['multiprice']['more']['list']));
+
+  if ($type == 'country') {
+    // if one selected a cid
+    $country = db_fetch_object(db_query("SELECT * FROM {uc_countries} WHERE country_id = %d", $id));
+    if (module_exists('uc_multiprice_dynamic')) {
+      $form['multiprice']['multiprices']['country'][$country->country_id] = uc_multiprice_dynamic_field_element($country);
+      $form['multiprice']['multiprices']['country'][$country->country_id]['dynamic']['list_price'] = uc_multiprice_dynamic_element($country, 'list_price');
+      $form['multiprice']['multiprices']['country'][$country->country_id]['dynamic']['cost']       = uc_multiprice_dynamic_element($country, 'cost');
+      $form['multiprice']['multiprices']['country'][$country->country_id]['dynamic']['sell_price'] = uc_multiprice_dynamic_element($country, 'sell_price');
+    }
+    else{
+      $form['multiprice']['multiprices']['country'][$country->country_id] = uc_multiprice_field_element($country);
+    }
+  } 
+  elseif ($type == 'role') {
+    // if one selected a rid
+    $enabled_roles = array_filter(variable_get('uc_multiprice_roles', array()));
+    $roles = user_roles();
+    foreach ($enabled_roles as $k => $v) {
+      if ($k == $id) {
+        $role->role_id = $k;
+        $role->role_name = $roles[$k];
+      }
+    }    
+    if (module_exists('uc_multiprice_dynamic')) {
+      $form['multiprice']['multiprices']['role'][$role->role_id] = uc_multiprice_dynamic_field_element($role);
+      $form['multiprice']['multiprices']['role'][$role->role_id]['dynamic']['list_price'] = uc_multiprice_dynamic_element($role, 'list_price');
+      $form['multiprice']['multiprices']['role'][$role->role_id]['dynamic']['cost']       = uc_multiprice_dynamic_element($role, 'cost');
+      $form['multiprice']['multiprices']['role'][$role->role_id]['dynamic']['sell_price'] = uc_multiprice_dynamic_element($role, 'sell_price');
+    }
+    else {
+      $form['multiprice']['multiprices']['role'][$role->role_id] = uc_multiprice_field_element($role);
+    }
   }
 
   form_set_cache($form_build_id, $form, $form_state);
 
   // Rebuild the form. 
   $form = form_builder($_POST['form_id'], $form, $form_state);
-  
   // return correct part
-  unset($form['multiprice']['countries']['prefix']);
-  unset($form['multiprice']['countries']['suffix']);
-  $output = drupal_render($form['multiprice']['countries']);
-  $output .= '<script type="text/javascript">Drupal.settings.uc_multiprice.countries.push('. $country_id .');</script>'; // bad programming?
+  unset($form['multiprice']['multiprices']['prefix']);
+  unset($form['multiprice']['multiprices']['suffix']);
+  $output = drupal_render($form['multiprice']['multiprices']);
+  $output .= '<script type="text/javascript">Drupal.settings.uc_multiprice.'. $type .'.push('. $id .');$("#edit-multiprice-more-list option:selected").remove()</script>'; // bad programming?
   
   print drupal_to_js(array('status' => TRUE, 'data' => $output));
   exit;
@@ -586,14 +760,16 @@
   return $form;  
 }
 
-// save selection to session
+/**
+ * Save selection to session.
+ */
 function uc_multiprice_region_form_submit($form, &$form_state) {
   uc_multiprice_country_id($form_state['values']['country_id']);
 }
   
 /**
-* Save selected country id to the order.
-*/
+ * Save selected country id to the order.
+ */
 function uc_multiprice_save_country_to_order($order_id) {
   // Load up the existing data array.
   $data = db_result(db_query("SELECT data FROM {uc_orders} WHERE order_id = %d", $order_id));
@@ -607,14 +783,21 @@
 }
 
 /**
-* Get countries from the multiprice form and add to drupal settings
-*/
+ * Get countries from the multiprice form and add to drupal settings
+ */
 function uc_multiprice_add_js($form) {
-  foreach($form['multiprice']['countries'] as $key => $country) {
-    if(is_array($country) && isset($country['country_id'])) {
+  $countries = array();
+  foreach ((array)$form['multiprice']['multiprices']['country'] as $key => $country) {
+    if (is_array($country) && isset($country['country_id'])) {
       $countries[] = $key;
     }
   }
-  drupal_add_js(array('uc_multiprice' => array('countries' => $countries)), 'setting');
+  $roles = array();
+  foreach ((array)$form['multiprice']['multiprices']['role'] as $key => $role) {
+    if (is_array($role) && isset($role['role_id'])) {
+      $roles[] = $key;
+    }
+  }
+  drupal_add_js(array('uc_multiprice' => array('country' => $countries, 'role' => $roles)), 'setting');
   return $form;
 }
\ No newline at end of file
Index: uc_multiprice_dynamic/js/uc_multiprice_dynamic.js
===================================================================
--- uc_multiprice_dynamic/js/uc_multiprice_dynamic.js	(revision 29)
+++ uc_multiprice_dynamic/js/uc_multiprice_dynamic.js	(working copy)
@@ -1,12 +1,14 @@
-function uc_multiprice_dynamic(country_id, field) {
+
+function uc_multiprice_dynamic(field_id, field, countryrole) {
+
 	var field = field.replace('_', '-');
-	var _return = $("#edit-multiprice-countries-"+country_id+"-"+field+"");
-	var _default = $("#edit-multiprice-countries-0-"+field);
-	var dynamic_button = $("#edit-multiprice-countries-"+country_id+"-edit-"+field);
-	var enabled = $("#edit-multiprice-countries-"+country_id+"-dynamic-"+field+"-enabled");
+	var _return = $("#edit-multiprice-multiprices-"+countryrole+"-"+field_id+"-"+field+"");
+	var _default = $("#edit-multiprice-multiprices-0-"+field);
+	var dynamic_button = $("#edit-multiprice-multiprices-"+countryrole+"-"+field_id+"-edit-"+field);
+	var enabled = $("#edit-multiprice-multiprices-"+countryrole+"-"+field_id+"-dynamic-"+field+"-enabled");
 	
 	//check if enabled otherwise quit
-	if(enabled.attr("checked")) {
+	if (enabled.attr("checked")) {
 		$(dynamic_button).addClass('dynamic_on');
 		_return.attr("disabled", "disabled");
 		$(_return).addClass('disabled');
@@ -18,14 +20,14 @@
 	}
 
 	// get operators and values
-	var operator 		= $("#edit-multiprice-countries-"+country_id+"-dynamic-"+field+"-operator-value");
-	var operator_1 	= $("#edit-multiprice-countries-"+country_id+"-dynamic-"+field+"-1-operator");
-	var value_1			= $("#edit-multiprice-countries-"+country_id+"-dynamic-"+field+"-1-value");
-	var operator_2	= $("#edit-multiprice-countries-"+country_id+"-dynamic-"+field+"-2-operator");
-	var value_2 		= $("#edit-multiprice-countries-"+country_id+"-dynamic-"+field+"-2-value");
+	var operator 		= $("#edit-multiprice-multiprices-"+countryrole+"-"+field_id+"-dynamic-"+field+"-operator-value");
+	var operator_1 	= $("#edit-multiprice-multiprices-"+countryrole+"-"+field_id+"-dynamic-"+field+"-1-operator");
+	var value_1			= $("#edit-multiprice-multiprices-"+countryrole+"-"+field_id+"-dynamic-"+field+"-1-value");
+	var operator_2	= $("#edit-multiprice-multiprices-"+countryrole+"-"+field_id+"-dynamic-"+field+"-2-operator");
+	var value_2 		= $("#edit-multiprice-multiprices-"+countryrole+"-"+field_id+"-dynamic-"+field+"-2-value");
 
 	// do some changing
-	if(operator_1.val() == 'default') {
+	if (operator_1.val() == 'default') {
 		value_1.val(_default.val());
 		value_1.attr("disabled", "disabled");
 		$(value_1).addClass('disabled');
@@ -34,7 +36,7 @@
 		$(value_1).removeClass('disabled');
 	}
 	
-	if(operator_2.val() == 'default') {
+	if (operator_2.val() == 'default') {
 		value_2.val(_default.val());
 		value_2.attr("disabled", "disabled");
 		$(value_2).addClass('disabled');
@@ -43,24 +45,24 @@
 		$(value_2).removeClass('disabled');
 	}
 	
-	if(operator_1.val() == 'custom') {
+	if (operator_1.val() == 'custom') {
 		value_1.attr("disabled", '');
 	}
-	if(operator_2.val() == 'custom') {
+	if (operator_2.val() == 'custom') {
 		value_2.attr("disabled", '');
 	}
 	
 	// dynamiculations
-	if(operator.val() == 'x') {
+	if (operator.val() == 'x') {
 		result = parseFloat(value_1.val()) * parseFloat(value_2.val());
 	}
-	if(operator.val() == '-') {
+	if (operator.val() == '-') {
 		result = parseFloat(value_1.val()) - parseFloat(value_2.val());
 	}
-	if(operator.val() == '+') {
+	if (operator.val() == '+') {
 		result = parseFloat(value_1.val()) + parseFloat(value_2.val());
 	}
-	if(operator.val() == '/') {
+	if (operator.val() == '/') {
 		result = parseFloat(value_1.val()) / parseFloat(value_2.val());
 	}
 	
@@ -69,9 +71,12 @@
 }
 
 function uc_multiprice_dynamic_recalculate(field) {
-	jQuery.each(Drupal.settings.uc_multiprice.countries, function() {
-			uc_multiprice_dynamic(this, field);																																
-	})
+	jQuery.each(Drupal.settings.uc_multiprice.country, function() {
+			uc_multiprice_dynamic(this, field, 'country');																																
+	});
+	jQuery.each(Drupal.settings.uc_multiprice.role, function() {
+			uc_multiprice_dynamic(this, field, 'role');																																
+	});
 }
 
 Drupal.behaviors.uc_multiprice_dynamic = function (context) {
@@ -94,13 +99,13 @@
 	});
 
 	// add listeners to default
-	$("#edit-multiprice-countries-0-list-price").change(function(){
+	$("#edit-multiprice-multiprice-0-list-price").change(function(){
 			uc_multiprice_dynamic_recalculate('list-price');
 	});
-	$("#edit-multiprice-countries-0-sell-price").change(function(){
+	$("#edit-multiprice-multiprice-0-sell-price").change(function(){
 			uc_multiprice_dynamic_recalculate('sell-price');
 	});
-	$("#edit-multiprice-countries-0-cost").change(function(){
+	$("#edit-multiprice-multiprice-0-cost").change(function(){
 			uc_multiprice_dynamic_recalculate('cost');
 	});
 	
Index: uc_multiprice_dynamic/uc_multiprice_dynamic.module
===================================================================
--- uc_multiprice_dynamic/uc_multiprice_dynamic.module	(revision 29)
+++ uc_multiprice_dynamic/uc_multiprice_dynamic.module	(working copy)
@@ -2,12 +2,18 @@
 // $Id: uc_multiprice_dynamic.module,v 1.7 2009/10/14 08:28:03 docc Exp $
 
 /**
- * @file Provides Multiprice dynamic options
+ * Provides Multiprice options
+ *
+ * Module developed by Docc | Optixdesigns.nl
+ * @author Docc 
+ * Module extended by Bartezz | Intrige.nl
+ * @author Bartezz <bartezz@gmail.com>
+ *
  */
 
-/*******************************************************************************
+/**
  * Hook Functions (Drupal)
- ******************************************************************************/
+ */
 
 /**
  * Implementation of hook_menu().
@@ -43,9 +49,9 @@
   );
 }
 
-/*******************************************************************************
+/**
  * Theme functions
- ******************************************************************************/
+ */
 
 /**
  * theme_uc_multiprice_dynamic_element_form
@@ -64,43 +70,60 @@
   drupal_add_js($path .'/js/uc_multiprice_dynamic.js', 'module', 'header');  
   drupal_add_css($path .'/css/uc_multiprice_dynamic_style.css', 'module', 'all', FALSE);
   
-  $header = array(array('data' => t('Country'), 'width' => '125px'), '', t('List price'), '', t('Cost'), '', t('Sell price'), t('Delete'));
+  $header = array(array('data' => t('Country/role'), 'width' => '125px'), '', t('List price'), '', t('Cost'), '', t('Sell price'), t('Delete'));
   
   $row = array();
-  foreach (element_children($form) as $country_id) {
+  foreach (element_children($form) as $multiprice_id) {
     $row = array();
-    if ($country_id != '0' || $country_id != 'dynamic') {
-      $row[] = check_plain($form[$country_id]['country']['#value']);
-      $row[] = drupal_render($form[$country_id]['edit']['list_price']);
-      $row[] = drupal_render($form[$country_id]['list_price']);
-      $row[] = drupal_render($form[$country_id]['edit']['cost']);
-      $row[] = drupal_render($form[$country_id]['cost']);
-      $row[] = drupal_render($form[$country_id]['edit']['sell_price']);
-      $row[] = drupal_render($form[$country_id]['sell_price']);
-      $row[] = drupal_render($form[$country_id]['delete']);
-    }elseif($country_id == '0') {
-      $row[] = check_plain($form[$country_id]['country']['#value']);
+    if ($multiprice_id != '0' || $multiprice_id != 'dynamic') {
+      foreach (element_children($form[$multiprice_id]) as $mpid) {
+        $row = array();
+        $row[] = check_plain($form[$multiprice_id][$mpid]['title']['#value']);
+        $row[] = drupal_render($form[$multiprice_id][$mpid]['edit']['list_price']);
+        $row[] = drupal_render($form[$multiprice_id][$mpid]['list_price']);
+        $row[] = drupal_render($form[$multiprice_id][$mpid]['edit']['cost']);
+        $row[] = drupal_render($form[$multiprice_id][$mpid]['cost']);
+        $row[] = drupal_render($form[$multiprice_id][$mpid]['edit']['sell_price']);
+        $row[] = drupal_render($form[$multiprice_id][$mpid]['sell_price']);
+        $row[] = drupal_render($form[$multiprice_id][$mpid]['delete']);
+        $rows[] = $row;
+        
+        $row = array();
+        $row[] = array('data' => drupal_render($form[$multiprice_id][$mpid]['dynamic']['list_price']), 'colspan' => '8');
+        $rows[] = array('data' => $row, 'id' => 'edit-multiprice-multiprices-'. $multiprice_id .'-'. $mpid .'-edit-list-price-dynamic', 'class' => 'popup');
+        
+        $row = array();
+        $row[] = array('data' => drupal_render($form[$multiprice_id][$mpid]['dynamic']['cost']), 'colspan' => '8');
+        $rows[] = array('data' => $row, 'id' => 'edit-multiprice-multiprices-'. $multiprice_id .'-'. $mpid .'-edit-cost-dynamic', 'class' => 'popup');
+        
+        $row = array();
+        $row[] = array('data' => drupal_render($form[$multiprice_id][$mpid]['dynamic']['sell_price']), 'colspan' => '8');
+        $rows[] = array('data' => $row, 'id' => 'edit-multiprice-multiprices-'. $multiprice_id .'-'. $mpid .'-edit-sell-price-dynamic', 'class' => 'popup');
+      }
+    }
+    elseif ($multiprice_id == '0') {
+      $row[] = check_plain($form[$multiprice_id]['title']['#value']);
       $row[] = '';
-      $row[] = drupal_render($form[$country_id]['list_price']);
+      $row[] = drupal_render($form[$multiprice_id]['list_price']);
       $row[] = '';
-      $row[] = drupal_render($form[$country_id]['cost']);
+      $row[] = drupal_render($form[$multiprice_id]['cost']);
       $row[] = '';
-      $row[] = drupal_render($form[$country_id]['sell_price']);
+      $row[] = drupal_render($form[$multiprice_id]['sell_price']);
       $row[] = '';
-    }  
-    $rows[] = $row;
-    
-    $row = array();
-    $row[] = array('data' => drupal_render($form[$country_id]['dynamic']['list_price']), 'colspan' => '8');
-    $rows[] = array('data' => $row, 'id' => 'edit-multiprice-countries-'. $country_id .'-edit-list-price-dynamic', 'class' => 'popup');
-    
-    $row = array();
-    $row[] = array('data' => drupal_render($form[$country_id]['dynamic']['cost']), 'colspan' => '8');
-    $rows[] = array('data' => $row, 'id' => 'edit-multiprice-countries-'. $country_id .'-edit-cost-dynamic', 'class' => 'popup');
-    
-    $row = array();
-    $row[] = array('data' => drupal_render($form[$country_id]['dynamic']['sell_price']), 'colspan' => '8');
-    $rows[] = array('data' => $row, 'id' => 'edit-multiprice-countries-'. $country_id .'-edit-sell-price-dynamic', 'class' => 'popup');
+      $rows[] = $row;
+      
+      $row = array();
+      $row[] = array('data' => drupal_render($form[$multiprice_id]['dynamic']['list_price']), 'colspan' => '8');
+      $rows[] = array('data' => $row, 'id' => 'edit-multiprice-multiprices-'. $multiprice_id .'-edit-list-price-dynamic', 'class' => 'popup');
+      
+      $row = array();
+      $row[] = array('data' => drupal_render($form[$multiprice_id]['dynamic']['cost']), 'colspan' => '8');
+      $rows[] = array('data' => $row, 'id' => 'edit-multiprice-multiprices-'. $multiprice_id .'-edit-cost-dynamic', 'class' => 'popup');
+      
+      $row = array();
+      $row[] = array('data' => drupal_render($form[$multiprice_id]['dynamic']['sell_price']), 'colspan' => '8');
+      $rows[] = array('data' => $row, 'id' => 'edit-multiprice-multiprices-'. $multiprice_id .'-edit-sell-price-dynamic', 'class' => 'popup');
+    }    
   }
   
   $output .= theme('table', $header, $rows, array('id' => 'uc_multiprice_table'));
@@ -108,18 +131,17 @@
   return $output;
 }
 
-/*******************************************************************************
+/**
  * Callbacks, forms etc
- ******************************************************************************/
-
-function uc_multiprice_dynamic_element($country, $type) {
+ */
+function uc_multiprice_dynamic_element($multiprice, $type) {
   $field['#theme'] = 'uc_multiprice_dynamic_element_form';
   $field['enabled'] = array(
       '#type' => 'checkbox',
       '#title' => t('Enabled'),
-      '#description' => t("Calculate @type for @country", array('@type' => str_replace('_', ' ', $type), '@country' => $country->country_name)),
-      '#default_value' => isset($country->data[$type]['enabled']) ? $country->data[$type]['enabled'] : 0,
-      '#attributes' => array('onclick' => 'uc_multiprice_dynamic("'. $country->country_id .'", "'. $type .'");'),
+      '#description' => t("Calculate @type for @country", array('@type' => str_replace('_', ' ', $type), '@country' => ($multiprice->country_name ? $multiprice->country_name : $multiprice->role_name))),
+      '#default_value' => isset($multiprice->data[$type]['enabled']) ? $multiprice->data[$type]['enabled'] : 0,
+      '#attributes' => array('onclick' => 'uc_multiprice_dynamic("'. ($multiprice->country_id ? $multiprice->country_id : $multiprice->role_id) .'", "'. $type .'", "'. ($multiprice->country_name ? 'country' : 'role') .'");'),
   );
   $field['type'] = array(
     '#type' => 'hidden',
@@ -135,17 +157,17 @@
     '#type' => 'select',
     '#required' => FALSE,
     '#options' => array('default' => 'Default', 'custom' => 'Custom'),
-    '#default_value' => isset($country->data[$type]['1']) ? check_plain($country->data[$type]['1']['operator']) : 'custom',
-    '#attributes' => array('onchange' => 'uc_multiprice_dynamic("'. $country->country_id .'", "'. $type .'");'),
+    '#default_value' => isset($multiprice->data[$type]['1']) ? check_plain($multiprice->data[$type]['1']['operator']) : 'custom',
+    '#attributes' => array('onchange' => 'uc_multiprice_dynamic("'. ($multiprice->country_id ? $multiprice->country_id : $multiprice->role_id) .'", "'. $type .'", "'. ($multiprice->country_name ? 'country' : 'role') .'");'),
   );
   $field['1']['value'] = array(
     '#type' => 'textfield',
-    '#default_value' => isset($country->data[$type]['1']['value']) ? check_plain($country->data[$type]['1']['value']) : 0.00,
+    '#default_value' => isset($multiprice->data[$type]['1']['value']) ? check_plain($multiprice->data[$type]['1']['value']) : 0.00,
     '#size' => 5,
-    '#attributes' => array('onchange' => 'uc_multiprice_dynamic("'. $country->country_id .'", "'. $type .'");'),
-    //'#disabled' => ($country->data[$type]['1']['operator']=='default')? TRUE : FALSE,
+    '#attributes' => array('onchange' => 'uc_multiprice_dynamic("'. ($multiprice->country_id ? $multiprice->country_id : $multiprice->role_id) .'", "'. $type .'", "'. ($multiprice->country_name ? 'country' : 'role') .'");'),
+    //'#disabled' => ($multiprice->data[$type]['1']['operator']=='default')? TRUE : FALSE,
   );
-	$field['operator'] = array(
+  $field['operator'] = array(
       '#type' => 'fieldset',
       '#tree' => TRUE,
       '#collapsible' => FALSE,
@@ -155,8 +177,8 @@
     '#type' => 'select',
     '#required' => FALSE,
     '#options' => array('x' => 'x', '-' => '-', '+' => '+', '/' => '/'),
-    '#attributes' => array('onchange' => 'uc_multiprice_dynamic("'. $country->country_id .'", "'. $type .'");'),
-    '#default_value' => isset($country->data[$type]) ? check_plain($country->data[$type]['value']) : 'x',
+    '#attributes' => array('onchange' => 'uc_multiprice_dynamic("'. ($multiprice->country_id ? $multiprice->country_id : $multiprice->role_id) .'", "'. $type .'", "'. ($multiprice->country_name ? 'country' : 'role') .'");'),
+    '#default_value' => isset($multiprice->data[$type]) ? check_plain($multiprice->data[$type]['value']) : 'x',
   );
   $field['2'] = array(
       '#type' => 'fieldset',
@@ -167,43 +189,42 @@
   $field['2']['operator'] = array(
     '#type' => 'select',
     '#options' => array('default' => 'Default', 'custom' => 'Custom'),
-    '#default_value' => isset($country->data[$type]['2']) ? check_plain($country->data[$type]['2']['operator']) : 'default',
-    '#attributes' => array('onchange' => 'uc_multiprice_dynamic("'. $country->country_id .'", "'. $type .'");'),
+    '#default_value' => isset($multiprice->data[$type]['2']) ? check_plain($multiprice->data[$type]['2']['operator']) : 'default',
+    '#attributes' => array('onchange' => 'uc_multiprice_dynamic("'. ($multiprice->country_id ? $multiprice->country_id : $multiprice->role_id) .'", "'. $type .'", "'. ($multiprice->country_name ? 'country' : 'role') .'");'),
   );
   $field['2']['value'] = array(
     '#type' => 'textfield',
-    '#default_value' => isset($country->data[$type]['2']) ? check_plain($country->data[$type]['2']['value']) : 0.00,
+    '#default_value' => isset($multiprice->data[$type]['2']) ? check_plain($multiprice->data[$type]['2']['value']) : 0.00,
     '#size' => 5,
-    '#attributes' => array('onchange' => 'uc_multiprice_dynamic("'. $country->country_id .'", "'. $type .'");'),
-    //'#disabled' => ($country->data[$type]['2']['operator']=='default')? TRUE : FALSE,
+    '#attributes' => array('onchange' => 'uc_multiprice_dynamic("'. ($multiprice->country_id ? $multiprice->country_id : $multiprice->role_id) .'", "'. $type .'", "'. ($multiprice->country_name ? 'country' : 'role') .'");'),
+    //'#disabled' => ($multiprice->data[$type]['2']['operator']=='default')? TRUE : FALSE,
   );
 
   return $field;
 }
 
-function uc_multiprice_dynamic_field_element($country) {
+function uc_multiprice_dynamic_field_element($multiprice) {
   $field['country_id'] = array(
     '#type' => 'hidden',
-    '#value' => $country->country_id,
-  );/*
-  $field['country'] = array(
-    '#type' => 'textfield',
-    '#default_value' => $country->country_name,
+    '#value' => $multiprice->country_id,
   );
-  */
-  $field['country'] = array(
+  $field['role_id'] = array(
     '#type' => 'hidden',
-    '#default_value' => $country->country_name,
+    '#value' => $multiprice->role_id,
   );
+  $field['title'] = array(
+    '#type' => 'hidden',
+    '#default_value' => $multiprice->country_name ? $multiprice->country_name : $multiprice->role_name,
+  );
   $field['edit']['list_price'] = array(
     '#type' => 'button',
     '#weight' => 1,
-    '#attributes' => array('class' => ($country->data['list_price']['enabled']==1) ? 'dynamic dynamic_on' : 'dynamic'),
+    '#attributes' => array('class' => ($multiprice->data['list_price']['enabled']==1) ? 'dynamic dynamic_on' : 'dynamic'),
   );
   $field['list_price'] = array(
     '#type' => 'textfield',
     '#required' => FALSE,
-    '#default_value' => isset($country->list_price) ? check_plain($country->list_price) : 0.00,
+    '#default_value' => isset($multiprice->list_price) ? check_plain($multiprice->list_price) : 0.00,
     '#weight' => 0,
     '#size' => 10,
     '#maxlength' => 35,
@@ -211,12 +232,12 @@
   $field['edit']['cost'] = array(
     '#type' => 'button',
     '#weight' => 1,
-    '#attributes' => array('class' => ($country->data['cost']['enabled']==1) ? 'dynamic dynamic_on' : 'dynamic'),
+    '#attributes' => array('class' => ($multiprice->data['cost']['enabled']==1) ? 'dynamic dynamic_on' : 'dynamic'),
   );
   $field['cost'] = array(
     '#type' => 'textfield',
     '#required' => FALSE,
-    '#default_value' => isset($country->cost) ? check_plain($country->cost) : 0.00,
+    '#default_value' => isset($multiprice->cost) ? check_plain($multiprice->cost) : 0.00,
     '#weight' => 1,
     '#size' => 20,
     '#maxlength' => 35,
@@ -224,13 +245,13 @@
   $field['edit']['sell_price'] = array(
     '#type' => 'button',
     '#weight' => 1,
-    '#attributes' => array('class' => ($country->data['sell_price']['enabled']==1) ? 'dynamic dynamic_on' : 'dynamic'),
+    '#attributes' => array('class' => ($multiprice->data['sell_price']['enabled']==1) ? 'dynamic dynamic_on' : 'dynamic'),
   );
   $field['sell_price'] = array(
     '#type' => 'textfield',
     '#required' => FALSE,
     '#weight' => 2,
-    '#default_value' => isset($country->sell_price) ? check_plain($country->sell_price) : 0.00,
+    '#default_value' => isset($multiprice->sell_price) ? check_plain($multiprice->sell_price) : 0.00,
     '#size' => 20,
     '#maxlength' => 35,
   );
Index: uc_product_handler_field_multiprice.inc
===================================================================
--- uc_product_handler_field_multiprice.inc	(revision 29)
+++ uc_product_handler_field_multiprice.inc	(working copy)
@@ -11,17 +11,14 @@
  */
 class uc_product_handler_field_multiprice extends views_handler_field {
   function render($values) {
-    $multiprice = db_fetch_object(db_query("SELECT * FROM {uc_multiprice} WHERE nid = %d AND country_id = %d", 
-                       $values->nid, uc_multiprice_country_id()));
+    $multiprice = db_fetch_object(db_query("SELECT * FROM {uc_multiprice} WHERE nid = %d AND country_id = %d", $values->nid, uc_multiprice_country_id()));
     if ($multiprice) {
       if ($values->uc_products_sell_price)
-       $values->uc_products_sell_price = check_plain($multiprice->sell_price);
-       
+        $values->uc_products_sell_price = check_plain($multiprice->sell_price);
       if ($values->uc_products_cost)
-       $values->uc_products_cost = check_plain($multiprice->cost);
-      
+        $values->uc_products_cost = check_plain($multiprice->cost);
       if ($values->uc_products_list_price)
-       $values->uc_products_list_price = check_plain($multiprice->list_price);
+        $values->uc_products_list_price = check_plain($multiprice->list_price);
     }
 
     $context = array(
@@ -32,10 +29,10 @@
       'field' => $this->real_field,
       ),
     );
-    $table_alias_len = strlen($this->table_alias);
+    $table_alias_len = drupal_strlen($this->table_alias);
     foreach ($values as $key => $value) {
-      if (substr($key, 0, $table_alias_len) == $this->table_alias) {
-        $values->{substr($key, $table_alias_len + 1)} = $value;
+      if (drupal_substr($key, 0, $table_alias_len) == $this->table_alias) {
+        $values->{drupal_substr($key, $table_alias_len + 1)} = $value;
       }
     }
     
