You are here

commerce_invoice_receipt.module in Commerce Invoice Receipt 7

Same filename and directory in other branches
  1. 7.2 commerce_invoice_receipt.module

Provides a printable invoice receipt along with HTML mailing rules.

File

commerce_invoice_receipt.module
View source
<?php

/**
 * @file
 * Provides a printable invoice receipt along with HTML mailing rules.
 */

/**
 * Implements hook_menu().
 */
function commerce_invoice_receipt_menu() {
  $items = array();
  $items['admin/commerce/orders/%commerce_order/view/details'] = array(
    'title' => 'Order details',
    'page callback' => 'commerce_order_ui_order_view',
    'page arguments' => array(
      3,
    ),
    'access callback' => 'commerce_order_admin_order_view_access',
    'access arguments' => array(
      3,
    ),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
    'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
  );
  $items['admin/commerce/orders/%commerce_order/view/print'] = array(
    'title' => 'Printable invoice',
    'page callback' => 'commerce_invoice_receipt_view_print',
    'page arguments' => array(
      3,
    ),
    'access callback' => 'commerce_order_access',
    'access arguments' => array(
      'view',
      3,
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 1,
    'parent' => 'admin/commerce/orders/%commerce_order',
    'file' => 'includes/commerce_invoice_receipt.admin.inc',
  );
  $items['admin/commerce/orders/%commerce_order/view/mail'] = array(
    'title' => 'Email the invoice',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'commerce_invoice_receipt_mail_form',
      3,
    ),
    'access callback' => 'commerce_order_access',
    'access arguments' => array(
      'view',
      3,
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 2,
    'parent' => 'admin/commerce/orders/%commerce_order',
    'file' => 'includes/commerce_invoice_receipt.admin.inc',
  );
  $items['admin/commerce/orders/%commerce_order/edit/edit'] = array(
    'title' => 'Edit order',
    'page callback' => 'commerce_order_ui_order_form_wrapper',
    'page arguments' => array(
      3,
    ),
    'access callback' => 'commerce_order_access',
    'access arguments' => array(
      'update',
      3,
    ),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -5,
    'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
    'file path' => drupal_get_path('module', 'commerce_order_ui'),
    'file' => 'includes/commerce_order_ui.orders.inc',
  );
  $items['admin/commerce/orders/%commerce_order/edit/print'] = array(
    'title' => 'Printable invoice',
    'page callback' => 'commerce_invoice_receipt_view_print',
    'page arguments' => array(
      3,
    ),
    'access callback' => 'commerce_order_access',
    'access arguments' => array(
      'view',
      3,
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 1,
    'parent' => 'admin/commerce/orders/%commerce_order',
    'file' => 'includes/commerce_invoice_receipt.admin.inc',
  );
  $items['admin/commerce/orders/%commerce_order/edit/mail'] = array(
    'title' => 'Email the invoice',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'commerce_invoice_receipt_mail_form',
      3,
    ),
    'access callback' => 'commerce_order_access',
    'access arguments' => array(
      'view',
      3,
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 2,
    'parent' => 'admin/commerce/orders/%commerce_order',
    'file' => 'includes/commerce_invoice_receipt.admin.inc',
  );
  $items['user/%user/orders/%commerce_order/print'] = array(
    'title' => 'Printable Invoice',
    'page callback' => 'commerce_invoice_receipt_view_print',
    'page arguments' => array(
      3,
    ),
    'access callback' => 'commerce_order_customer_order_view_access',
    'access arguments' => array(
      3,
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 2,
    'file' => 'includes/commerce_invoice_receipt.admin.inc',
  );
  $items['user/%user/orders/%commerce_order/view'] = array(
    'title' => 'View',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => 1,
  );
  $items['admin/commerce/config/commerce_invoice_receipt'] = array(
    'title' => 'Invoice Receipt',
    'description' => 'Extra Commerce Invoice Receipt options.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'commerce_invoice_receipt_config',
    ),
    'access arguments' => array(
      'administer commerce_order entities',
    ),
  );
  return $items;
}
function commerce_invoice_receipt_css_path() {

  // Get current default theme
  $default_theme_path = drupal_get_path('theme', variable_get('theme_default', NULL));
  $default_template_css = drupal_get_path('module', 'commerce_invoice_receipt') . '/theme/commerce_invoice_receipt.css';

  // Check if the default theme wants to override the invoice stylesheet.
  $files = file_scan_directory($default_theme_path, '/commerce_invoice_receipt.css$/');
  if (count($files)) {
    $default_template_css = key($files);
  }
  return $default_template_css;
}
function commerce_invoice_receipt_view_print($order, $view_mode = 'invoice', $breadcrumb = TRUE) {
  $styles[] = array(
    'type' => 'file',
    'media' => 'all',
    'data' => commerce_invoice_receipt_css_path(),
    'group' => CSS_DEFAULT,
    'every_page' => FALSE,
    'weight' => 0,
    'preprocess' => FALSE,
    'browsers' => array(),
  );
  $html = drupal_get_css($styles);
  if (module_exists('views_ui')) {
    views_ui_contextual_links_suppress(TRUE);
  }
  $build = entity_view('commerce_order', array(
    $order->order_id => $order,
  ), $view_mode, NULL, TRUE);
  $invoice_info = _commerce_invoice_receipt_get_invoice_info($order, $build);
  $html .= theme('commerce_order_invoice_view', array(
    'info' => $invoice_info,
    'order' => $order,
  ));
  print _emogrifier_process($html, NULL, NULL, NULL, NULL, NULL);
}

/**
 * Implements hook_entity_info_alter().
 */
function commerce_invoice_receipt_entity_info_alter(&$entity_info) {
  $entity_info['commerce_order']['view modes']['invoice'] = array(
    'label' => t('Invoice/Receipt'),
    'custom settings' => TRUE,
  );
}

/**
 * hook_menu callback for a configuration settings page.
 *
 * Currently just implements an email to bcc on all invoices.
 */
function commerce_invoice_receipt_config() {
  $form = array();
  $form['commerce_invoice_send_from'] = array(
    '#type' => 'textfield',
    '#title' => t('Send From'),
    '#description' => t('Email address from which all invoices will be sent. If nothing is entered, Invoice Receipt will use the main email address for whole website instead. This is currently set to: %email.', array(
      '%email' => variable_get('site_email'),
    )),
    '#default_value' => variable_get('commerce_invoice_send_from'),
  );
  $form['commerce_invoice_copy_all_user'] = array(
    '#type' => 'textfield',
    '#title' => t('Copy Email'),
    '#default_value' => variable_get('commerce_invoice_copy_all_user', ''),
    '#size' => 40,
    '#maxlength' => 255,
    '#description' => t('Email which is copied on all commerce invoice receipts.'),
  );
  $form['commerce_invoice_copy_all_user_method'] = array(
    '#type' => 'select',
    '#options' => array(
      'Bcc' => 'BCC',
      'Cc' => 'CC',
    ),
    '#title' => t('Method'),
    '#default_value' => variable_get('commerce_invoice_copy_all_user_method', 'Bcc'),
    '#size' => 1,
    '#description' => t('What method to use when adding the email address.'),
    '#required' => TRUE,
  );
  return system_settings_form($form);
}

/**
* Implements hook_theme().
*/
function commerce_invoice_receipt_theme() {

  // Get current default theme
  $default_theme_path = drupal_get_path('theme', variable_get('theme_default', NULL));
  $default_template_path = drupal_get_path('module', 'commerce_invoice_receipt') . '/theme';

  // Check if the template has been copied in the front end theme.
  //
  // A manual check is necessary because otherwise Drupal will not find the
  // template override in the front end theme, when the site uses an admin theme
  // (e.g. when an administrator tries to see printable invoice from the back
  // end).
  //
  // This way the template will always be fetched from the front end theme if
  // there is one.
  //
  // Also, this way the template file can be located anywhere in the front end
  // theme (root, templates/, invoice/, or any other subfolder).
  $files = file_scan_directory($default_theme_path, '/commerce-order-invoice-view.tpl.php$/');
  if (count($files)) {
    $default_template_path = $default_theme_path;
  }
  return array(
    'commerce_order_invoice_view' => array(
      'variables' => array(
        'info' => NULL,
        'order' => NULL,
      ),
      'path' => $default_template_path,
      'template' => 'commerce-order-invoice-view',
    ),
  );
}

/**
* Implements hook_mail().
*/
function commerce_invoice_receipt_mail($key, &$message, $params) {
  $langcode = isset($message['language']) ? $message['language']->language : NULL;
  $options = array(
    'langcode' => $langcode,
    'context' => '',
  );
  switch ($key) {

    // Setup an e-mailed invoice.
    case 'invoice':
      $styles[] = array(
        'type' => 'file',
        'media' => 'all',
        'data' => commerce_invoice_receipt_css_path(),
        'group' => CSS_DEFAULT,
        'every_page' => FALSE,
        'weight' => 0,
        'preprocess' => FALSE,
        'browsers' => array(),
      );
      $html = drupal_get_css($styles);
      if (module_exists('views_ui')) {
        views_ui_contextual_links_suppress(TRUE);
      }
      $build = entity_view('commerce_order', array(
        $params['order']->order_id => $params['order'],
      ), 'invoice', NULL, TRUE);
      $invoice_info = _commerce_invoice_receipt_get_invoice_info($params['order'], $build);
      if (isset($params['headers'])) {
        $message['headers'] = $params['headers'];
      }
      $message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed;';
      $message['subject'] = isset($params['subject']) ? $params['subject'] : t('Your Order Invoice', array(), $options);
      $html .= theme('commerce_order_invoice_view', array(
        'info' => $invoice_info,
        'order' => $params['order'],
      ));
      $message['body'][] = _emogrifier_process($html, NULL, NULL, NULL, NULL, NULL);
      break;
  }
}

/**
 * Implements hook_rules_action_info().
 */
function commerce_invoice_receipt_rules_action_info() {
  return array(
    'commerce_invoice_receipt_action_mail' => array(
      'label' => t('Send invoice receipt email'),
      'group' => t('Commerce Invoice Receipt'),
      'parameter' => array(
        'commerce_order' => array(
          'type' => 'commerce_order',
          'label' => t('Order'),
          'description' => t('The order whose line items should be checked for the specified product. If the specified order does not exist, the comparison will act as if it is against a quantity of 0.'),
        ),
        'to' => array(
          'type' => 'text',
          'label' => t('To'),
          'description' => t('The e-mail address or addresses where the message will be sent to. The formatting of this string must comply with RFC 2822. Separate multiple addresses with a comma.'),
          'default value' => '[commerce-order:owner] <[commerce-order:mail]>',
        ),
        'subject' => array(
          'type' => 'text',
          'label' => t('Subject'),
          'description' => t('Email subject.'),
          'default value' => t('!site_name: Order #!order_id', array(
            '!site_name' => '[site:name]',
            '!order_id' => '[commerce-order:order-id]',
          )),
        ),
        'from' => array(
          'type' => 'text',
          'label' => t('From'),
          'description' => t('Email from address. Leave it empty to use the site-wide configured address.'),
          'optional' => TRUE,
        ),
      ),
      'callbacks' => array(
        'execute' => 'commerce_invoice_receipt_action_mail',
      ),
    ),
  );
}
function commerce_invoice_receipt_action_mail($order, $to, $subject, $from = NULL) {
  $to = str_replace(array(
    "\r",
    "\n",
  ), '', $to);
  $from = !empty($from) ? str_replace(array(
    "\r",
    "\n",
  ), '', $from) : NULL;
  $recipients = explode(',', $to);
  $params = array(
    'subject' => $subject,
    'order' => $order,
  );
  $account = user_load($order->uid);
  $language = user_preferred_language($account);
  foreach ($recipients as $recipient) {
    if ($recipient == '') {
      continue;
    }
    $message = drupal_mail('commerce_invoice_receipt', 'invoice', $recipient, $language, $params, $from);
    if ($message['result']) {
      watchdog('rules', 'Successfully sent email to %recipient', array(
        '%recipient' => $recipient,
      ));
    }
  }
}

/**
 * Generate an array for rendering all the invoice info.
 */
function _commerce_invoice_receipt_get_invoice_info($order, $build) {
  $info = array(
    'order_uid' => $order->uid,
    'order_created' => $order->created,
    'order_changed' => $order->changed,
    'order_number' => $order->order_number,
    'order_mail' => $order->mail,
    'order_status' => $order->status,
    'site_logo' => theme_get_setting('logo', variable_get('theme_default', NULL)),
  );

  // Adding a drupal_alter for other modules to edit the $info array prior to
  // being displayed.
  drupal_alter('commerce_invoice_receipt_info', $info);
  if (isset($build['commerce_order'][$order->order_id]['commerce_customer_shipping'])) {
    $info['customer_shipping'] = $build['commerce_order'][$order->order_id]['commerce_customer_shipping'][0]['#markup'];
  }
  if (isset($build['commerce_order'][$order->order_id]['commerce_customer_billing'][0]['#markup'])) {
    $info['customer_billing'] = $build['commerce_order'][$order->order_id]['commerce_customer_billing'][0]['#markup'];
  }
  if (isset($build['commerce_order'][$order->order_id]['commerce_line_items'])) {
    $info['line_items'] = $build['commerce_order'][$order->order_id]['commerce_line_items'][0]['#markup'];
  }
  if (isset($build['commerce_order'][$order->order_id]['commerce_order_total'])) {
    $info['order_total'] = $build['commerce_order'][$order->order_id]['commerce_order_total'][0]['#markup'];
  }
  return $info;
}