? d6_ec4.patch
Index: role_price.info
===================================================================
RCS file: /cvs/drupal/contributions/modules/role_price/role_price.info,v
retrieving revision 1.1
diff -u -p -r1.1 role_price.info
--- role_price.info	22 Jun 2008 09:54:39 -0000	1.1
+++ role_price.info	8 Jan 2009 14:04:27 -0000
@@ -1,6 +1,8 @@
-;$Id: role_price.info,v 1.1 2008/06/22 09:54:39 robertgarrigos Exp $
-dependencies  = store
-description   = "Role prices for E-Commerce."
-package       = "E-Commerce Uncategorized"
-name          = "Role Price"
-
+name = "Role Price"
+description = "Role prices for E-Commerce."
+core = 6.x
+php = 5.1
+package= "E-Commerce Uncategorized"
+dependencies[] = ec_store
+version = "6.x-beta1"
+project = "role_price"
Index: role_price.install
===================================================================
RCS file: /cvs/drupal/contributions/modules/role_price/role_price.install,v
retrieving revision 1.1
diff -u -p -r1.1 role_price.install
--- role_price.install	22 Jun 2008 09:54:39 -0000	1.1
+++ role_price.install	8 Jan 2009 14:04:27 -0000
@@ -1,20 +1,52 @@
 <?php
-// $Id: role_price.install,v 1.1 2008/06/22 09:54:39 robertgarrigos Exp $
 
 /**
- * E-Commerce role discount module schema
+ * Implementation of hook_install().
  */
 function role_price_install() {
-  switch ($GLOBALS['db_type']) {
-    case 'mysql':
-    case 'mysqli':
-      db_query("CREATE TABLE {ec_role_price} (
-        nid int(10) unsigned NOT NULL default '0',
-        rid int(5) unsigned NOT NULL default '0',
-        price decimal(10,2) NOT NULL default '0.00'
-      ) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */;");
-      break;
-  }
-  drupal_set_message(t('E-Commerce: Price per Role tables have been created.'));
+  // Create tables.
+  drupal_install_schema('role_price');
+}
+
+/**
+ * Implementation of hook_uninstall().
+ */
+function role_price_uninstall() {
+  // Remove tables.
+  drupal_uninstall_schema('role_price');
 }
 
+/**
+ * Implementation of hook_schema().
+ * E-Commerce role discount module schema
+ */
+function role_price_schema() {
+  $schema['ec_role_price'] = array(
+    'description' => t('Stores relation between nodes and polls.'),
+    'fields' => array(
+      'nid'     => array(
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0,
+        ),
+      'rid' => array(
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0,
+        ),
+      'price' => array(
+        'type' => 'numeric',
+        'size' => 'normal',
+        'not null' => TRUE,
+        'precision' => 10,
+        'scale' => 2,
+        'default' => 0,
+        ),        
+      ),
+    'primary key' => array('nid', 'rid'),
+    );
+
+  return $schema;
+}
Index: role_price.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/role_price/role_price.module,v
retrieving revision 1.3
diff -u -p -r1.3 role_price.module
--- role_price.module	24 Jun 2008 17:40:17 -0000	1.3
+++ role_price.module	8 Jan 2009 14:04:28 -0000
@@ -1,5 +1,5 @@
 <?php
-// $Id: role_price.module,v 1.3 2008/06/24 17:40:17 robertgarrigos Exp $
+// $Id: role_price.module,v 1.1 2008/06/22 09:54:39 robertgarrigos Exp $
 
 /**
  * @file
@@ -15,96 +15,88 @@
  * Drupal Hooks
  ********************************************************************/
 
