Index: README.txt
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/uc_multiprice/README.txt,v
retrieving revision 1.2
diff -u -r1.2 README.txt
--- README.txt	20 Mar 2009 18:34:21 -0000	1.2
+++ README.txt	7 Jun 2009 13:23:15 -0000
@@ -4,7 +4,7 @@
 A basic but powerfull module for Ubercart that allows to overide the product price based on a country.
 The module provides a block where the user can select a country/region. This selection is then used to select the correct price.
 
-If the module ip2cc is enabled it will be used to get the country code from the user by IP. (block selection will overide this).
+If enabled it will use ip2cc module or ip2country to get the country code from the user by IP. (block selection will overide this).
 
 
 Author
Index: uc_multiprice.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/uc_multiprice/uc_multiprice.info,v
retrieving revision 1.2
diff -u -r1.2 uc_multiprice.info
--- uc_multiprice.info	21 Apr 2009 18:11:50 -0000	1.2
+++ uc_multiprice.info	7 Jun 2009 13:21:11 -0000
@@ -1,5 +1,6 @@
+; $Id$
 name = Multiprice
 description = Allows price overide on a country level
-package = "Ubercart - core (optional)"
+package = "Ubercart - extra"
 dependencies[] = uc_product
 core = 6.x
\ No newline at end of file
Index: uc_multiprice.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/uc_multiprice/uc_multiprice.install,v
retrieving revision 1.1
diff -u -r1.1 uc_multiprice.install
--- uc_multiprice.install	19 Mar 2009 15:06:29 -0000	1.1
+++ uc_multiprice.install	7 Jun 2009 13:21:21 -0000
@@ -1,4 +1,5 @@
 <?php
+// $Id$
 
 /**
  * @file
@@ -52,6 +53,16 @@
 	return $schema;
 }
 
+/**
+ * Implementation of hook_install().
+ */
 function uc_multiprice_install() {
   drupal_install_schema('uc_multiprice');
 }
+
+/**
+ * Implementation of hook_uninstall().
+ */
+function uc_multiprice_uninstall() {
+  drupal_uninstall_schema('uc_multiprice');
+}
\ No newline at end of file
Index: uc_multiprice.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/uc_multiprice/uc_multiprice.module,v
retrieving revision 1.6
diff -u -r1.6 uc_multiprice.module
--- uc_multiprice.module	13 May 2009 08:29:52 -0000	1.6
+++ uc_multiprice.module	7 Jun 2009 13:44:24 -0000
@@ -1,4 +1,6 @@
 <?php
+// $Id$
+
 /**
  * @file Provides Multiprice options
  */
