--- erp_cash_sale/erp_cash_sale.module-org	2011-08-21 15:17:18.000000000 +0200
+++ erp_cash_sale/erp_cash_sale.module	2011-08-21 11:00:55.000000000 +0200
@@ -200,6 +200,11 @@
 function erp_cash_sale_admin_settings() {
   drupal_set_title(t('Cash sale settings'));
 
+  $form['erp_cash_sale_email_sender'] =
+    array('#type' => 'textfield',
+          '#title' => t('Email address from sender when sending cash sales'),
+          '#default_value' => variable_get('erp_cash_sale_email_sender', ''));
+
   $form['erp_cash_sale_email_bcc'] =
     array('#type' => 'textfield',
           '#title' => t('Email address to BCC when sending cash sales'),
--- erp_email/erp_email.info-org	2011-08-21 15:17:18.000000000 +0200
+++ erp_email/erp_email.info	2011-08-21 13:01:38.000000000 +0200
@@ -8,7 +8,7 @@
 core = 6.x
 
 ; Information added by drupal.org packaging script on 2011-02-25
-version = "6.x-1.x-dev"
+version = "6.x-1.2x-dev"
 core = "6.x"
 project = "erp"
 datestamp = "1298619201"
--- erp_email/erp_email.module-org	2011-08-21 18:03:27.000000000 +0200
+++ erp_email/erp_email.module	2011-08-21 17:57:19.000000000 +0200
@@ -88,7 +88,7 @@
   $items['admin/erp/email'] = array(
     'title' => 'Email',
     'description' => 'Configure erp email settings',
-    'access arguments' => array('erp email admin'),
+    'access arguments' => array('admin erp_email'),
     'page callback' => 'drupal_get_form',
     'page arguments' => array('erp_email_admin_settings'),
   );
@@ -143,7 +143,7 @@
 
   $form['erp_bcc_email'] = array(
     '#type' => 'textfield',
-    '#title' => t('BCC quote/invoice/etc emails to this address'),
+    '#title' => t('BCC general quote/invoice/etc emails to this address'),
     '#default_value' => variable_get('erp_bcc_email', ''),
   );
 
@@ -153,6 +153,12 @@
       '#title' => t('Send quote emails from this address'),
       '#default_value' => variable_get('erp_quote_email_sender', '')
     );
+
+    $form['erp_quote_email_bcc'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Email address to BCC when sending quotes'),
+      '#default_value' => variable_get('erp_quote_email_bcc', '')
+    );
   }
 
   if (module_exists('erp_invoice')) {
@@ -161,6 +167,13 @@
       '#title' => t('Send invoice emails from this address'),
       '#default_value' => variable_get('erp_invoice_email_sender', '')
     );
+
+    $form['erp_invoice_email_bcc'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Email address to BCC when sending invoices'),
+      '#default_value' => variable_get('erp_invoice_email_bcc', ''),
+      '#description' => t('Its advisable to setup an email account that will receive a copy of all invoices sent out to customers. Enter the email address for that account here.')
+    );
   }
 
   if (module_exists('erp_cash_sale')) {
@@ -169,6 +182,12 @@
       '#title' => t('Send cash sale emails from this address'),
       '#default_value' => variable_get('erp_cash_sale_email_sender', '')
     );
+
+    $form['erp_cash_sale_email_bcc'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Email address to BCC when sending cash sales'),
+      '#default_value' => variable_get('erp_cash_sale_email_bcc', '')
+    );
   }
 
   if (module_exists('erp_purchase_order')) {
@@ -177,6 +196,12 @@
       '#title' => t('Send purchase order emails from this address'),
       '#default_value' => variable_get('erp_purchase_order_email_sender', '')
     );
+
+    $form['erp_purchase_order_email_bcc'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Email address to BCC when sending purchase orders'),
+      '#default_value' => variable_get('erp_purchase_order_email_bcc', '')
+    );
   }
 
   return system_settings_form($form);
@@ -259,7 +284,7 @@
 }
 
 function erp_email_confirm_form($form_state, $node) {
-	$form = array();
+  $form = array();
   $email = array();
   $type = drupal_strtolower($type);
 
@@ -359,30 +384,64 @@
       $from_email = variable_get('erp_purchase_order_email_sender', FALSE);
       break;
   }
