You are here

function uc_reports_sales_summary in Ubercart 7.3

Same name and namespace in other branches
  1. 5 uc_reports/uc_reports.module \uc_reports_sales_summary()
  2. 6.2 uc_reports/uc_reports.admin.inc \uc_reports_sales_summary()

Displays the sales summary report.

1 string reference to 'uc_reports_sales_summary'
uc_reports_menu in uc_reports/uc_reports.module
Implements hook_menu().

File

uc_reports/uc_reports.admin.inc, line 731
Reports administration menu items.

Code

function uc_reports_sales_summary() {
  $order_statuses = uc_reports_order_statuses();
  $date_day_of_month = date('j');
  $date_month = date('n');
  $month_start = mktime(0, 0, 0, $date_month, 1);
  $month_end = mktime(0, 0, 0, $date_month + 1, 1) - 1;
  $today_start = mktime(0, 0, 0);
  $today_end = mktime(23, 59, 59);

  // Build the report table header.
  $header = array(
    t('Sales data'),
    t('Number of orders'),
    t('Total revenue'),
    t('Average order'),
  );

  // Calculate and add today's sales summary to the report table.
  $today = uc_reports_get_sales($today_start);
  $rows[] = array(
    l(t('Today, !date', array(
      '!date' => format_date($today_start, 'uc_store'),
    )), 'admin/store/orders/search/results/0/0/0/0/0/0/' . $today_start . '/' . $today_end),
    $today['total'],
    array(
      'data' => array(
        '#theme' => 'uc_price',
        '#price' => $today['income'],
      ),
    ),
    array(
      'data' => array(
        '#theme' => 'uc_price',
        '#price' => $today['average'],
      ),
    ),
  );

  // Calculate and add yesterday's sales summary to the report table.
  $yesterday = uc_reports_get_sales($today_start - 86400);
  $rows[] = array(
    l(t('Yesterday, !date', array(
      '!date' => format_date($today_start - 86400, 'uc_store'),
    )), 'admin/store/orders/search/results/0/0/0/0/0/0/' . ($today_start - 86400) . '/' . ($today_end - 86400)),
    $yesterday['total'],
    array(
      'data' => array(
        '#theme' => 'uc_price',
        '#price' => $yesterday['income'],
      ),
    ),
    array(
      'data' => array(
        '#theme' => 'uc_price',
        '#price' => $yesterday['average'],
      ),
    ),
  );

  // Get the sales report for the month.
  $month = uc_reports_get_sales($month_start, 'month');
  $month_title = format_date($month_start, 'custom', 'M Y');

  // Add the month-to-date details to the report table.
  $rows[] = array(
    l(t('Month-to-date, @month', array(
      '@month' => $month_title,
    )), 'admin/store/orders/search/results/0/0/0/0/0/0/' . $month_start . '/' . $month_end),
    $month['total'],
    array(
      'data' => array(
        '#theme' => 'uc_price',
        '#price' => $month['income'],
      ),
    ),
    array(
      'data' => array(
        '#theme' => 'uc_price',
        '#price' => $month['average'],
      ),
    ),
  );

  // Calculate the daily averages for the month.
  $daily_orders = round($month['total'] / $date_day_of_month, 2);
  $daily_revenue = round($month['income'] / $date_day_of_month, 2);
  if ($daily_orders > 0) {
    $daily_average = round($daily_revenue / $daily_orders, 2);
  }
  else {
    $daily_average = 0;
  }

  // Add the daily averages for the month to the report table.
  $rows[] = array(
    t('Daily average for @month', array(
      '@month' => $month_title,
    )),
    $daily_orders,
    array(
      'data' => array(
        '#theme' => 'uc_price',
        '#price' => $daily_revenue,
      ),
    ),
    '',
  );

  // Store the number of days remaining in the month.
  $remaining_days = date('t') - $date_day_of_month;

  // Add the projected totals for the month to the report table.
  $rows[] = array(
    t('Projected totals for @date', array(
      '@date' => $month_title,
    )),
    round($month['total'] + $daily_orders * $remaining_days, 2),
    array(
      'data' => array(
        '#theme' => 'uc_price',
        '#price' => round($month['income'] + $daily_revenue * $remaining_days, 2),
      ),
    ),
    '',
  );

  // Add the sales data report table to the output.
  $build['sales'] = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#attributes' => array(
      'class' => array(
        'uc-sales-table',
      ),
    ),
  );

  // Build the header statistics table header.
  $header = array(
    array(
      'data' => t('Statistics'),
      'width' => '50%',
    ),
    '',
  );
  $rows = array(
    array(
      array(
        'data' => t('Grand total sales'),
      ),
      array(
        'data' => array(
          '#theme' => 'uc_price',
          '#price' => db_query("SELECT SUM(order_total) FROM {uc_orders} WHERE order_status IN (:statuses)", array(
            ':statuses' => $order_statuses,
          ))
            ->fetchField(),
        ),
      ),
    ),
    array(
      array(
        'data' => t('Customers total'),
      ),
      array(
        'data' => db_query("SELECT COUNT(DISTINCT uid) FROM {uc_orders} WHERE order_status IN (:statuses)", array(
          ':statuses' => $order_statuses,
        ))
          ->fetchField(),
      ),
    ),
    array(
      array(
        'data' => t('New customers today'),
      ),
      array(
        'data' => db_query("SELECT COUNT(DISTINCT uid) FROM {uc_orders} WHERE order_status IN (:statuses) AND :start <= created AND created <= :end", array(
          ':statuses' => $order_statuses,
          ':start' => $today_start,
          ':end' => $today_end,
        ))
          ->fetchField(),
      ),
    ),
    array(
      array(
        'data' => t('Online customers'),
      ),
      array(
        'data' => db_query("SELECT COUNT(DISTINCT s.uid) FROM {sessions} s LEFT JOIN {uc_orders} o ON s.uid = o.uid WHERE s.uid > 0 AND o.order_status IN (:statuses)", array(
          ':statuses' => $order_statuses,
        ))
          ->fetchField(),
      ),
    ),
  );

  // Add the statistics table to the output.
  $build['statistics'] = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#attributes' => array(
      'width' => '100%',
      'class' => array(
        'uc-sales-table',
      ),
    ),
  );

  // Build the total orders by status table header.
  $header = array(
    array(
      'data' => t('Total orders by status'),
      'width' => '50%',
    ),
    '',
  );
  $rows = array();
  $unknown = 0;

  // Loop through the order statuses with their total number of orders.
  $result = db_query("SELECT s.order_status_id, order_status, s.title, s.weight, COUNT(o.order_status) as order_count FROM {uc_orders} o LEFT JOIN {uc_order_statuses} s ON s.order_status_id = o.order_status GROUP BY s.order_status_id, order_status, s.title, s.weight ORDER BY s.weight DESC");
  while ($status = $result
    ->fetchAssoc()) {
    if (!empty($status['title'])) {

      // Add the total number of orders with this status to the table.
      $rows[] = array(
        l($status['title'], 'admin/store/orders/view', array(
          'query' => array(
            'order_status' => $status['order_status_id'],
          ),
        )),
        $status['order_count'],
      );
    }
    else {

      // Keep track of the count of orders with an unknown status.
      $unknown += $status['order_count'];
    }
  }

  // Add the unknown status count to the table.
  if ($unknown > 0) {
    $rows[] = array(
      t('Unknown status'),
      $unknown,
    );
  }

  // Add the total orders by status table to the output.
  $build['orders'] = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#attributes' => array(
      'class' => array(
        'uc-sales-table',
      ),
    ),
  );
  return $build;
}