From cc99903831f1fdb6e2c98c6ff7331068f5fcf4f3 Mon Sep 17 00:00:00 2001
From: PeterO <peter@simplistechsolutions.com>
Date: Tue, 1 Mar 2011 18:00:39 -0600
Subject: [PATCH] fix dup emails nub230-644538

---
 payment/uc_payment/uc_payment.ca.inc  |   19 +++++++++++++++----
 payment/uc_paypal/uc_paypal.pages.inc |    2 +-
 uc_cart/uc_cart.module                |    8 ++++++--
 3 files changed, 22 insertions(+), 7 deletions(-)

diff --git a/payment/uc_payment/uc_payment.ca.inc b/payment/uc_payment/uc_payment.ca.inc
index 164637f..5be141b 100644
--- a/payment/uc_payment/uc_payment.ca.inc
+++ b/payment/uc_payment/uc_payment.ca.inc
@@ -67,11 +67,11 @@ function uc_payment_ca_predicate() {
     ),
   );
 
-  // Set the order status to "Completed" when checkout is complete, none
+  // Set the order status to "Completed" when a payment is received, none
   // of the products are shippable, and the balance is less than or equal to 0.
-  $predicates['uc_checkout_complete_paid'] = array(
-    '#title' => t('Update order status upon checkout completion with full payment'),
-    '#trigger' => 'uc_checkout_complete',
+  $predicates['uc_payment_received_non_shippable'] = array(
+    '#title' => t('Update order status on non-shippable orders to completed'),
+    '#trigger' => 'uc_payment_entered',
     '#class' => 'payment',
     '#status' => 1,
     '#weight' => 1,
@@ -99,6 +99,17 @@ function uc_payment_ca_predicate() {
             'negate' => TRUE,
           ),
         ),
+        array(
+          '#name' => 'uc_order_status_condition',
+          '#title' => t('If the order status is not already Completed.'),
+          '#argument_map' => array(
+            'order' => 'order',
+          ),
+          '#settings' => array(
+            'negate' => TRUE,
+            'order_status' => 'completed',
+          ),
+        ),
       ),
     ),
     '#actions' => array(
diff --git a/payment/uc_paypal/uc_paypal.pages.inc b/payment/uc_paypal/uc_paypal.pages.inc
index 07b1a66..f2350f8 100644
--- a/payment/uc_paypal/uc_paypal.pages.inc
+++ b/payment/uc_paypal/uc_paypal.pages.inc
@@ -103,8 +103,8 @@ function uc_paypal_ipn($order_id = 0) {
           watchdog('uc_paypal', 'Payment @txn_id for order @order_id did not equal the order total.', array('@txn_id' => $txn_id, '@order_id' => $order->order_id), WATCHDOG_WARNING, l(t('view'), 'admin/store/orders/'. $order->order_id));
         }
         $comment = t('PayPal transaction ID: @txn_id', array('@txn_id' => $txn_id));
-        uc_payment_enter($order_id, 'paypal_wps', $payment_amount, $order->uid, NULL, $comment);
         uc_cart_complete_sale($order);
+        uc_payment_enter($order_id, 'paypal_wps', $payment_amount, $order->uid, NULL, $comment);
         uc_order_comment_save($order_id, 0, t('Payment of @amount @currency submitted through PayPal.', array('@amount' => uc_price($payment_amount, $context, $options), '@currency' => $payment_currency)), 'order', 'payment_received');
         uc_order_comment_save($order_id, 0, t('PayPal IPN reported a payment of @amount @currency.', array('@amount' => uc_price($payment_amount, $context, $options), '@currency' => $payment_currency)));
         break;
diff --git a/uc_cart/uc_cart.module b/uc_cart/uc_cart.module
index 691aca8..6a3c82b 100644
--- a/uc_cart/uc_cart.module
+++ b/uc_cart/uc_cart.module
@@ -1291,8 +1291,12 @@ function uc_cart_complete_sale($order, $login = FALSE) {
   // Clear our the session variables used to force the cart workflow.
   unset($_SESSION['cart_order'], $_SESSION['do_complete'], $_SESSION['new_user']);
 
-  module_invoke_all('uc_checkout_complete', $order, $account);
-  ca_pull_trigger('uc_checkout_complete', $order, $account);
+  // Only pull the triggers if the order was in checkout when first loaded.
+  if (uc_order_status_data($order->order_status, 'state') == 'in_checkout') {
+    module_invoke_all('uc_checkout_complete', $order, $account);
+    ca_pull_trigger('uc_checkout_complete', $order, $account);
+  }
+
 
   return $themed_output;
 }
-- 
1.5.4.3