+  // If there is no document sender address, use stores email address
   if (!$from_email) {
-    $from_email = $store->mail;
+    $from_email = $store->email;
+  }
+
+  //$path = $form_state['values']['node']['path'];
+
+  $node = $form_state['values']['node'];
+
+  if (!$node->path) {
+    $path = 'node/'. $node->nid;
+  }
+  else {
+    $path = $node->path;
   }
 
   switch ($form_state['values']['email_format']) {
     case 1: // HTML
       require_once(drupal_get_path('module', 'print') .'/print.pages.inc');
+      require_once(drupal_get_path('module', 'print_mail') .'/print_mail.inc');
 
-      $html = _erp_email_print_html_controller($form_state['values']['node'], $form_state['values']['email_text'], FALSE);
+      //$html = _erp_email_print_html_controller($form_state['values']['node'], $form_state['values']['email_text'], FALSE);
+      //list($headers, $body) = erp_email_build_mime_body($from_email, $html, TRUE);
+
+      $cid = isset($form_state['values']['cid']) ? $form_state['values']['cid'] : NULL;
+
+      //$print_mail_text_message = filter_xss_admin(variable_get('print_mail_text_message', t('Message from sender')));
+      $sender_message = $print_mail_text_message .':<br /><br /><em>'. nl2br(check_plain($form_state['values']['email_text'])) .'</em>';
+
+      //$print = print_controller($form_state['values']['path'], $cid, PRINT_MAIL_FORMAT, $form_state['values']['chk_teaser'], $sender_message);
+      $print = print_controller($path, $cid, PRINT_MAIL_FORMAT, false, $sender_message);
+
+      // Spaces in img URLs must be replaced with %20
+      $pattern = '!<(img\s[^>]*?)>!is';
+      $print['content'] = preg_replace_callback($pattern, '_print_mail_encode_urls', $print['content']);
+
+      $attachments[] = array(
+        'filecontent' => $print['content'],
+        'filemime' => 'text/html',
+        'filename' => $params['subject'].'.html',
+      );
 
-      list($headers, $body) = erp_email_build_mime_body($from_email, $html, TRUE);
       break;
 
     case 2: // PDF
       require_once(drupal_get_path('module', 'print_pdf') .'/print_pdf.pages.inc');
 
-      $output = _erp_email_print_pdf_controller($form_state['values']['node']);
+      $attachments[] = array(   // attach pdf file
+        'filecontent' => print_pdf_generate_path($path),  // call the print_pdf function to generate PDF content string
+        'filemime' => 'application/pdf',
+        'filename' => $params['subject'].'.pdf',
+      );
 
-      list($headers, $body) = erp_email_build_mime_body($from_email, $form_state['values']['email_text'], FALSE, $output);
       break;
   }
 
-  $params['headers'] = $headers;
-  $params['body'] = $body;
+  $params['body'] = $form_state['values']['email_text'];
+
   $addresses = erp_email_collate($form_state['values']);
   if (is_array($addresses)) {
     $to_addresses = implode(', ', $addresses);
@@ -392,8 +451,20 @@
     return;
   }
 
-  //drupal_mail('erp_email', 'erp_email', $to_addresses, language_default(), $params, $store->email, TRUE);
-  mail($to_addresses, $params['subject'], $body, $headers);
+  $headers = array('From' => $from_email, 'Sender' => $from_email, 'Reply-to' => $from_email, 'Return-Path' => $from_email, 'Errors-To' => $from_email);
+  //$headers = array_merge($headers, array('X-Mailer' => 'Drupal'));
+
+  foreach (array('erp_bcc_email', 'erp_quote_email_bcc', 'erp_invoice_email_bcc', 'erp_cash_sale_email_bcc', 'erp_purchase_order_email_bcc') as $xmail) {
+    $erp_bcc_email = variable_get($xmail, FALSE);
+    if ($erp_bcc_email) {
+      $headers = array_merge($headers, array('Bcc' => $erp_bcc_email));
+    }
+  }
+
+  $params['headers'] = $headers;
+
+  // send email
+  mimemail( $from_email, $to_addresses, $params['subject'], $params['body'], FALSE, $params['headers'], drupal_html_to_text(decode_entities($params['body'])), $attachments );
 
   return;
 }
@@ -472,239 +543,3 @@
   return array_unique($addresses);
 }
 
