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	17 Oct 2010 00:01:24 -0000
@@ -515,8 +515,18 @@ function uc_edi_order_admin_form_import_
  * Handler for submitting order exports.
  */
 function uc_edi_order_admin_form_export_submit($form, &$form_state) {
+  // Export orders.
   $filename = uc_edi_export_orders();
-  drupal_set_message(t('Order export file !file created.', array('!file' => $filename)));
+
+  // If the export was successfull.
+  if ($filename) {
+    // Inform the user of a successfull export.
+    drupal_set_message(t('Order export file !file created.', array('!file' => $filename)));
+  }
+  else {
+    // Inform the user of an unsuccessfull export.
+    drupal_set_message(t('Problem creating order export file. Check your EDI settings and directory permissions.'), 'error');
+  }
 }
 
 /**
@@ -615,7 +625,10 @@ function uc_edi_order_export_archive_fil
  *   A string containing the contents of the file.
  *
  * @return
- *   The filename of the created export file.
+ *   An associative array containing the following items or NULL if the file
+ *   could not be created.
+ *   - filename: A string containing the name of the export file.
+ *   - path: A string containing the path of the export file.
  */
 function uc_edi_create_export($type, $contents) {
   switch ($type) {
@@ -623,7 +636,7 @@ function uc_edi_create_export($type, $co
       // 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')) {
+      if (!$file = @fopen($filepath, 'wb')) {
         watchdog('uc_edi', 'Problem creating order export file. Check your EDI settings and directory permissions.', array(), WATCHDOG_ERROR);
         return;
       }
@@ -925,42 +938,53 @@ function uc_edi_import_orders() {
  * Load all valid orders and create the export file in the specified directory.
  *
  * @return
- *   A string containing the filename of the created export file.
+ *   A string containing the name of the export file or NULL if the file could
+ *   not be created.
  */
 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'));
 
   $update_status = variable_get('uc_edi_order_exported_status', 'edi_pending');
+  $orders = array();
   while ($order = db_fetch_object($result)) {
+    // Keep a list of order ids for updating if export is successfull.
+    $orders[] = $order->order_id;
+
     // 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)));
-  }
+    // If export was successfull.
+    if ($file_return = uc_edi_create_export('order', $output)) {
+      // Update each orders status.
+      foreach ($orders as $order) {
+        uc_order_update_status($order, $update_status);
+        uc_order_comment_save($order, 0, variable_get('uc_edi_order_exported_message', '-'), 'order', $update_status);
+        uc_order_comment_save($order, 0, t('Order exported by EDI module.'), 'admin');
+      }
 
-  variable_set('uc_edi_order_last_export', time());
+      // Send an email if configured.
+      $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)));
+      }
+
+      variable_set('uc_edi_order_last_export', time());
+    }
+  }
 
   return $file_return['filename'];
 }
