You are here

function uc_reports_sales_summary in Ubercart 5

Same name and namespace in other branches
  1. 6.2 uc_reports/uc_reports.admin.inc \uc_reports_sales_summary()
  2. 7.3 uc_reports/uc_reports.admin.inc \uc_reports_sales_summary()
1 string reference to 'uc_reports_sales_summary'
uc_reports_menu in uc_reports/uc_reports.module
Implementation of hook_menu().

File

uc_reports/uc_reports.module, line 718
Displays reports on sales, customers, and products to store admin

Code

function uc_reports_sales_summary() {
  $timezone_offset = time() + _uc_reports_timezone_offset();
  $order_statuses = _uc_reports_order_statuses();
  $format = variable_get('uc_date_format_default', 'm/d/Y');
  $date_month = gmdate('n', $timezone_offset);
  $date_year = gmdate('Y', $timezone_offset);
  $date_day_of_month = gmdate('j', $timezone_offset);
  $date_days_in_month = gmdate('t', $timezone_offset);
  $month_start = gmmktime(0, 0, 0, $date_month, 1, $date_year);
  $month_end = gmmktime(23, 59, 59, $date_month, $date_days_in_month, $date_year);
  $today_start = gmmktime(0, 0, 0, $date_month, $date_day_of_month, $date_year);
  $today_end = gmmktime(23, 59, 59, $date_month, $date_day_of_month, $date_year);

  // 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, 'custom', $format, 0),
    )), 'admin/store/orders/search/results/0/0/0/0/0/0/' . $today_start . '/' . $today_end),
    $today['total'],
    uc_currency_format($today['income']),
    uc_currency_format($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, 'custom', $format, 0),
    )), 'admin/store/orders/search/results/0/0/0/0/0/0/' . ($today_start - 86400) . '/' . ($today_end - 86400)),
    $yesterday['total'],
    uc_currency_format($yesterday['income']),
    uc_currency_format($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'],
    uc_currency_format($month['income']),
    uc_currency_format($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,
    uc_currency_format($daily_revenue),
    '',
  );

  // Store the number of days remaining in the month.
  $remaining_days = $date_days_in_month - $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),
    uc_currency_format(round($month['income'] + $daily_revenue * $remaining_days, 2)),
    '',
  );

  // Add the sales data report table to the output.
  $output = theme('table', $header, $rows, array(
    'class' => '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' => uc_currency_format(db_result(db_query("SELECT SUM(order_total) FROM {uc_orders} WHERE order_status IN {$order_statuses}"))),
      ),
    ),
    array(
      array(
        'data' => t('Customers total'),
      ),
      array(
        'data' => db_result(db_query("SELECT COUNT(DISTINCT uid) FROM {uc_orders} WHERE order_status IN {$order_statuses}")),
      ),
    ),
    array(
      array(
        'data' => t('New customers today'),
      ),
      array(
        'data' => db_result(db_query("SELECT COUNT(DISTINCT uid) FROM {uc_orders} WHERE order_status IN {$order_statuses} AND %d >= created AND created >= %d", $today_end, $today_start)),
      ),
    ),
    array(
      array(
        'data' => t('Online customers'),
      ),
      array(
        'data' => db_result(db_query("SELECT COUNT(DISTINCT s.uid) FROM {sessions} as s LEFT JOIN {uc_orders} as o ON s.uid = o.uid WHERE s.uid > 0 AND o.order_status IN {$order_statuses}")),
      ),
    ),
  );

  // Add the statistics table to the output.
  $output .= theme('table', $header, $rows, array(
    'width' => '100%',
    'class' => '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} as o LEFT JOIN {uc_order_statuses} as 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 = db_fetch_array($result)) {
    if (!empty($status['title'])) {

      // Add the total number of orders with this status to the table.
      $rows[] = array(
        l($status['title'], 'admin/store/orders/sort/' . $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.
  $output .= theme('table', $header, $rows, array(
    'class' => 'uc-sales-table',
  ));
  return $output;
}