You are here

commerce_reports.module in Commerce Reporting 7.3

File

commerce_reports.module
View source
<?php

/**
 * 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 statusses 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.',
    'access arguments' => array(
      'access commerce reports',
    ),
    // Inherited by children.
    '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_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_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'),
    ),
    'commerce_reports_customer_map' => array(
      'info' => t('Commerce Reports: Customer map'),
    ),
  );
}

/**
 * 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':
      return commerce_reports_customers();
    case 'commerce_reports_customer_map':
      return commerce_reports_customer_map();
  }
}

/**
 * Implements hook_form_alter().
 *
 * Changes the apperance 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') {
    drupal_add_css(drupal_get_path('module', 'commerce_reports') . '/commerce_reports.css');
    $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_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;
}

/**
 * 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' => 'Year',
          'module' => 'views',
          'block' => 'cc437fbe6b867b448dc946fd925800a3',
        ),
        'month' => array(
          'title' => 'Month',
          'module' => 'views',
          'block' => '1127e4706efe2c1eb8537a65a644e572',
        ),
        'week' => array(
          'title' => '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' => 'Revenue',
          'module' => 'views',
          'block' => 'commerce_reports_products-chart_revenue',
        ),
        'quantity' => array(
          'title' => 'Quantity',
          'module' => 'views',
          'block' => '31eccd6666389cf554f290e91ef9848a',
        ),
      ),
      'weight' => 240,
    ),
    'customer_statistics' => array(
      'title' => t('Customer statistics'),
      'sections' => array(
        'customers' => array(
          'module' => 'commerce_reports',
          'block' => 'commerce_reports_customers',
        ),
      ),
      'weight' => 280,
    ),
    'billing_information' => array(
      'title' => t('Billing information'),
      'type' => COMMERCE_REPORTS_DASHBOARD_BROAD_BLOCK,
      'report' => array(
        'title' => t('Customer profiles'),
        'path' => 'admin/commerce/customer-profiles',
      ),
      'sections' => array(
        'map' => array(
          'module' => 'commerce_reports',
          'block' => 'commerce_reports_customer_map',
        ),
      ),
      'weight' => 300,
    ),
    'sales_statistics' => array(
      'title' => t('Sales statistics'),
      'sections' => array(
        'sales' => array(
          'module' => 'commerce_reports',
          'block' => 'commerce_reports_sales',
        ),
      ),
      'weight' => 310,
    ),
  );
}