You are here

commerce_reports.module in Commerce Reporting 7.4

Module file for Commerce Reports.

File

commerce_reports.module
View source
<?php

/**
 * @file
 * Module file for Commerce Reports.
 */

/**
 * Implements hook_help().
 */
function commerce_reports_help($path, $arg) {
  switch ($path) {
    case 'admin/commerce/reports/products':
      return '<p>' . t('The table lists each product listed in the store, its amount sold and the revenue it produced.') . '</p>';
    case 'admin/commerce/reports/customers':
      return '<p>' . t("The following are total orders, products, sales, and average order totals for each store customer. Clicking on the header links will toggle a descending or ascending order. Clicking on a customer's name will take you to their account page.") . '</p>';
    case 'admin/commerce/reports/sales':
      return '<p>' . t('Here you can find a customizable and filterable sales report. You can break down sales monthly, weekly or daily, specify a time period and which order statuses should be included. Each line indicates the total amount of orders, the total revenue and the average revenue per order for a certain day, week or month.') . '</p>';
    case 'admin/commerce/reports/payment-methods':
      return '<p>' . t('Here you can find basic information about the usage of the various payment methods. Each line represents some gateway and statistics associated with that gateway.') . '</p>';
  }
}

/**
 * Implements hook_menu().
 */
function commerce_reports_menu() {
  $items = array();
  $items['admin/commerce/reports'] = array(
    'title' => 'Reports',
    'description' => 'View reports for your store.',
    // Inherited by children.
    'access arguments' => array(
      'access commerce reports',
    ),
    'page callback' => 'commerce_reports_dashboard',
    'file' => 'commerce_reports.dashboard.inc',
    'weight' => -20,
  );
  $items['admin/commerce/reports/dashboard'] = array(
    'title' => 'Dashboard',
    'description' => 'View reports for your store.',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'file' => 'commerce_reports.dashboard.inc',
    'weight' => 0,
  );
  return $items;
}

/**
 * Implements hook_modules_enabled().
 */
function commerce_reports_modules_enabled($modules) {
  if (in_array('views_data_export', $modules)) {

    // Flush Views cache so export displays import.
    views_invalidate_cache();
  }
}

/**
 * Implements hook_theme().
 */
function commerce_reports_theme($existing, $type, $theme, $path) {
  return array(
    'commerce_reports_dashboard_block' => array(
      'render element' => 'block',
      'template' => 'dashboard-block',
      'path' => drupal_get_path('module', 'commerce_reports') . '/theme',
    ),
    'commerce_reports_dashboard_row' => array(
      'render element' => 'row',
      'template' => 'dashboard-row',
      'path' => drupal_get_path('module', 'commerce_reports') . '/theme',
    ),
  );
}

/**
 * Implements hook_menu_local_tasks_alter().
 */
function commerce_reports_menu_local_tasks_alter(&$data, $router_item, $root_path) {

  // Alter local tasks under admin/commerce/reports/sales
  if ($router_item['tab_parent'] == 'admin/commerce/reports/sales' || $router_item['path'] == 'admin/commerce/reports/sales') {
    $data['tabs'][1]['output'][] = array(
      '#theme' => 'menu_local_task',
      '#link' => array(
        'title' => t('Monthly'),
        'href' => 'admin/commerce/reports/sales',
        'localized_options' => array(
          'attributes' => array(
            'title' => t('Monthly reports'),
          ),
        ),
      ),
      '#active' => $router_item['path'] == 'admin/commerce/reports/sales',
    );
  }
}

/**
 * Implements hook_charts_info_alter().
 */
function commerce_reports_charts_info_alter(&$info) {

  // Say the Google charts library supports geo charts.
  $info['google']['types'][] = 'geomap';
}

/**
 * Implements hook_charts_type_info().
 */
function commerce_reports_charts_type_info() {
  $chart_types['geomap'] = array(
    'label' => t('Geomap'),
    // If this chart supports both an X and Y axis, set this to
    // CHARTS_DUAL_AXIS. If only a single axis is supported (e.g. pie), then
    // set this to CHARTS_SINGLE_AXIS.
    'axis' => CHARTS_DUAL_AXIS,
    // Many charting libraries always refer to the main axis as the "y-axis",
    // even if the chart's main axis is horizontal. An example of this is a
    // bar chart, where the values are along the horizontal axis.
    // Meaning x/y axis are flipped.
    'axis_inverted' => TRUE,
  );
  return $chart_types;
}