@@ -11,134 +13,145 @@
  * Implementation of hook_init().
  */
 function uc_multiprice_init(){
-	uc_multiprice_country_id();
+  uc_multiprice_country_id();
 }
 
 /**
  * Implementation of hook_menu().
  */
 function uc_multiprice_menu(){
-	  $items['uc_multiprice/js'] = array(
+  $items['uc_multiprice/js'] = array(
     'page callback' => 'uc_multiprice_js',
     'access arguments' => array('administer nodes'),
     'type' => MENU_CALLBACK,
   );
-	return $items;
+  /*
+  $items['admin/store/settings/multiprice'] = array(
+    'title' => 'Multiprice Settings',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('uc_multiprice_settings'),
+    'access arguments' => array('administer store'),
+    'type' => MENU_NORMAL_ITEM,
+  );
+  */
+  return $items;
 }
 
 /**
  * Implementation of hook_form_alter().
  */
 function uc_multiprice_form_alter(&$form, $form_state, $form_id) { 
-	if (uc_product_is_product_form($form)) {
-		$form['base']['multiprice'] = array(
-				'#type' => 'fieldset',
-				'#title' => t('Product multi pricing'),
-				'#description' => t('You can override the global pricing by country.'),
-				'#tree' => TRUE,
-				'#weight' => 6,
-				'#collapsible' => TRUE,
-      	'#collapsed' => FALSE,
-		);	 
-		$form['base']['multiprice']['countries'] = array(
-				'#type' => 'fieldset',
-				'#tree' => TRUE,
-				'#weight' => 3,
-				'#collapsible' => FALSE,
-      	'#collapsed' => FALSE,
-				'#prefix' => '<div id="multiprice-countries">',
-    		'#suffix' => '</div>',
-				'#theme' => 'uc_multiprice_form',
-		);
-
-		// Get all multiprices
-		$result = db_query("SELECT 
-											 		c.country_id, c.country_name, m.nid, m.sell_price, m.cost, m.list_price 
-												FROM 
-													{uc_countries} c 
-												LEFT JOIN 
-													{uc_multiprice} m 
-												ON 
-													c.country_id = m.country_id 
-												WHERE 
-													c.version > 0 
-												AND 
-													m.nid = %d 
-												ORDER BY 
-													c.country_name ASC", $form['nid']['#value']);
-		$exist = array();
-		while ($country = db_fetch_object($result)) {
-			$form['base']['multiprice']['countries'] += uc_multiprice_field_element($country);
-			$exist[] = $country->country_id;
-		}
-			
-			$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)) {
-				if(!in_array($country->country_id, $exist))
-					$countries[$country->country_id] = $country->country_name;	
-			}
-			
-		 	$form['base']['multiprice']['more'] = array(
-			'#type' => 'select',
-			'#description' => t("Select the country/region to add a price overide."),
-			'#options' => $countries,
-			'#weight' => 1,
-			'#ahah' => array(
-				'path' => 'uc_multiprice/js',
-				'wrapper' => 'multiprice-countries',
-				'method' => 'replace',
-				'effect' => 'slide',
-				),
-  	);
-		$form['#validate'][] = 'uc_multiprice_form_validate';
-
-	}
-	
-	// Checkout form
-	if($form_id=='uc_cart_checkout_form') {
-		$id = uc_multiprice_country_id();
-		$form['panes']['delivery']['delivery_country']['#default_value'] = $id;
-		$form['panes']['delivery']['delivery_country']['#attributes'] = array('disabled' => 'disabled');	
-		drupal_add_js('$(document).ready( function() { $(\'#edit-panes-delivery-delivery-country\').val(\''.$id.'\').trigger(\'change\'); });', 'inline');
-	}
+  if (uc_product_is_product_form($form)) {
+    $form['base']['multiprice'] = array(
+        '#type' => 'fieldset',
+        '#title' => t('Product multi pricing'),
+        '#description' => t('You can override the global pricing by country.'),
+        '#tree' => TRUE,
+        '#weight' => 6,
+        '#collapsible' => TRUE,
+        '#collapsed' => FALSE,
+    );   
+    $form['base']['multiprice']['countries'] = array(
+        '#type' => 'fieldset',
+        '#tree' => TRUE,
+        '#weight' => 3,
+        '#collapsible' => FALSE,
+        '#collapsed' => FALSE,
+        '#prefix' => '<div id="multiprice-countries">',
+        '#suffix' => '</div>',
+        '#theme' => 'uc_multiprice_form',
+    );
+
+    // Get all multiprices
+    $result = db_query("SELECT 
+                           c.country_id, c.country_name, m.nid, m.sell_price, m.cost, m.list_price 
+                        FROM 
+                          {uc_countries} c 
+                        LEFT JOIN 
+                          {uc_multiprice} m 
+                        ON 
+                          c.country_id = m.country_id 
+                        WHERE 
+                          c.version > 0 
+                        AND 
+                          m.nid = %d 
+                        ORDER BY 
+                          c.country_name ASC", $form['nid']['#value']);
+    $exist = array();
+    while ($country = db_fetch_object($result)) {
+      $form['base']['multiprice']['countries'] += uc_multiprice_field_element($country);
+      $exist[] = $country->country_id;
+    }
+      
+      $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)) {
+        if(!in_array($country->country_id, $exist))
+          $countries[$country->country_id] = $country->country_name;  
+      }
+      
+       $form['base']['multiprice']['more'] = array(
+      '#type' => 'select',
+      '#description' => t("Select the country/region to add a price overide."),
+      '#options' => $countries,
+      '#weight' => 1,
+      '#ahah' => array(
+        'path' => 'uc_multiprice/js',
+        'wrapper' => 'multiprice-countries',
+        'method' => 'replace',
+        'effect' => 'slide',
+        ),
+    );
+    $form['#validate'][] = 'uc_multiprice_form_validate';
+
+  }
+  
+  // Checkout form
+  if($form_id=='uc_cart_checkout_form') {
+    $id = uc_multiprice_country_id();
+    $form['panes']['delivery']['delivery_country']['#default_value'] = $id;
+    $form['panes']['delivery']['delivery_country']['#attributes'] = array('disabled' => 'disabled');  
+    drupal_add_js('$(document).ready( function() { $(\'#edit-panes-delivery-delivery-country\').val(\''.$id.'\').trigger(\'change\'); });', 'inline');
+  }
 }
 
 /**
  * Implementation of hook_nodeapi().
  */