-/*
- * Implementation of hook_help().
+/**
+ * Implementation of menu_hook()
+ */
+function role_price_menu() {
+  $items['admin/ecsettings/role_price'] = array(
+    'title' => t('Role price'),
+    'description' => t('Set which roles will be eligible for special price.'),
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('role_price_settings'),
+    'access callback' => 'user_access',
+    'access arguments' => array('administer store')
+  );
 
-function role_price_help($section) {
-  switch ($section) {
-    case 'admin/help#role_price':
-      return '<p>'. t('') .'</p>';
-      break;
-    case 'admin/ecsettings/role_price':
-      return '<p>'. t('') .'</p>';
-      break;
-  }
+  return $items;
 }
-*/
 
 /**
- * Implementation of menu_hook()
+ * Implementation of theme_hook()
  */
-function role_price_menu($may_cache) {
-  $items = array();
-  if ($may_cache) {
-    $items[] = array(
-      'path' => 'admin/ecsettings/role_price',
-      'title' => t('Role price'),
-      'description' => t('Set which roles will be eligible for special price.'),
-      'callback' => 'drupal_get_form',
-      'callback arguments' => array('role_price_settings'),
-      'access' => user_access('administer store')
-    );
-    
-  }
-  return $items;
+function role_price_theme($existing, $type, $theme, $path) {
+  return array(
+    'role_price' => array(
+      'arguments' => array('role', 'price'),
+    )
+  );
 }
 
+
 /********************************************************************
  * Ecommerce hooks
  ********************************************************************/
 
 
 function role_price_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
-  //var_dump($op);
   global $user;
   switch ($op) {
     case 'submit':
-      
+
       break;
     case 'insert':
-    
+
       if ($node->type == 'product') {
         // save default price
-        db_query("INSERT INTO {ec_role_price} (nid, rid, price) VALUES ('%d', '%d', '%s')", $node->nid, 0, $node->price);
+        db_query("INSERT INTO {ec_role_price} (nid, rid, price) VALUES ('%d', '%d', '%s')", $node->nid, 1, $node->price);
         //save role price
-        if (is_array($node->role_price)) {
-          foreach ($node->role_price as $key => $value) {
-            db_query("INSERT INTO {ec_role_price} (nid, rid, price) VALUES ('%d', '%d', '%s')", $node->nid, $key, $value);
-          }
+        foreach ($node->role_price as $key => $value) {
+          db_query("INSERT INTO {ec_role_price} (nid, rid, price) VALUES ('%d', '%d', '%s')", $node->nid, $key, $value);
         }
       }
-      
+
       break;
     case 'update':
-    
+
       if ($node->type == 'product') {
         // delete all node prices so we don't have to worry about updating as we will insert again all prices later
         db_query("DELETE FROM {ec_role_price} WHERE nid = %d", $node->nid);
         // save default price
-        db_query("INSERT INTO {ec_role_price} (nid, rid, price) VALUES ('%d', '%d', '%s')", $node->nid, 0, $node->price);
+        db_query("INSERT INTO {ec_role_price} (nid, rid, price) VALUES ('%d', '%d', '%s')", $node->nid, 1, $node->price);
         //save role price
-        if (is_array($node->role_price)) {
-          foreach ($node->role_price as $key => $value) {
-            db_query("INSERT INTO {ec_role_price} (nid, rid, price) VALUES ('%d', '%d', '%s')", $node->nid, $key, $value);
-          }
+        foreach ($node->role_price as $key => $value) {
+          db_query("INSERT INTO {ec_role_price} (nid, rid, price) VALUES ('%d', '%d', '%s')", $node->nid, $key, $value);
         }
       }
-      
+
       break;
     case 'view':
       $show_prices = FALSE;
       $show_roles = variable_get('role_price_show_prices', array());
-      $active_roles = variable_get('role_price_roles', array());
       foreach ($user->roles as $key => $value) {
         if ($show_roles[$key] > 0) {
           $show_prices = TRUE;
         }
       }
+
       if (is_array($node->role_price) && $show_prices) {
         foreach ($node->role_price as $key => $price) {
           $roles = user_roles();
           foreach ($roles as $keyrole => $role) {
-            if (($key == $keyrole) && in_array($keyrole, $active_roles)) {
+            if ($key == $keyrole) {
+              if($keyrole == 1) {
+                $role = t('normal');
+              }
               $node->content['price '. $role] = array(
                 '#value' => theme('role_price', $role, $price),
                 '#weight' => 10,
@@ -112,55 +104,93 @@ function role_price_nodeapi(&$node, $op,
             }
           }
         }
-        $node->content['base price'] = array(
-          '#value' => theme('role_price', '', $node->role_price[0]),
-          '#weight' => 9,
-          );      
       }
       break;
     case 'load':
-    
       if ($node->type == 'product') {
+        $org_price = db_result(db_query('SELECT price FROM {ec_product} WHERE nid = %d', $node->nid));
         $query = db_query('SELECT rid, price FROM {ec_role_price} WHERE nid = %d', $node->nid);
         while ($price = db_fetch_object($query)) {
           $role_price[$price->rid] = $price->price;
         }
+
+        $roles = variable_get('role_price_roles', array());
+
+        // If no role prices are set then load the original product price for all roles
+        if(!isset($role_price)) {
+          $role_price[1] = $org_price;
+          foreach ($roles as $key => $value) {
+            if($value) {
+              $role_price[$key] = $org_price;
+            }
+          }
+        }
+
         $node->role_price = $role_price;
+        asort($roles);
+        $price_set = 0;
+        foreach ($roles as $key => $value) {
+          //if($
+          if ($value != 0 && $price_set != 1) {
+            if (array_key_exists($key, $user->roles)) {
+              $node->price = $role_price[$key];
+              $price_set = 1;
+            }
+            else {
+              $node->price = $role_price[1];
+            }
+          }
+        }
       }
       break;
-    
-    case 'delete':
-      db_query("DELETE FROM {ec_role_price} WHERE nid = %d", $node->nid);
-      break;
-      
   }
 }
 
-function role_price_form_alter($form_id, &$form) {
+function role_price_form_alter(&$form, $form_state, $form_id) {
   if ($form['type']['#value'] == 'product') {
     $node = $form['#node'];
+    unset($form['product']['price']);
+    //var_dump($node);
     if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {
       $roles = user_roles();
       asort($roles);
       $roles_to_apply = variable_get('role_price_roles', array());
 
-    $form['product']['role_price'] = array(
+    $form['product']['prices'] = array(
         '#type' => 'fieldset',
-        '#title' => t('Prices per role'),
+        '#title' => t('Prices'),
         '#collapsible' => TRUE,
         '#collapsed' => FALSE,
         '#weight' => -20,
-        '#tree' => TRUE
+        '#tree' => FALSE
+      );
+    $form['product']['prices']['price'] = array(
+      '#type' => 'price',
+      '#title' => t('Price'),
+      '#size' => 25,
+      '#maxlength' => 50,
+      '#required' => TRUE,
+      '#default_value' => role_price_payment_format($node->role_price[1]),
+      '#description' => t('This price will only apply to users whose role is none of the listed in the Price per Role fieldset down here.'),
+      '#weight' => -20,
+    );
+      $form['product']['prices']['role_price'] = array(
+        '#type' => 'fieldset',
+        '#title' => t('Price per role'),
+        '#collapsible' => TRUE,
+        '#collapsed' => FALSE,
+        '#tree' => TRUE,
+        '#weight' => 1
       );
       foreach ($roles as $key => $value) {
         if ($roles_to_apply[$key] != 0) {
-          $form['product']['role_price'][$key] = array(
+          $form['product']['prices']['role_price'][$key] = array(
             '#type' => 'price',
             '#title' => t('Price for role !role', array('!role' => t('!r', array('!r' => $value)))),
             '#size' => 25,
             '#maxlength' => 50,
             '#required' => TRUE,
-            '#default_value' => module_invoke('payment', 'format', $node->role_price[$key]),
+            '#default_value' => role_price_payment_format($node->role_price[$key]),
             '#description' => t('How much does this product retail for? Note: This price may be different from the selling price due to price adjustments elsewhere.'),
           );
         }
@@ -180,11 +210,25 @@ function role_price_product_specials($no
   else {
     $account = $user;
   }
+  $org_price = db_result(db_query('SELECT price FROM {ec_product} WHERE nid = %d', $node->nid));
   $query = db_query('SELECT rid, price FROM {ec_role_price} WHERE nid = %d', $node->nid);
   while ($price = db_fetch_object($query)) {
     $role_price[$price->rid] = $price->price;
   }
+
   $roles = variable_get('role_price_roles', array());
+
+  // If no role prices are set then load the original product price for all roles
+  if(!isset($role_price)) {
+    $role_price[1] = $org_price;
+    foreach ($roles as $key => $value) {
+      if($value) {
+        $role_price[$key] = $org_price;
+      }
+    }
+  }
+
+  asort($roles);
   $price_set = 0;
   foreach ($roles as $key => $value) {
     if ($value != 0 && $price_set != 1) {
@@ -193,12 +237,12 @@ function role_price_product_specials($no
         $price_set = 1;
       }
       else {
-        $final_price = $role_price[0];
+        $final_price = $role_price[1];
       }
     }
   }
-  $adjustment = ($final_price >= $role_price[0]) ?  $final_price - $role_price[0] : -($role_price[0] - $final_price);
-  return array('price_role' => $adjustment);
+  $adjustment = ($final_price < $node->price) ? -($node->price - $final_price) : $final_price - $node->price;
+  return array('price' => $adjustment);
 }
 
 
@@ -213,22 +257,20 @@ function role_price_settings() {
   }
   $roles = $role;
   $form['role_price_roles'] = array(
-    '#type' => 'select',
+    '#type' => 'checkboxes',
     '#title' => t('Roles'),
     '#default_value' => variable_get('role_price_roles', array()),
     '#options' => $roles,
-    '#multiple' => TRUE,
     '#description' => t('Roles to which different product prices can be applied. Not selected roles will have a default price you will set for each product during product creation. For users with more than one role assigned: the price finally assigned will be of the role shown closer to the top of this list.'),
   );
   $form['role_price_show_prices'] = array(
-    '#type' => 'select',
+    '#type' => 'checkboxes',
     '#title' => t('Roles to show all the prices'),
     '#description' => t('Check the boxes for each role you want to show all the prices.'),
     '#options' => $roles,
-    '#multiple' => TRUE,
     '#default_value' => variable_get('role_price_show_prices', array()),
   );
-  
+
   return system_settings_form($form);
 }
 
@@ -236,7 +278,16 @@ function role_price_settings() {
 function theme_role_price($role, $price) {
   $output = '';
   $output .= '<div class="role_price">';
-  $output .= '<span class="role_price_role">'. t('Price') .' '. t($role) .'</span>: <span class="role_price_price">'. module_invoke('payment', 'format', $price) .'</span>';
+  $output .= '<span class="role_price_role">'. t('Price') .' '. t($role) .'</span>: <span class="role_price_price">'. role_price_payment_format($price) .'</span>';
   $output .= '</div>';
+
   return $output;
 }
+
+function role_price_payment_format($price) {
+  $price = number_format((float) $price,
+    variable_get('payment_decimal_places', 2),
+    variable_get('payment_decimal', '.'),
+    variable_get('payment_thousands', ','));
+  return (variable_get('payment_symbol_position', 1) == 1) ? variable_get('payment_symbol', '$') . $price : $price . variable_get('payment_symbol', '$');
+}