/**
 * Implements hook_charts_google_visualization_types_alter().
 */
function commerce_reports_charts_google_visualization_types_alter(&$types) {
  $types['geomap'] = 'GeoMap';
}

/**
 * Implements hook_block_info().
 *
 * Defines the custom blocks defined by commerce_reports.
 */
function commerce_reports_block_info() {
  return array(
    'commerce_reports_sales' => array(
      'info' => t('Commerce Reports: Sales statistics'),
    ),
    'commerce_reports_customers' => array(
      'info' => t('Commerce Reports: Customer statistics'),
    ),
  );
}

/**
 * Implements hook_block_view().
 *
 * Implementation of the custom blocks defined by commerce_reports.
 */
function commerce_reports_block_view($delta = '') {
  module_load_include('inc', 'commerce_reports', 'commerce_reports.blocks');
  switch ($delta) {
    case 'commerce_reports_sales':
      return commerce_reports_sales();
    case 'commerce_reports_customers':
      $block_render = module_invoke('views', 'block_view', 'f3f98e42ba2d4d3103b95e9199cf48fe');
      return array(
        'title' => t('Customer statistics'),
        'content' => $block_render['content'],
      );
  }
  return NULL;
}

/**
 * Implements hook_form_alter().
 *
 * Changes the appearance of the exposed form of the sales data report.
 */
function commerce_reports_form_views_exposed_form_alter(&$form, &$form_state, $form_id) {
  if ($form['#id'] == 'views-exposed-form-commerce-reports-sales-page') {
    $form['granularity']['#description'] = t('Large daily reports may take a long time to display.');
    $form['state']['#description'] = t('Only orders with selected statuses will be included in the report.');
  }
}

/**
 * Implements hook_views_pre_render().
 */
function commerce_reports_views_pre_render(&$view) {
  if (strpos($view->name, 'commerce_reports_') !== FALSE) {
    drupal_add_css(drupal_get_path('module', 'commerce_reports') . '/theme/css/commerce-reports.css');
  }
}

/**
 * Implements hook_permission().
 */
function commerce_reports_permission() {
  return array(
    'access commerce reports' => array(
      'title' => t('Access commerce reports'),
    ),
  );
}

/**
 * Implements hook_views_api().
 */
function commerce_reports_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'commerce_reports') . '/includes/views',
  );
}

/**
 * Implements hook_date_format_types().
 *
 * Defines a default date type for commerce_reports.
 */
function commerce_reports_date_formats() {
  return array(
    array(
      'commerce_reports' => 'Commerce Reports',
    ),
  );
}

/**
 * Implements hook_field_formatter_info().
 *
 * Defines a formatter view for the commerce_price field type that works for
 * the commerce_reports_visualization handler.
 */
function commerce_reports_field_formatter_info() {
  return array(
    'commerce_reports_visualization' => array(
      'label' => t('Formatted amount for visualization'),
      'field types' => array(
        'commerce_price',
      ),
      'settings' => array(
        'calculation' => FALSE,
      ),
    ),
  );
}

/**
 * Implements hook_field_formatter_view().
 *
 * Executes the formatter view for the commerce price field type.
 */
function commerce_reports_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $default_currency = commerce_default_currency();
  $element = array();
  foreach ($items as $delta => $item) {
    if (is_null($item['amount'])) {
      continue;
    }
    switch ($display['type']) {
      case 'commerce_reports_visualization':
        if ($item['currency_code'] != $default_currency) {
          $amount = commerce_currency_convert($item['amount'], $item['currency_code'], $default_currency);
        }
        else {
          $amount = $item['amount'];
        }
        $element[$delta] = array(
          '#markup' => commerce_currency_amount_to_decimal($amount, $default_currency),
        );
        break;
    }
  }
  return $element;
}

/**
 * Returns the order states to report on.
 *
 * @return array
 *    Array of order states for Views to filter on.
 */
function commerce_reports_reportable_order_states() {
  $cache =& drupal_static(__FUNCTION__, array());
  if (empty($cache)) {
    $report_states = array(
      'pending',
      'completed',
    );
    drupal_alter('commerce_reports_reportable_order_states', $report_states);

    // Validate states.
    $order_states = commerce_order_states();
    foreach ($report_states as $state) {
      if (!in_array($state, $order_states)) {
        unset($report_states[$state]);
      }
    }

    // Use array_combine to match View's 'value' => 'value',
    $cache = array_combine($report_states, $report_states);
  }
  return $cache;
}