-function uc_multiprice_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL){
-	if($node->type == 'product' || $node->type == 'product_kit'){
-		switch ($op) {
-			case 'load':
-				// load multprice and add to node object
-				$result = db_query("SELECT * FROM {uc_multiprice} WHERE nid = %d", $node->nid);
-				while($row = db_fetch_object($result)) {
-					$node->multiprice[$row->country_id] = $row;
+function uc_multiprice_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
+  if(uc_product_is_product($node)) {
+    switch ($op) {
+      case 'load':
+        // Add to node object
+        $result = db_query("SELECT * FROM {uc_multiprice} WHERE nid = %d", $node->nid);
+        while($row = db_fetch_object($result)) {
+          $node->multiprice[$row->country_id] = $row;
+        }
+        // Replace product price with multiprice if it's not node form.
+        if ((arg(2) != 'edit') && (arg(2) != 'add')) {
+          $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);
+          }
 				}
-				// replace product price with multiprice
-				$cid = uc_multiprice_country_id();
-				if($node->multiprice[$cid]){
-					$node->sell_price = $node->multiprice[$cid]->sell_price;
-					$node->list_price = $node->multiprice[$cid]->list_price;
-					$node->cost 			= $node->multiprice[$cid]->cost;
-				}
-			break;
-			case 'insert':
+      break;
+      case 'insert':
       case 'update':
-				db_query("DELETE FROM {uc_multiprice} WHERE nid = %d", $node->nid);
+        db_query("DELETE FROM {uc_multiprice} WHERE nid = %d", $node->nid);
        
-			 if(count($node->multiprice['countries'])==0)
-					break;
-				
-				foreach($node->multiprice['countries'] as $country_id=>$country){
-						if(empty($country->delete)){
-							db_query("INSERT INTO {uc_multiprice} (nid, country_id, list_price, cost, sell_price) VALUES (%d, %d, %f, %f, %f)",
-								$node->nid, $country_id, $country['list_price'], $country['cost'], $country['sell_price']);
-						}
-				}
+       if(count($node->multiprice['countries'])==0)
+          break;
+        
+        foreach($node->multiprice['countries'] as $country_id=>$country){
+            if(empty($country->delete)){
+              db_query("INSERT INTO {uc_multiprice} (nid, country_id, list_price, cost, sell_price) VALUES (%d, %d, %f, %f, %f)",
+                $node->nid, $country_id, $country['list_price'], $country['cost'], $country['sell_price']);
+            }
+        }
       break;
-		}
-	}
+    }
+  }
 }
 
 /**
@@ -161,10 +174,10 @@
       break;
     case 'view':
       if ($delta == 0) {
-				$block = array(
-					'subject' => t('Country'),
-					'content' => drupal_get_form('uc_multiprice_region_form'),
-				);
+        $block = array(
+          'subject' => t('Country'),
+          'content' => drupal_get_form('uc_multiprice_region_form'),
+        );
         return $block;
       }
       break;
@@ -180,23 +193,25 @@
  * Avaiable for other modules
  */
 function uc_multiprice_country_id($country_id = FALSE) {
-	if($country_id){
-		$_SESSION['country_id'] = $country_id;
-	}
-	
-	// Select country by IP if not already set and ip2cc is avaible
-	if(empty($_SESSION['country_id'])) {
-		// IP by ip2cc
-		if(module_exists('ip2cc')) {
-			$country = ip2cc_get_country(ip2cc_get_user_ip_address());
-			$_SESSION['country_id'] = $country->country_number;
-		}
-		// Not, use default country
-		if(empty($country)) {
-			$_SESSION['country_id'] = uc_store_default_country();
-		}
-	}
-	return $_SESSION['country_id'];
+  if($country_id){
+    $_SESSION['country_id'] = $country_id;
+  }
+  
+  // Get Country by IP if not set
+  if(empty($_SESSION['country_id'])) {
+    if (module_exists('ip2country')) {  // IP by ip2country
+      $_SESSION['country_code'] = ip2country_get_country(ip_address());
+    }else
+    if(module_exists('ip2cc')) { // IP by 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();
+    }
+  }
+  return $_SESSION['country_id'];
 }
 
 function uc_multiprice_theme() {
@@ -208,16 +223,16 @@
 }
 
 function theme_uc_multiprice_form($form) {
-	$output = '';
-	$header = array(t('Country'), t('List price'), t('Cost'), t('Sell price'), t('Delete'));
+  $output = '';
+  $header = array(t('Country'), t('List price'), t('Cost'), t('Sell price'), t('Delete'));
   $row = array();
   foreach (element_children($form) as $country_id) {
-		$row = array();
+    $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']);
+    $row[] = drupal_render($form[$country_id]['sell_price']);
+    $row[] = drupal_render($form[$country_id]['delete']);
     $rows[] = $row;
   }
   $output .= theme('table', $header, $rows, array('id' => 'uc_multiprice_table'));
@@ -225,57 +240,57 @@
 }
 
 function uc_multiprice_field_element($country){
-	$field[$country->country_id]['country'] = array(
-		'#type' => 'hidden',
-		'#required' => FALSE,
-		'#default_value' => $country->country_name,
-	);
-	$field[$country->country_id]['list_price'] = array(
-		'#type' => 'textfield',
-		'#required' => FALSE,
-		'#default_value' => isset($country->list_price) ? $country->list_price : 0.00,
-		'#weight' => 0,
-		'#size' => 20,
-		'#maxlength' => 35,
-	);
-	$field[$country->country_id]['cost'] = array(
-		'#type' => 'textfield',
-		'#required' => FALSE,
-		'#default_value' => isset($country->cost) ? $country->cost : 0.00,
-		'#weight' => 1,
-		'#size' => 20,
-		'#maxlength' => 35,
-	);
-	$field[$country->country_id]['sell_price'] = array(
-		'#type' => 'textfield',
-		'#required' => FALSE,
-		'#weight' => 2,
-		'#default_value' => isset($country->sell_price) ? $country->sell_price : 0.00,
-		'#size' => 20,
-		'#maxlength' => 35,
-	);
-	$field[$country->country_id]['delete'] = array(
-		'#type' => 'checkbox',
-		'#required' => FALSE,
-	);
-	return $field;
+  $field[$country->country_id]['country'] = array(
+    '#type' => 'hidden',
+    '#required' => FALSE,
+    '#default_value' => $country->country_name,
+  );
+  $field[$country->country_id]['list_price'] = array(
+    '#type' => 'textfield',
+    '#required' => FALSE,
+    '#default_value' => isset($country->list_price) ? check_plain($country->list_price) : 0.00,
+    '#weight' => 0,
+    '#size' => 20,
+    '#maxlength' => 35,
+  );
+  $field[$country->country_id]['cost'] = array(
+    '#type' => 'textfield',
+    '#required' => FALSE,
+    '#default_value' => isset($country->cost) ? check_plain($country->cost) : 0.00,
+    '#weight' => 1,
+    '#size' => 20,
+    '#maxlength' => 35,
+  );
+  $field[$country->country_id]['sell_price'] = array(
+    '#type' => 'textfield',
+    '#required' => FALSE,
+    '#weight' => 2,
+    '#default_value' => isset($country->sell_price) ? check_plain($country->sell_price) : 0.00,
+    '#size' => 20,
+    '#maxlength' => 35,
+  );
+  $field[$country->country_id]['delete'] = array(
+    '#type' => 'checkbox',
+    '#required' => FALSE,
+  );
+  return $field;
 }
 
 function uc_multiprice_js() {
-	if(!$_POST){
-		print "fake";
-		exit;
-	}
-	
-	// new field
-	$country_id = check_plain($_POST['multiprice']['more']);
-	$country = db_fetch_object(db_query("SELECT * FROM {uc_countries} WHERE country_id = %d", $country_id));
-	$field = uc_multiprice_field_element($country);
+  if(!$_POST){
+    print "fake";
+    exit;
+  }
+  
+  // new field
+  $country_id = check_plain($_POST['multiprice']['more']);
+  $country = db_fetch_object(db_query("SELECT * FROM {uc_countries} WHERE country_id = %d", $country_id));
+  $field = uc_multiprice_field_element($country);
 
-	$form_state = array('submitted' => FALSE);
+  $form_state = array('submitted' => FALSE);
   $form_build_id = $_POST['form_build_id'];
 
-	// Add the new element to the stored form. Without adding the element to the
+  // Add the new element to the stored form. Without adding the element to the
   // form, Drupal is not aware of this new elements existence and will not
   // process it. We retreive the cached form, add the element, and resave.
   $form = form_get_cache($form_build_id, $form_state);
@@ -286,36 +301,36 @@
     '#programmed' => FALSE,
   );
   // Rebuild the form. 