-/**
- * Take some text, and file attachments, and build a mime email body out of it.
- *
- */
-function erp_email_build_mime_body($from, $body, $html = FALSE) {
-  $headers = '';
-  $message = '';
-  $args = func_get_args();
-
-  // Ignore the first two
-  $args = array_slice($args, 3);
-
-  $timestamp = md5(uniqid(time()));
-
-  $headers = "MIME-Version: 1.0\n";
-  $headers .= "From: $from\nReply-to: $from\nReturn-path: $from\nErrors-to: $from\nX-Mailer: Drupal\n";
-  $headers .= "Content-Type: multipart/mixed;\n\tboundary=$timestamp\n";
-  $erp_bcc_email = variable_get('erp_bcc_email', FALSE);
-  if ($erp_bcc_email) {
-    $headers .= "Bcc: ". $erp_bcc_email ."\n";
-  }
-
-  $message = "\n--$timestamp\n";
-  if ($html) {
-    $message .= "Content-Type: text/html; charset=UTF-8;"."\n\n"; // sets the mime type
-  }
-  else {
-    $message .= "Content-Type: text/plain; charset=UTF-8;"."\n\n"; // sets the mime type
-  }
-  $message .= $body ."\n";
-  $message .= "\n\n";
-
-  foreach ($args as $filename) {
-    if (file_exists($filename) && (filesize($filename) > 0)) {
-      $filename = str_replace("\\", "\\\\", $filename);
-
-      $message .= "--$timestamp"."\n";
-      $message .= "Content-Type:". file_get_mimetype($filename) .";\n\tname=". $filename ."\n";
-      $message .= "Content-Transfer-Encoding: base64\n";
-      $message .= "Content-Disposition: attachment;\n\tfilename=". $filename ."\n\n";
-
-      $filedata = fread(fopen($filename, "rb"), filesize($filename));
-
-      $message .= chunk_split(base64_encode($filedata));
-      $message .= "\n\n";
-    }
-  }
-
-  $message .= "--$timestamp--";
-
-  $results = array($headers, $message);
-
-  return $results;
-}
-
-
-/**
- * The code below is almost directly copied from print_pdf.pages.inc, but we dont always directly output
- * to the user, which allows us to then email as an attachment. Someday I migth submit patches to print
- * module to make it more usable like an API, until then....
- */
-function _erp_email_print_html_controller($node, $extra_text, $file_output) {
-	if (!$node->path) {
-    $path = 'node/'. $node->nid;
-	}
-	else {
-		$path = $node->path;
-	}
-  $cid = isset($_GET['comment']) ? (int)$_GET['comment'] : NULL;
-
-  $print = print_controller($path, $cid, PRINT_HTML_FORMAT);
-  if ($print !== FALSE) {
-    ob_start();
-    include_once(_print_get_template(PRINT_HTML_FORMAT, $print['type']));
-    $html = ob_get_contents();
-    ob_end_clean();
-    $html = drupal_final_markup($html);
-
-    $nodepath = (isset($node->path)) ? drupal_get_normal_path($node->path) : 'node/'. $path;
-    db_query("UPDATE {print_page_counter} SET totalcount = totalcount + 1, timestamp = %d WHERE path = '%s'", time(), $nodepath);
-    // If we affected 0 rows, this is the first time viewing the node.
-    if (!db_affected_rows()) {
-      // We must create a new row to store counters for the new node.
-      db_query("INSERT INTO {print_page_counter} (path, totalcount, timestamp) VALUES ('%s', 1, %d)", $nodepath, time());
-    }
-
-    if ($file_output) {
-      $html_filename = str_replace('/', '_', $path) .'.html';
-
-      $output_dir = file_directory_path() .'/'. $node->type;
-      file_check_directory($output_dir, FILE_CREATE_DIRECTORY);
-      $output_file = $output_dir .'/'. $html_filename;
-      file_put_contents($output_file, $html);
-      return $output_file;
-    }
-    else {
-      return $html;
-    }
-  }
-}
-
-function _erp_email_print_pdf_controller($node) {
-  global $base_url, $language;
-
-  // Disable caching for generated PDFs, as Drupal doesn't ouput the proper headers from the cache
-  $GLOBALS['conf']['cache'] = FALSE;
-
-  if (!$node->path) {
-    $path = 'node/'. $node->nid;
-  }
-  else {
-    $path = $node->path;
-  }
-  $cid = isset($_GET['comment']) ? (int)$_GET['comment'] : NULL;
-
-  $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
-
-  $print = print_controller($path, $cid, PRINT_PDF_FORMAT);
-  if ($print === FALSE) {
-    return;
-  }
-
-  // Img elements must be set to absolute
-  $pattern = '!<(img\s[^>]*?)>!is';
-  $print['content'] = preg_replace_callback($pattern, '_print_rewrite_urls', $print['content']);
-  $print['logo'] = preg_replace_callback($pattern, '_print_rewrite_urls', $print['logo']);
-  $print['footer_message'] = preg_replace_callback($pattern, '_print_rewrite_urls', $print['footer_message']);
-  // And converted from private to public paths
-  $file_downloads = variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC);
-  if ($file_downloads == FILE_DOWNLOADS_PRIVATE) {
-    switch (variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE)) {
-      case LANGUAGE_NEGOTIATION_PATH_DEFAULT:
-      case LANGUAGE_NEGOTIATION_PATH:
-        $lang = $language->language;
-        break;
-      default:
-        $lang = '';
-        break;
-    }
-    $pattern = "!(<img\s[^>]*?src\s*?=\s*?['\"]?)${base_url}/(?:(?:index.php)?\?q=)?(?:${lang}/)?system/files(/[^>]*?>)!is";
-    $replacement = '$1file://'. realpath(file_directory_path()) .'$2';
-    $print['content'] = preg_replace($pattern, $replacement, $print['content']);
-    $print['logo'] = preg_replace($pattern, $replacement, $print['logo']);
-    $print['footer_message'] = preg_replace($pattern, $replacement, $print['footer_message']);
-  }
-
-  $node = $print['node'];
-  $temp = ob_get_status();
-  ob_start();
-  include_once(_print_get_template(PRINT_PDF_FORMAT, $print['type']));
-  $html = ob_get_contents();
-  ob_end_clean();
-  $html = drupal_final_markup($html);
-
-  // Convert the a href elements, to make sure no relative links remain
-  $pattern = '!<(a\s[^>]*?)>!is';
-  $html = preg_replace_callback($pattern, '_print_rewrite_urls', $html);
-  // And make anchor links relative again, to permit in-PDF navigation
-  $html = preg_replace("!${base_url}/". PRINTPDF_PATH ."/.*?%2523!", '#', $html);
-
-  $pdf_filename = variable_get('print_pdf_filename', PRINT_PDF_FILENAME_DEFAULT);
-  if (function_exists('token_replace') && !empty($pdf_filename)) {
-    $pdf_filename = token_replace($pdf_filename, 'node', $node) .'.pdf';
-  }
-  else {
-    $pdf_filename = str_replace('/', '_', $path) .'.pdf';
-  }
-
-  if (basename($print_pdf_pdf_tool, '.exe') == 'wkhtmltopdf') {
-    return _erp_email_print_pdf_wkhtmltopdf($print, $html, $pdf_filename, $node);
-  }
-  else {
-    return drupal_not_found();
-  }
-
-  $nodepath = (isset($node->path)) ? drupal_get_normal_path($node->path) : 'node/'. $path;
-  db_query("UPDATE {print_pdf_page_counter} SET totalcount = totalcount + 1, timestamp = %d WHERE path = '%s'", time(), $nodepath);
-  // If we affected 0 rows, this is the first time viewing the node.
-  if (!db_affected_rows()) {
-    // We must create a new row to store counters for the new node.
-    db_query("INSERT INTO {print_pdf_page_counter} (path, totalcount, timestamp) VALUES ('%s', 1, %d)", $nodepath, time());
-  }
-}
-
-function _erp_email_print_pdf_wkhtmltopdf($print, $html, $filename, $node) {
-  $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
-  $print_pdf_paper_size = drupal_strtolower(variable_get('print_pdf_paper_size', PRINT_PDF_PAPER_SIZE_DEFAULT));
-  $print_pdf_page_orientation = variable_get('print_pdf_page_orientation', PRINT_PDF_PAGE_ORIENTATION_DEFAULT);
-  $print_pdf_content_disposition = variable_get('print_pdf_content_disposition', PRINT_PDF_CONTENT_DISPOSITION_DEFAULT);
-  $print_pdf_wkhtmltopdf_options = variable_get('print_pdf_wkhtmltopdf_options', PRINT_PDF_WKHTMLTOPDF_OPTIONS);
-  $print_pdf_xvfb_options = variable_get('print_pdf_xvfb_options', PRINT_PDF_XVFB_OPTIONS);
-
-  $xvfb_binary = key(file_scan_directory(drupal_get_path('module', 'print'), '^Xvfb$'));
-  $dpi = 96;
-
-  if (function_exists('token_replace') && !empty($print_pdf_wkhtmltopdf_options)) {
-    $print_pdf_wkhtmltopdf_options = token_replace($print_pdf_wkhtmltopdf_options, 'node', $print['node']);
-  }
-
-  $descriptor = array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w'));
-  $pdf_output = (stristr(PHP_OS, 'Win')) ? tempnam(file_directory_temp(), 'pdf') : '-';
-  $cmd = realpath($print_pdf_pdf_tool) ." --page-size $print_pdf_paper_size --orientation $print_pdf_page_orientation --dpi $dpi $print_pdf_wkhtmltopdf_options - $pdf_output";
-
-  $process = proc_open($cmd, $descriptor, $pipes, NULL, isset($xvfb_binary) ? array('DISPLAY' => ':'. $xdisplay) : NULL);
-
-  if (is_resource($process)) {
-    fwrite($pipes[0], $html);
-    fclose($pipes[0]);
-
-    if (stristr(PHP_OS, 'Win')) {
-      // It seems Windows can't use the pipe properly so get the contents from a file and then delete it.
-      $pdf = file_get_contents($pdf_output);
-      unlink($pdf_output);
-    }
-    else {
-      $pdf = stream_get_contents($pipes[1]);
-    }
-    fclose($pipes[1]);
-
-    stream_set_blocking($pipes[2], 0);
-    $error = stream_get_contents($pipes[2]);
-    if (!empty($error)) {
-      watchdog('print_pdf', 'wkhtmltopdf: '. $error);
-    }
-    fclose($pipes[2]);
-
-    $retval = proc_terminate($process);
-  }
-
-  $output_dir = file_directory_path() .'/'. $node->type;
-  file_check_directory($output_dir, FILE_CREATE_DIRECTORY);
-  $output_file = $output_dir .'/'. $filename;
-  file_put_contents($output_file, $pdf);
-
-  return $output_file;
-}
--- erp_invoice/erp_invoice.module-org	2011-08-21 15:17:18.000000000 +0200
+++ erp_invoice/erp_invoice.module	2011-08-21 11:00:55.000000000 +0200
@@ -255,6 +255,12 @@
 function erp_invoice_admin_settings() {
   drupal_set_title(t('Invoice settings'));
 
+  $form['erp_invoice_email_sender'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Email address from sender when sending invoices'),
+    '#default_value' => variable_get('erp_invoice_email_sender', ''),
+  );
+
   $form['erp_invoice_email_bcc'] = array(
     '#type' => 'textfield',
     '#title' => t('Email address to BCC when sending invoices'),
--- erp_item/erp_item.module-org	2011-08-21 15:17:18.000000000 +0200
+++ erp_item/erp_item.module	2011-08-21 11:00:55.000000000 +0200
@@ -965,7 +965,7 @@
     $item_node = node_load($node->entries['item'][$i]);
     if ($item_node) {
       $desc = check_plain($item_node->title);
-      if (user_access('item edit')) {
+      if (user_access('item edit') && (!$node->printing)) {
         $code = l($item_node->code, 'node/'. $item_node->nid);
       }
       else {
--- erp_purchase_order/erp_purchase_order.module-org	2011-08-21 15:17:18.000000000 +0200
+++ erp_purchase_order/erp_purchase_order.module	2011-08-21 11:00:55.000000000 +0200
@@ -183,6 +183,11 @@
 function erp_purchase_order_admin_settings() {
   drupal_set_title(t('Purchase order settings'));
 
+  $form['erp_purchase_order_email_sender'] =
+    array('#type' => 'textfield',
+          '#title' => t('Email address from sender when sending purchase orders'),
+          '#default_value' => variable_get('erp_purchase_order_email_sender', ''));
+
   $form['erp_purchase_order_email_bcc'] =
     array('#type' => 'textfield',
           '#title' => t('Email address to BCC when sending purchase orders'),
--- erp_quote/erp_quote.module-org	2011-08-21 15:17:18.000000000 +0200
+++ erp_quote/erp_quote.module	2011-08-21 11:00:55.000000000 +0200
@@ -186,6 +186,11 @@
 function erp_quote_admin_settings() {
   drupal_set_title(t('Quote settings'));
 
+  $form['erp_quote_email_sender'] =
+    array('#type' => 'textfield',
+          '#title' => t('Email address from sender when sending quotes'),
+          '#default_value' => variable_get('erp_quote_email_sender', ''));
+
   $form['erp_quote_email_bcc'] =
     array('#type' => 'textfield',
           '#title' => t('Email address to BCC when sending quotes'),
