Index: uc_vat.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/uc_vat/uc_vat.module,v
retrieving revision 1.48
diff -u -p -r1.48 uc_vat.module
--- uc_vat.module	19 Mar 2010 06:12:18 -0000	1.48
+++ uc_vat.module	17 Sep 2010 10:19:48 -0000
@@ -93,7 +93,7 @@ function uc_vat_theme($existing, $type, 
 function uc_vat_form_alter(&$form, $form_state) {
   if (uc_product_is_product_form($form)) {
     // Add VAT to product prices before editing.
-    $taxes = uc_taxes_rate_load();
+    $taxes = uc_vat_load_taxes();
     if ($fields = _uc_vat_product_fields()) {
       foreach ($fields as $field) {
         $value = $form['base']['prices'][$field]['#default_value'];
@@ -135,7 +135,7 @@ function uc_vat_form_alter(&$form, $form
  * Remove VAT from product prices after editing but before saving.
  */
 function uc_vat_uc_product_form_submit($form, &$form_state) {
-  $taxes = uc_taxes_rate_load();
+  $taxes = uc_vat_load_taxes();
   $fields = _uc_vat_product_fields();
 
   foreach ($fields as $field) {
@@ -174,7 +174,7 @@ function uc_vat_form_uc_order_edit_form_
 
 function uc_vat_form_uc_order_edit_products_form_alter(&$form, $form_state) {
   $order = uc_order_load($form['#parameters'][2][0]->order_id);
-  $taxes = isset($order->data['taxes']) ? $order->data['taxes'] : uc_taxes_rate_load();
+  $taxes = isset($order->data['taxes']) ? $order->data['taxes'] : uc_vat_load_taxes();
 
   // Use attribute field names as the true price field is named just "price" not "sell price".
   if ($fields = _uc_vat_attribute_fields()) {
@@ -212,7 +212,7 @@ function uc_vat_uc_order_edit_form_submi
 }
 
 function _uc_vat_uc_order_edit_products_submit($order) {
-  $taxes = isset($order->data['taxes']) ? $order->data['taxes'] : uc_taxes_rate_load();
+  $taxes = isset($order->data['taxes']) ? $order->data['taxes'] : uc_vat_load_taxes();
   $fields = _uc_vat_attribute_fields();
 
   // Modify $_POST directly, as products are subform elements injected into the form, not real Form API elements.
@@ -246,7 +246,7 @@ function uc_vat_form_uc_attribute_option
           'attribute' => $attribute,
         ),
       );
-      $taxes = uc_taxes_rate_load();
+      $taxes = uc_vat_load_taxes();
 
       foreach ($attribute->options as $oid => $option) {
         foreach ($fields as $field) {
@@ -283,7 +283,7 @@ function uc_vat_form_uc_attribute_option
     if (variable_get('uc_vat_default_attributes_inclusive', FALSE)) {
       $attribute = uc_attribute_load($form['aid']['#value']);
       $option = $attribute->options[$form['oid']['#value']];
-      $taxes = uc_taxes_rate_load();
+      $taxes = uc_vat_load_taxes();
 
       foreach ($fields as $field) {
         $value = $option->$field;
@@ -313,7 +313,7 @@ function uc_vat_form_uc_attribute_option
  * Remove VAT from default option prices.
  */
 function uc_vat_uc_attribute_option_form_submit($form, &$form_state) {
-  $taxes = uc_taxes_rate_load();
+  $taxes = uc_vat_load_taxes();
 
   foreach (_uc_vat_attribute_fields() as $field) {
     $value = $form_state['values'][$field];
@@ -339,7 +339,7 @@ function uc_vat_form_uc_object_options_f
     // This form is used for products and product classes, and we need to know the node type.
     $object = $form['#parameters'][2];
     $type = $form['#parameters'][3] == 'product' ? $object->type : $object->pcid;
-    $taxes = uc_taxes_rate_load();
+    $taxes = uc_vat_load_taxes();
 
     foreach (element_children($form['attributes']) as $aid) {
       foreach (element_children($form['attributes'][$aid]['options']) as $oid) {
@@ -366,7 +366,7 @@ function uc_vat_form_uc_object_options_f
 function uc_vat_uc_object_options_form_submit($form, &$form_state) {
   $object = $form['#parameters'][2];
   $type = $form['#parameters'][3] == 'product' ? $object->type : $object->pcid;
-  $taxes = uc_taxes_rate_load();
+  $taxes = uc_vat_load_taxes();
 
   foreach (element_children($form['attributes']) as $aid) {
     foreach (element_children($form['attributes'][$aid]['options']) as $oid) {
@@ -436,7 +436,7 @@ function uc_vat_order($op, $arg1, $arg2)
     case 'save':
       $data = unserialize(db_result(db_query("SELECT data FROM {uc_orders} WHERE order_id = %d", $arg1->order_id)));
       if (!isset($data['taxes'])) {
-        $data['taxes'] = uc_vat_load_order_taxes($arg1, user_load($arg1->uid));
+        $data['taxes'] = uc_vat_load_taxes($arg1, user_load($arg1->uid));
         db_query("UPDATE {uc_orders} SET data = '%s' WHERE order_id = %d", serialize($data), $arg1->order_id);
       }
   }
@@ -454,13 +454,9 @@ function uc_vat_price_handler_alter(&$pr
     // User has "show prices without VAT" permission.
     $tax_rates = array();
   }
-  elseif ($context['type'] == 'order_product') {
-    // For products in new orders, load taxes according to conditional actions rules.
-    $tax_rates = uc_vat_load_order_taxes($context['subject']['order'], $context['account']);
-  }
   else {
-    // Load all available tax rates.
-    $tax_rates = uc_taxes_rate_load();
+    // Load tax rates according to conditional actions rules, using context where available.
+    $tax_rates = uc_vat_load_taxes($context['subject']['order'], $context['account']);
   }
 
   switch ($context['type']) {
@@ -490,10 +486,42 @@ function uc_vat_price_handler_alter(&$pr
 
 /**
  * Apply conditional actions to an order and return only the active set of taxes.
+ * If no order is supplied, make a reasonable guess as to which country the user is in.
  */
-function uc_vat_load_order_taxes($order, $account = NULL) {
+function uc_vat_load_taxes($order = NULL, $account = NULL) {
   global $user;
- 
+
+  // Use the current user if none was supplied.
+  if (!$account) {
+    $account = $user;
+  }
+
+  // Build a fake order object if none was supplied.
+  if (!$order) {
+    $order = new stdClass();
+    $order->uid = $account->uid;
+    $order->order_id = 0;
+    $order->order_status = uc_order_state_default('in_checkout');
+  }
+
+  // Ensure we have a country code for the order.
+  if (!isset($order->billing_country) || !isset($order->delivery_country)) {
+    // Default to store country.
+    $country_id = variable_get('uc_store_country', 840);
+
+    // TODO: find alternative ways of guessing the country if we do not know it
+    // - If user is logged in, look for previous completed orders and assume the same country will be used?
+    // - If that fails or user is not logged in, use geolocation if available?
+
+    if (!isset($order->billing_country)) {
+      $order->billing_country = $country_id;
+    }
+    if (!isset($order->delivery_country)) {
+      $order->delivery_country = $country_id;
+    }
+  }
+
+  // Run CA rules against the order.
   $predicates = ca_load_trigger_predicates('calculate_taxes');
   $arguments = array(
     'order' => array(
@@ -508,10 +536,11 @@ function uc_vat_load_order_taxes($order,
     'account' => array(
       '#entity' => 'user',
       '#title' => t('User'),
-      '#data' => $account ? $account : $user,
+      '#data' => $account,
     ),
   );
 
+  // Only use taxes that match the CA rules.
   $taxes = uc_taxes_rate_load();
   foreach ($taxes as $id => $tax) {
     $arguments['tax']['#data'] = $tax;
@@ -572,7 +601,6 @@ function uc_vat_product_price_alter(&$pr
 function uc_vat_line_item_price_alter(&$price, &$context, &$options, $tax_rates) {
   $order = $context['subject']['order'];
   $line_item = (array)$context['subject']['line_item'];
-  $taxes = uc_taxes_calculate($order);
 
   // If we are in the order preview pane at checkout, the tax conditions may have changed,
   // so force recalculation of the subtotal line item here.
@@ -582,19 +610,16 @@ function uc_vat_line_item_price_alter(&$
 
   // Add the taxes that satisfy the conditions and apply to this line item
   // to its amount.
-  foreach ($taxes as $tax) {
-    if (isset($tax_rates[$tax->id])) {
-      $tax_rate = $tax_rates[$tax->id];
-      $taxed_line_items = $tax_rate->taxed_line_items;
-      if (is_array($taxed_line_items) && in_array($line_item['type'], $taxed_line_items)) {
-        if ($line_item['type'] == 'tax' && $line_item['weight'] >= $tax->weight) {
-          continue;
-        }
-
-        // The total tax amount comes from many sources, so only add the tax
-        // that was applied to this line item.
-        $price['price'] += $line_item['amount'] * $tax_rate->rate;
+  foreach ($tax_rates as $tax_rate) {
+    $taxed_line_items = $tax_rate->taxed_line_items;
+    if (is_array($taxed_line_items) && in_array($line_item['type'], $taxed_line_items)) {
+      if ($line_item['type'] == 'tax' && $line_item['weight'] >= $tax_rate->weight) {
+        continue;
       }
+
+      // The total tax amount comes from many sources, so only add the tax
+      // that was applied to this line item.
+      $price['price'] += $line_item['amount'] * $tax_rate->rate;
     }
   }
 }
@@ -736,7 +761,7 @@ function uc_vat_tapir_table_alter(&$tabl
  * Add VAT subtotals to cart table.
  */
 function uc_vat_uc_cart_view_table_alter(&$table) {
-  $taxes = uc_taxes_rate_load();
+  $taxes = uc_vat_load_taxes();
 
   $subtotal = 0;
   $tax_subtotal = 0;
Index: uc_vat.recalculate.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/uc_vat/uc_vat.recalculate.inc,v
retrieving revision 1.1
diff -u -p -r1.1 uc_vat.recalculate.inc
--- uc_vat.recalculate.inc	4 Mar 2010 20:49:54 -0000	1.1
+++ uc_vat.recalculate.inc	17 Sep 2010 10:19:49 -0000
@@ -17,7 +17,7 @@ function uc_vat_recalculate_prices($form
   $types = "'". implode("','", array_filter($form_state['values']['taxed_product_types'])) ."'";
 
   // Set up old and new tax arrays.
-  $old_taxes = uc_taxes_rate_load();
+  $old_taxes = uc_vat_load_taxes();
   $new_taxes = array();
   foreach ($old_taxes as $id => $tax) {
     $new_taxes[$id] = clone $tax;
Index: uc_vat.theme.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/uc_vat/uc_vat.theme.inc,v
retrieving revision 1.2
diff -u -p -r1.2 uc_vat.theme.inc
--- uc_vat.theme.inc	23 Mar 2010 13:02:30 -0000	1.2
+++ uc_vat.theme.inc	17 Sep 2010 10:19:49 -0000
@@ -14,7 +14,7 @@
  * @ingroup themeable
  */
 function theme_uc_vat_cart_review_table($show_subtotal = TRUE, $order = NULL) {
-  $taxes = uc_vat_exclude_vat() ? array() : uc_vat_load_order_taxes($order);
+  $taxes = uc_vat_exclude_vat() ? array() : uc_vat_load_taxes($order);
 
   $tax_subtotal = 0;
   $subtotal = 0;
