You are here

function uc_reports_sales_summary in Ubercart 6.2

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

Display 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 644
Reports administration menu items.

Code

function uc_reports_sales_summary() {

  // "Now" timestamp
  $time = time();

  // Site time minus GMT time, in seconds
  $timezone_offset = _uc_reports_timezone_offset();

  // Find day/month/year of "Now" in site timezone
  $date_month = format_date($time, 'custom', 'n', $timezone_offset);
  $date_year = format_date($time, 'custom', 'Y', $timezone_offset);
  $date_day_of_month = format_date($time, 'custom', 'j', $timezone_offset);
  $date_days_in_month = format_date($time, 'custom', 't', $timezone_offset);

  // Calculate Unix timecodes (defined to be in GMT time)
  // for beginning and ending of reporting periods
  // Use gm functions so PHP won't try to do any timezone conversions by itself
  $month_start = gmmktime(0, 0, 0, $date_month, 1, $date_year) - $timezone_offset;
  $month_end = gmmktime(23, 59, 59, $date_month, $date_days_in_month, $date_year) - $timezone_offset;
  $today_start = gmmktime(0, 0, 0, $date_month, $date_day_of_month, $date_year) - $timezone_offset;
  $today_end = gmmktime(23, 59, 59, $date_month, $date_day_of_month, $date_year) - $timezone_offset;

  // Initialize variables used later
  $order_statuses = _uc_reports_order_statuses();
  $format = variable_get('uc_date_format_default', 'm/d/Y');

  // 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);
  $context = array(
    'revision' => 'themed-original',
    'type' => 'amount',
  );
  $rows[] = array(
    l(t('Today, !date', array(
      '!date' => format_date($today_start, 'custom', $format, $timezone_offset),
    )), 'admin/store/orders/search/results/0/0/0/0/0/0/' . $today_start . '/' . $today_end),
    $today['total'],
    uc_price($today['income'], $context),
    uc_price($today['average'], $context),
  );

  // 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, $timezone_offset),
    )), 'admin/store/orders/search/results/0/0/0/0/0/0/' . ($today_start - 86400) . '/' . ($today_end - 86400)),
    $yesterday['total'],
    uc_price($yesterday['income'], $context),
    uc_price($yesterday['average'], $context),
  );

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

  // 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_price($month['income'], $context),
    uc_price($month['average'], $context),
  );

  // 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_price($daily_revenue, $context),
    '',
  );

  // 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_price(round($month['income'] + $daily_revenue * $remaining_days, 2), $context),
    '',
  );

  // 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_price(db_result(db_query("SELECT SUM(order_total) FROM {uc_orders} WHERE order_status IN {$order_statuses}")), $context),
      ),
    ),
    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;
}