You are here

function commerce_reports_customers in Commerce Reporting 7.3

Implementation of table showing statistics about customers.

1 call to commerce_reports_customers()
commerce_reports_block_view in ./commerce_reports.module
Implements hook_block_view().
4 string references to 'commerce_reports_customers'
CommerceReportsCustomerTestCase::_test in tests/commerce_reports.test
commerce_reports_block_view in ./commerce_reports.module
Implements hook_block_view().
commerce_reports_commerce_reports_dashboard in ./commerce_reports.module
Implements hook_commerce_reports_dashboard().
commerce_reports_views_default_views in includes/views/commerce_reports.views_default.inc
Implements hook_views_default_views().

File

./commerce_reports.blocks.inc, line 80
Provides all statistics and other features that are not powered by Views.

Code

function commerce_reports_customers() {
  $statistics = array();
  $total_revenue = db_query('SELECT currency_code, SUM(amount) AS amount FROM {commerce_payment_transaction} GROUP by currency_code');
  while ($result = $total_revenue
    ->fetch()) {
    $statistics[] = array(
      t('Total revenue'),
      commerce_currency_format($result->amount, $result->currency_code),
    );
  }
  $total_customers = db_query("SELECT COUNT(uid) FROM (SELECT u.uid AS uid FROM {users} u INNER JOIN {commerce_order} o ON u.uid = o.uid WHERE o.status IN ('completed', 'pending') GROUP BY o.uid) s")
    ->fetchField();
  $new_customers_today = db_query("SELECT COUNT(uid) FROM (SELECT u.uid AS uid FROM {users} u INNER JOIN {commerce_order} o ON u.uid = o.uid WHERE o.status IN ('completed', 'pending') GROUP BY o.uid HAVING MIN(o.created) >= :created) s", array(
    ':created' => strtotime('midnight', REQUEST_TIME),
  ))
    ->fetchField();
  $online_customers = db_query("SELECT COUNT(uid) FROM (SELECT u.uid AS uid FROM {users} u INNER JOIN {commerce_order} o ON u.uid = o.uid WHERE o.status IN ('completed', 'pending') GROUP BY o.uid HAVING o.uid <> 0) s WHERE uid IN (SELECT sess.uid FROM {sessions} sess)")
    ->fetchField();
  $statistics = array_merge($statistics, array(
    array(
      t('Customers total'),
      $total_customers,
    ),
    array(
      t('New customers today'),
      $new_customers_today,
    ),
    array(
      t('Online customers'),
      $online_customers,
    ),
  ));
  return array(
    '#theme' => 'table',
    '#rows' => $statistics,
  );
}