Index: CHANGELOG.txt
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/uc_edi/CHANGELOG.txt,v
retrieving revision 1.1
diff -u -p -r1.1 CHANGELOG.txt
--- CHANGELOG.txt	15 Oct 2010 08:31:57 -0000	1.1
+++ CHANGELOG.txt	22 Oct 2010 16:00:26 -0000
@@ -3,3 +3,5 @@ $Id: CHANGELOG.txt,v 1.1 2010/10/15 08:3
 Journal 6.x-1.x, 2010-10-14
 ---------------------------
 task #354696 by AntoineSolutions: Updated the head branch to Drupal 6.x-1.x.
+task #949690 by AntoineSolutions: Updated integration of export functionality
+  with Conditional Actions.
Index: uc_edi.ca.inc
===================================================================
RCS file: uc_edi.ca.inc
diff -N uc_edi.ca.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ uc_edi.ca.inc	22 Oct 2010 16:00:26 -0000
@@ -0,0 +1,144 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Ubercart EDI Conditional Actions hooks and functions.
+ */
+
+/**
+ * Implements hook_ca_trigger().
+ */
+function uc_edi_ca_trigger() {
+  $triggers['uc_edi_order_export'] = array(
+    '#title' => t('Order has been exported.'),
+    '#category' => t('EDI'),
+    '#arguments' => array(
+      'order' => array(
+        '#entity' => 'uc_order',
+        '#title' => t('Order'),
+      ),
+    ),
+  );
+
+  return $triggers;
+}
+
+/**
+ * Implements hook_ca_predicate().
+ */
+function uc_edi_ca_predicate() {
+  $predicates['uc_edi_order_export'] = array(
+    '#title' => t('Export the order'),
+    '#class' => 'edi',
+    '#trigger' => 'uc_checkout_complete',
+    '#status' => variable_get('uc_edi_order_export_method', 'cron') == 'checkout' ? 1 : 0,
+    '#weight' => 5,
+    '#conditions' => array(
+      '#operator' => 'AND',
+      '#conditions' => array(
+        array(
+          '#name' => 'uc_order_status_condition',
+          '#title' => t('If the order is ready for export.'),
+          '#argument_map' => array(
+            'order' => 'order',
+          ),
+          '#settings' => array(
+            'negate' => FALSE,
+            'order_status' => 'edi_export',
+          ),
+        ),
+      ),
+    ),
+    '#actions' => array(
+      array(
+        '#name' => 'uc_edi_action_order_export',
+        '#title' => t('Export the order.'),
+        '#argument_map' => array(
+          'order' => 'order',
+        ),
+      ),
+    ),
+  );
+
+  $predicates['uc_edi_order_export_update'] = array(
+    '#title' => t('Update an order after export'),
+    '#class' => 'edi',
+    '#trigger' => 'uc_edi_order_export',
+    '#status' => 1,
+    '#actions' => array(
+      array(
+        '#name' => 'uc_order_update_status',
+        '#title' => t('Update the order status to Shipment pending.'),
+        '#argument_map' => array(
+          'order' => 'order',
+        ),
+        '#settings' => array(
+          'order_status' => 'edi_pending',
+        ),
+      ),
+      array(
+        '#name' => 'uc_order_action_add_comment',
+        '#title' => t('Add an order export comment.'),
+        '#description' => t('Enter a message displayed on orders when an order gets exported.'),
+        '#argument_map' => array(
+          'order' => 'order',
+        ),
+        '#settings' => array(
+          'comment_type' => 'order',
+          'comment' => t('Order processed.'),
+        ),
+      ),
+      array(
+        '#name' => 'uc_order_action_add_comment',
+        '#title' => t('Add an admin comment.'),
+        '#argument_map' => array(
+          'order' => 'order',
+        ),
+        '#settings' => array(
+          'comment_type' => 'admin',
+          'comment' => t('Order exported by EDI module.'),
+        ),
+      ),
+    ),
+  );
+
+  return $predicates;
+}
+
+/**
+ * Implements hook_action().
+ */
+function uc_edi_ca_action() {
+  $order = array(
+    '#entity' => 'uc_order',
+    '#title' => t('Order'),
+  );
+
+  $actions['uc_edi_action_order_export'] = array(
+    '#title' => t('Export an order'),
+    '#category' => t('EDI'),
+    '#callback' => 'uc_edi_action_order_export',
+    '#arguments' => array(
+      'order' => $order,
+    ),
+  );
+
+  return $actions;
+}
+
+/******************************************************************************
+ * Conditional Action Callbacks and Forms                                     *
+ ******************************************************************************/
+
+/**
+ * Action for exporting an order.
+ *
+ * @param $order
+ *   Ubercart order object.
+ * @param $settings
+ *   Default values for form elements.
+ */
+function uc_edi_action_order_export($order, $settings) {
+  uc_edi_export_order($order);
+}
Index: uc_edi.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/uc_edi/uc_edi.install,v
retrieving revision 1.2
diff -u -p -r1.2 uc_edi.install
--- uc_edi.install	15 Oct 2010 08:28:04 -0000	1.2
+++ uc_edi.install	22 Oct 2010 16:00:26 -0000
@@ -43,8 +43,6 @@ function uc_edi_uninstall() {
   variable_del('uc_edi_order_export_mail_body');
   variable_del('uc_edi_order_export_freq');
   variable_del('uc_edi_order_export_status');
-  variable_del('uc_edi_order_exported_status');
-  variable_del('uc_edi_order_exported_message');
   variable_del('uc_edi_order_export_pattern');
   variable_del('uc_edi_order_export_product_pattern');
   variable_del('uc_edi_order_export_delimiter');
@@ -89,3 +87,46 @@ function uc_edi_update_6000() {
 
   return array();
 }
+
+/**
+ * Implements hook_update_N().
+ */
+function uc_edi_update_6001() {
+  // If EDI was configured not to automatically run.
+  if (variable_get('uc_edi_order_export_freq', '1 day') == 'never') {
+    // Set the new export method to manual.
+    variable_set('uc_edi_order_export_method', 'manual');
+    // Reset the auto export frequency variable.
+    variable_del('uc_edi_order_export_freq');
+  }
+
+  // Update the 'Export the order' predicate to match the cron settings.
+  $export_status = variable_get('uc_edi_order_export_status', 'edi_export');
+  if ($export_status != 'edi_export') {
+    // Load the 'Export the order' predicate.
+    $predicate = ca_load_predicate('uc_edi_order_export');
+    // Update the default values with variable values.
+    $predicate['#conditions']['#conditions'][0]['#settings']['order_status'] = $export_status;
+    // Save the predicate.
+    ca_save_predicate($predicate);
+  }
+
+  // Update the 'Update an order after export' predicate with variable values.
+  $exported_status = variable_get('uc_edi_order_exported_status', 'edi_pending');
+  $exported_message = variable_get('uc_edi_order_exported_message', '-');
+  if ($exported_status != 'edi_pending' || $exported_message != '-') {
+    // Load the order export update predicate.
+    $predicate = ca_load_predicate('uc_edi_order_export_update');
+    // Update the default values with variable values.
+    $predicate['#actions'][0]['#settings']['order_status'] = $exported_status;
+    $predicate['#actions'][1]['#settings']['comment'] = $exported_message;
+    // Save the predicate.
+    ca_save_predicate($predicate);
+  }
+
+  // Delete unused variables.
+  variable_del('uc_edi_order_exported_status');
+  variable_del('uc_edi_order_exported_message');
+
+  return array();
+}
Index: uc_edi.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/uc_edi/uc_edi.module,v
retrieving revision 1.2
diff -u -p -r1.2 uc_edi.module
--- uc_edi.module	15 Oct 2010 08:28:04 -0000	1.2
+++ uc_edi.module	22 Oct 2010 16:00:26 -0000
@@ -9,6 +9,8 @@
  *   http://www.ubercart.org
  */
 
+require_once('uc_edi.ca.inc');
+
 /*******************************************************************************
  * Hook Functions (Drupal)
  ******************************************************************************/
@@ -25,6 +27,11 @@ function uc_edi_menu() {
     'access arguments' => array('administer edi orders'),
     'type' => MENU_NORMAL_ITEM,
   );
+  $items['admin/store/settings/edi/order-export-method-js'] = array(
+    'page callback' => 'uc_edi_order_export_method_js',
+    'access arguments' => array('administer edi orders'),
+    'type' => MENU_CALLBACK,
+  );
   $items['admin/store/settings/edi/help'] = array(
     'title' => 'EDI help topics',
     'description' => 'Find information on certain parts of the EDI module.',
@@ -84,7 +91,7 @@ function uc_edi_cron() {
   }
 
   // Decide if it's time to export orders.
-  if (variable_get('uc_edi_order_export_freq', '1 day')) {
+  if (variable_get('uc_edi_order_export_method', 'cron') == 'cron') {
     $time = strtotime(variable_get('uc_edi_order_export_freq', '1 day') . ' ago');
     if (variable_get('uc_edi_order_last_export', 0) < $time) {
       uc_edi_export_orders();
@@ -263,43 +270,24 @@ function uc_edi_settings_form() {
     '#disabled' => !(module_exists('mimemail')),
     '#default_value' => variable_get('uc_edi_order_export_mail_body', ''),
   );
-  $form['export']['orders']['uc_edi_order_export_freq'] = array(
-    '#type' => 'select',
-    '#title' => t('Export frequency'),
-    '#description' => t('The frequency at which automatic exports should occur.'),
+  $method = variable_get('uc_edi_order_export_method', 'cron');
+  $form['export']['orders']['uc_edi_order_export_method'] = array(
+    '#type' => 'radios',
+    '#title' => t('Export method'),
+    '#description' => t('The method used to export the order.'),
     '#options' => array(
-      '1 second' => t('Every cron run'),
-      '1 hour' => t('Once an hour'),
-      '6 hours' => t('Every six hours'),
-      '12 hours' => t('Every twelve hours'),
-      '1 day' => t('Once a day'),
-      'never' => t('Never'),
+      'checkout' => t('On checkout completion'),
+      'cron' => t('At a specified interval'),
+      'manual' => t('Manualy'),
+    ),
+    '#default_value' => $method,
+    '#ahah' => array(
+      'method' => 'replace',
+      'path' => 'admin/store/settings/edi/order-export-method-js',
+      'wrapper' => 'method-sub-wrapper',
     ),
-    '#default_value' => variable_get('uc_edi_order_export_freq', '1 day'),
-  );
-  foreach (uc_order_status_list('edi_processing') as $status) {
-    $options[$status['id']] = $status['title'];
-  }
-  $form['export']['orders']['uc_edi_order_export_status'] = array(
-    '#type' => 'select',
-    '#title' => t('Ready for export order status'),
-    '#description' => t('Select a status used to mark orders as ready for export.'),
-    '#options' => $options,
-    '#default_value' => variable_get('uc_edi_order_export_status', 'edi_export'),
-  );
-  $form['export']['orders']['uc_edi_order_exported_status'] = array(
-    '#type' => 'select',
-    '#title' => t('Exported order status'),
-    '#description' => t('Select a status used to mark orders that have been exported.'),
-    '#options' => $options,
-    '#default_value' => variable_get('uc_edi_order_exported_status', 'edi_pending'),
-  );
-  $form['export']['orders']['uc_edi_order_exported_message'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Export order comment'),
-    '#description' => t('Enter a message displayed on orders when an order gets exported.'),
-    '#default_value' => variable_get('uc_edi_order_exported_message', '-'),
   );
+  $form['export']['orders']['uc_edi_order_export_method_sub'] = uc_edi_order_export_method($method);
   $form['export']['orders']['uc_edi_order_export_pattern'] = array(
     '#type' => 'textarea',
     '#title' => t('Order export pattern'),
@@ -326,7 +314,94 @@ function uc_edi_settings_form() {
     '#disabled' => TRUE,
   );
 
-  return system_settings_form($form);
+  $form = system_settings_form($form);
+
+  // We will call system_settings_form_submit() manually, so remove it for now.
+  unset($form['#submit']);
+  return $form;
+}
+
+/**
+ * Return the unrendered method sub form.
+ *
+ * @param $method
+ *   One of the following order export methods:
+ *   - checkout: Export orders on checkout completion.
+ *   - cron: Export orders order at a specified interval.
+ *   - manual: Export orders manually.
+ *
+ *  @return
+ *    An array containing the unrendered form items.
+ */
+function uc_edi_order_export_method($method) {
+  // Generate the correct form based on the method.
+  switch ($method) {
+    // Export on checkout completion.
+    case 'checkout':
+      $form = array(
+        '#value' => t('Exporting orders on checkout completion uses the <a href="!url1">Export the order</a> predicate for exporting orders. Please verify your <a href="!url2">Conditional Actions settings</a>.', array('!url1' => url('admin/store/ca/uc_edi_order_export/edit'), '!url2' => url('admin/store/ca'))),
+      );
+      break;
+
+    // Export at a specified interval.
+    case 'cron':
+      $form['uc_edi_order_export_freq'] = array(
+        '#type' => 'select',
+        '#title' => t('Export frequency'),
+        '#description' => t('The frequency at which automatic exports should occur.'),
+        '#options' => array(
+          '1 second' => t('Every cron run'),
+          '1 hour' => t('Once an hour'),
+          '6 hours' => t('Every six hours'),
+          '12 hours' => t('Every twelve hours'),
+          '1 day' => t('Once a day'),
+        ),
+        '#default_value' => variable_get('uc_edi_order_export_freq', '1 day'),
+      );
+      foreach (uc_order_status_list('general') as $status) {
+        $options[$status['id']] = $status['title'];
+      }
+      $form['uc_edi_order_export_status'] = array(
+        '#type' => 'select',
+        '#title' => t('Ready for export order status'),
+        '#description' => t('Select a status used to mark orders as ready for export.'),
+        '#options' => $options,
+        '#default_value' => variable_get('uc_edi_order_export_status', 'edi_export'),
+      );
+      break;
+
+    // Export manually.
+    case 'manual':
+      $form = array(
+        '#value' => t('Exporting orders manually can be accomplished using the <a href="!url">Manual EDI import/export form</a>.', array('!url' => url('admin/store/orders/edi'))),
+      );
+      break;
+  }
+
+  // Add prefix and suffix elements.
+  $form += array(
+    '#prefix' => '<div id="method-sub-wrapper">',
+    '#suffix' => '</div>',
+  );
+
+  return $form;
+}
+
+/**
+ * Return the rendered method sub form.
+ */
+function uc_edi_order_export_method_js() {
+  // Retrieve the unrendered frequency form.
+  $form = uc_edi_order_export_method($_POST['uc_edi_order_export_method']);
+
+  // Remove #prefix and #suffix to avoid a duplicate wrapper.
+  unset($form['#prefix'], $form['#suffix']);
+
+  // Build and render the form, then return it in JSON format.
+  $form_state = array();
+  $form = form_builder($form['#name'], $form, $form_state);
+  $output = drupal_render($form);
+  drupal_json(array('status' => TRUE, 'data' => $output));
 }
 
 /**
@@ -362,6 +437,44 @@ function uc_edi_settings_form_validate($
 }
 
 /**
+ * Handler for submitting the settings form.
+ */
+function uc_edi_settings_form_submit($form, &$form_state) {
+  // Call the default submit handler first.
+  system_settings_form_submit($form, $form_state);
+
+  // If the export method has changed from or to 'checkout'.
+  if ($form['export']['orders']['uc_edi_order_export_method']['#default_value'] != $form['export']['orders']['uc_edi_order_export_method']['#value'] &&
+     ($form['export']['orders']['uc_edi_order_export_method']['#default_value'] == 'checkout' || $form['export']['orders']['uc_edi_order_export_method']['#value'] == 'checkout')) {
+    // Load the order export predicate.
+    $predicate = ca_load_predicate('uc_edi_order_export');
+    $update = FALSE;
+
+    // Update the 'Export the order' predicate based on export method.
+    if ($form_state['values']['uc_edi_order_export_method'] == 'checkout') {
+      if ($predicate['#status'] == 0) {
+        $predicate['#status'] = 1;
+        $update = TRUE;
+      }
+    }
+    else {
+      if ($predicate['#status'] == 1) {
+        $predicate['#status'] = 0;
+        $update = TRUE;
+      }
+    }
+
+    // If manual predicate update was required.
+    if ($update) {
+      ca_save_predicate($predicate);
+    }
+
+    // Inform the user that the predicate was updated.
+    drupal_set_message(t('The <a href="!url1">Export order</a> predicate has been updated. Please validate your <a href="!url2">Conditional Actions settings</a>.', array('!url1' => url('admin/store/ca/uc_edi_order_export/edit'), '!url2' => url('admin/store/ca'))));
+  }
+}
+
+/**
  * Displays the help page for select EDI topics.
  */
 function uc_edi_admin_help() {
@@ -449,18 +562,28 @@ function uc_edi_order_admin_form(&$form_
     '#collapsed' => FALSE,
   );
 
-  // Figure out how many seconds remain till the next export will run.
-  if (variable_get('uc_edi_order_export_freq', '1 day') != 'never') {
-    $elapsed = time() - variable_get('uc_edi_order_last_export', 0);
-    $needed = time() - strtotime(variable_get('uc_edi_order_export_freq', '1 day') . ' ago');
-    $seconds = $needed - $elapsed;
-  }
-  else {
-    $seconds = t('(auto export disabled) 0');
+  // Number of orders ready for export.
+  $count = count($orders);
+  switch (variable_get('uc_edi_order_export_method', 'cron')) {
+    case 'checkout':
+      $message = t('Orders are configured to export on checkout completion. !count order(s) require manual export.', array('!count' => $count));
+      break;
+
+    case 'cron':
+      // Figure out how many seconds remain till the next export will run.
+      $elapsed = time() - variable_get('uc_edi_order_last_export', 0);
+      $needed = time() - strtotime(variable_get('uc_edi_order_export_freq', '1 day') . ' ago');
+      $seconds = $needed - $elapsed;
+      $message = t('Auto export configured to export !count orders in !seconds seconds:', array('!count' => $count, '!seconds' => $seconds < 0 ? 0 : $seconds));
+      break;
+
+    case 'manual':
+      $message = t('!count order(s) ready for manual export.', array('!count' => $count));
+      break;
   }
 
   $form['export']['export_info'] = array(
-    '#value' => '<div><b>' . t('There are !count orders ready to export in !seconds seconds:', array('!count' => count($orders), '!seconds' => $seconds < 0 ? 0 : $seconds)) . '</b><br />' . implode(', ', $orders) . '<p></p></div>',
+    '#value' => '<div><b>' . $message . '</b><br />' . implode(', ', $orders) . '<p></p></div>',
   );
 
   // Display a fieldset with a preview of the export file.
@@ -606,38 +729,61 @@ function uc_edi_order_export_archive_fil
 }
 
 /**
- * Create an export file containing $contents!
+ * Create an export file containing $contents and call delievery methods.
  *
  * @param $type
  *   The type of export file to create:
  *   - order
+ * @param $filename
+ *   A string containing the name of the file.
  * @param $contents
  *   A string containing the contents of the file.
  *
  * @return
- *   The filename of the created export file.
+ *   TRUE if the export file was created, otherwise FALSE.
  */
-function uc_edi_create_export($type, $contents) {
-  switch ($type) {
-    case 'order':
-      // Generate the order export filename.
-      $filename = variable_get('uc_edi_order_export_prefix', 'export-') . mktime() . '.' . variable_get('uc_edi_order_export_extension', 'edi');
-      $filepath = variable_get('uc_edi_order_export_dir', '') . '/' . $filename;
-      if (!$file = fopen($filepath, 'wb')) {
-        watchdog('uc_edi', 'Problem creating order export file. Check your EDI settings and directory permissions.', array(), WATCHDOG_ERROR);
-        return;
-      }
-
-      fwrite($file, $contents);
-      fclose($file);
+function uc_edi_create_export($type, $filename, $contents) {
+  // Don't bother creating an export file if no data was passed in.
+  if (!empty($contents)) {
+    // Generate the file path.
+    $filepath = variable_get('uc_edi_order_export_dir', '') . '/' . $filename;
+
+    switch ($type) {
+      case 'order':
+        // If file fails to open.
+        if (!$file = fopen($filepath, 'wb')) {
+          watchdog('uc_edi', 'Problem creating order export file. Check your EDI settings and directory permissions.', array(), WATCHDOG_ERROR);
+          return FALSE;
+        }
 
-      watchdog('uc_edi', 'Order export file !filename created.', array('!filename' => $filename));
+        // Write the contents to the file and close it.
+        fwrite($file, $contents);
+        fclose($file);
+
+        // Create log of file creation.
+        watchdog('uc_edi', 'Order export file !filename created.', array('!filename' => $filename));
+
+        // Handle delivery methods here.
+        $mail_to = variable_get('uc_edi_order_export_mail_to', '');
+        if (!empty($mail_to) && module_exists('mimemail')) {
+          $mail_from = variable_get('uc_edi_order_export_mail_from', variable_get('site_mail', 'test@example.com'), 'test@example.com');
+          $subject = variable_get('uc_edi_order_export_mail_subject', '');
+          $body = variable_get('uc_edi_order_export_mail_body', '');
+          $attachments = array(
+            0 => array(
+              'filepath' => $filepath,
+              'filemime' => 'text/csv',
+            ),
+          );
+          mimemail($mail_from, $mail_to, $subject, $body, FALSE, array(), NULL, $attachments);
+          watchdog('uc_edi', t('Export mailed to @mail-to .', array('@mail-to' => $mail_to)));
+        }
 
-      return array(
-        'filename' => $filename,
-        'path' => $filepath,
-      );
+        return TRUE;
+    }
   }
+
+  return FALSE;
 }
 
 /**
@@ -922,47 +1068,68 @@ function uc_edi_import_orders() {
 }
 
 /**
- * Load all valid orders and create the export file in the specified directory.
+ * Create an export file for a single order in the specified directory.
+ *
+ * @return
+ *   A string containing the filename of the created export file or NULL if the
+ *   file creation process failed.
+ */
+function uc_edi_export_order(&$order) {
+  // Generate the filename using the order Id for uniqueness.
+  $filename = variable_get('uc_edi_order_export_prefix', 'export-') . $order->order_id . '.' . variable_get('uc_edi_order_export_extension', 'edi');
+  // Create the order's export string.
+  $contents .= uc_edi_generate_order_export($order);
+
+  // Create the export file.
+  if (uc_edi_create_export('order', $filename, $contents)) {
+    // Pull the EDI Export trigger on the order.
+    ca_pull_trigger('uc_edi_order_export', $order);
+
+    // Update the order export timestamp.
+    variable_set('uc_edi_order_last_export', time());
+
+    return $filename;
+  }
+
+  return;
+}
+
+/**
+ * Create an export file for all valid orders in the specified directory.
  *
  * @return
- *   A string containing the filename of the created export file.
+ *   A string containing the filename of the created export file or NULL if the
+ *   file creation process failed.
  */
 function uc_edi_export_orders() {
-  $result = db_query("SELECT order_id FROM {uc_orders} WHERE order_status = '%s' ORDER BY order_id", variable_get('uc_edi_order_export_status', 'edi_export'));
+  // Generate the filename using the current timestamp for uniqueness.
+  $filename = variable_get('uc_edi_order_export_prefix', 'export-') . mktime() . '.' . variable_get('uc_edi_order_export_extension', 'edi');
 
-  $update_status = variable_get('uc_edi_order_exported_status', 'edi_pending');
+  // Load all orders ready for export.
+  $result = db_query("SELECT order_id FROM {uc_orders} WHERE order_status = '%s' ORDER BY order_id", variable_get('uc_edi_order_export_status', 'edi_export'));
   while ($order = db_fetch_object($result)) {
+    // Keep array of orders to modify after successfull export.
+    $orders[] = $order;
     // Load each valid order and create its export string.
     $order = uc_order_load($order->order_id);
-    $output .= uc_edi_generate_order_export($order);
-
-    uc_order_update_status($order->order_id, $update_status);
-    uc_order_comment_save($order->order_id, 0, variable_get('uc_edi_order_exported_message', '-'), 'order', $update_status);
-    uc_order_comment_save($order->order_id, 0, t('Order exported by EDI module.'), 'admin');
-  }
-
-  // Don't bother creating an export file if no data was created.
-  if (!empty($output)) {
-    $file_return = uc_edi_create_export('order', $output);
-  }
-  $mail_to = variable_get('uc_edi_order_export_mail_to', '');
-  if (!empty($mail_to) && module_exists('mimemail')) {
-    $mail_from = variable_get('uc_edi_order_export_mail_from', variable_get('site_mail', 'test@example.com'), 'test@example.com');
-    $subject = variable_get('uc_edi_order_export_mail_subject', '');
-    $body = variable_get('uc_edi_order_export_mail_body', '');
-    $attachments = array(
-      0 => array(
-        'filepath' => $file_return['path'],
-        'filemime' => 'text/csv',
-      )
-    );
-    mimemail($mail_from, $mail_to, $subject, $body, FALSE, array(), NULL, $attachments);
-    watchdog('uc_edi', t('Export mailed to @mail-to .', array('@mail-to' => $mail_to)));
+    // Create each order's export string.
+    $contents .= uc_edi_generate_order_export($order);
   }
 
-  variable_set('uc_edi_order_last_export', time());
+  // Create the export file.
+  if (uc_edi_create_export('order', $filename, $contents)) {
+    // Pull the EDI Export trigger on all the orders.
+    foreach ($orders as $order) {
+      ca_pull_trigger('uc_edi_order_export', $order);
+    }
+
+    // Update the order export timestamp.
+    variable_set('uc_edi_order_export_last', time());
+
+    return $filename;
+  }
 
-  return $file_return['filename'];
+  return;
 }
 
 /**