-	$form = form_builder($_POST['form_id'], $form, $form_state);
-	
-	// return correct part
-	unset($form['base']['multiprice']['countries']['prefix']);
-	unset($form['base']['multiprice']['countries']['suffix']);
-	$output = drupal_render($form['base']['multiprice']['countries']);
-	
-	print drupal_to_js(array('status' => TRUE, 'data' => $output));
+  $form = form_builder($_POST['form_id'], $form, $form_state);
+  
+  // return correct part
+  unset($form['base']['multiprice']['countries']['prefix']);
+  unset($form['base']['multiprice']['countries']['suffix']);
+  $output = drupal_render($form['base']['multiprice']['countries']);
+  
+  print drupal_to_js(array('status' => TRUE, 'data' => $output));
   exit;
 }
 
 function uc_multiprice_region_form($form_state){
-	$result = db_query("SELECT * FROM {uc_countries} WHERE version > 0 ORDER BY country_name ASC");
-	while ($country = db_fetch_object($result)) {
-	$countries[$country->country_id] = $country->country_name;
-	}
-
-	$form['country_id'] = array(
-	'#type' => 'select',
-	'#default_value' => uc_multiprice_country_id(),
-	'#options' => $countries,
-	'#attributes' => array('onchange' => 'javascript:this.form.submit();'),
-	);
-	
-	// hide the submit button
-	$form['submit'] = array('#type' => 'submit', '#value' => t('Save'),'#attributes' => array('style'=>'display:none;'));
-	return $form;	
+  $result = db_query("SELECT * FROM {uc_countries} WHERE version > 0 ORDER BY country_name ASC");
+  while ($country = db_fetch_object($result)) {
+  $countries[$country->country_id] = $country->country_name;
+  }
+
+  $form['country_id'] = array(
+  '#type' => 'select',
+  '#default_value' => uc_multiprice_country_id(),
+  '#options' => $countries,
+  '#attributes' => array('onchange' => 'javascript:this.form.submit();'),
+  );
+  
+  // hide the submit button
+  $form['submit'] = array('#type' => 'submit', '#value' => t('Save'),'#attributes' => array('style'=>'display:none;'));
+  return $form;  
 }
 
 // save selection to session
 function uc_multiprice_region_form_submit($form, &$form_state){
-	uc_multiprice_country_id($form_state['values']['country_id']);
+  uc_multiprice_country_id($form_state['values']['country_id']);
 }
\ No newline at end of file