/**
 * Implements hook_commerce_reports_dashboard().
 */
function commerce_reports_commerce_reports_dashboard() {
  return array(
    'overview_today' => array(
      'title' => t('Today'),
      'report' => array(
        'title' => t('Sales reports'),
        'path' => 'admin/commerce/reports/sales',
      ),
      'sections' => array(
        'today' => array(
          'module' => 'views',
          'block' => 'ff7824695a12b49a79aee7d394cbd917',
        ),
      ),
      'weight' => 20,
    ),
    'overview_yesterday' => array(
      'title' => t('Yesterday'),
      'report' => array(
        'title' => t('Sales reports'),
        'path' => 'admin/commerce/reports/sales',
      ),
      'sections' => array(
        'today' => array(
          'module' => 'views',
          'block' => '1a260a710ed0ccc9e8175e7301e597fe',
        ),
      ),
      'weight' => 60,
    ),
    'overview_monthly' => array(
      'title' => t('Last 30 days'),
      'report' => array(
        'title' => t('Sales reports'),
        'path' => 'admin/commerce/reports/sales',
      ),
      'sections' => array(
        'today' => array(
          'module' => 'views',
          'block' => '724c01e6b3c93b66c793ff63f7402697',
        ),
      ),
      'weight' => 80,
    ),
    'sales' => array(
      'title' => t('Sales'),
      'type' => COMMERCE_REPORTS_DASHBOARD_ROW,
      'switchSections' => TRUE,
      'report' => array(
        'title' => t('Sales reports'),
        'path' => 'admin/commerce/reports/sales',
      ),
      'sections' => array(
        'year' => array(
          'title' => t('Year'),
          'module' => 'views',
          'block' => 'cc437fbe6b867b448dc946fd925800a3',
        ),
        'month' => array(
          'title' => t('Month'),
          'module' => 'views',
          'block' => '1127e4706efe2c1eb8537a65a644e572',
        ),
        'week' => array(
          'title' => t('Week'),
          'module' => 'views',
          'block' => 'd70fc459675538d56c73a9f90574211a',
        ),
      ),
      'weight' => 100,
    ),
    'customers' => array(
      'title' => t('Customers'),
      'report' => array(
        'title' => t('Customer reports'),
        'path' => 'admin/commerce/reports/customers',
      ),
      'sections' => array(
        'revenue' => array(
          'module' => 'views',
          'block' => 'f3f98e42ba2d4d3103b95e9199cf48fe',
        ),
      ),
      'weight' => 200,
    ),
    'products' => array(
      'title' => t('Products'),
      'switchSections' => TRUE,
      'report' => array(
        'title' => t('Product reports'),
        'path' => 'admin/commerce/reports/products',
      ),
      'sections' => array(
        'revenue' => array(
          'title' => t('Revenue'),
          'module' => 'views',
          'block' => 'commerce_reports_products-chart_revenue',
        ),
        'quantity' => array(
          'title' => t('Quantity'),
          'module' => 'views',
          'block' => '31eccd6666389cf554f290e91ef9848a',
        ),
      ),
      'weight' => 240,
    ),
    'customer_statistics' => array(
      'title' => t('Customer statistics'),
      'sections' => array(
        'customers' => array(
          'module' => 'views',
          'block' => 'e5caeb6059c44dfa5008c4ff7c6f04be',
        ),
      ),
      'weight' => 280,
    ),
    'billing_information' => array(
      'title' => t('Billing information'),
      'switchSections' => TRUE,
      'type' => COMMERCE_REPORTS_DASHBOARD_BROAD_BLOCK,
      'report' => array(
        'title' => t('Customer profiles'),
        'path' => 'admin/commerce/customer-profiles',
      ),
      'sections' => array(
        'billing_profiles' => array(
          'title' => t('Billing'),
          'module' => 'views',
          'block' => 'c143b512518b3a7c6b30be35b192abbf',
        ),
        'shipping_profiles' => array(
          'title' => t('Shipping'),
          'module' => 'views',
          'block' => '5efb68bd488bc78cae2aac5b8ddc29a6',
        ),
      ),
      'weight' => 300,
    ),
    'sales_statistics' => array(
      'title' => t('Sales statistics'),
      'sections' => array(
        'sales' => array(
          'module' => 'commerce_reports',
          'block' => 'commerce_reports_sales',
        ),
      ),
      'weight' => 310,
    ),
  );
}