You are here

function _uc_reports_get_sales in Ubercart 5

Same name and namespace in other branches
  1. 6.2 uc_reports/uc_reports.module \_uc_reports_get_sales()

Given a timestamp and time period function returns sales that occurred in that time period

@return: An associative array containing information about sales:

  • "date" => A string representing the day counting was started
  • "income" => The total revenue that occurred during the time period
  • "total" => The total number of orders completed during the time period
  • "average" => The average revenue produced for each order

Parameters

$time: A UNIX time stamp representing the time in which to get sales data

$period: The amount of time over which to count sales (e.g. [1] day, month, year)

2 calls to _uc_reports_get_sales()
uc_reports_sales_summary in uc_reports/uc_reports.module
uc_reports_sales_year in uc_reports/uc_reports.module

File

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

Code

function _uc_reports_get_sales($time, $period = 'day') {
  $timezone = _uc_reports_timezone_offset();

  // Get the current date markers.
  $date = array(
    'day' => format_date($time, 'custom', 'j', 0),
    'month' => format_date($time, 'custom', 'n', 0),
    'year' => format_date($time, 'custom', 'Y', 0),
  );

  // Add one to the granularity chosen, and use it to calc the new time.
  $date[$period] += 1;
  $new_time = gmmktime(0, 0, 0, $date['month'], $date['day'], $date['year']);

  // Set up the default SQL for getting orders with the proper status
  // within this period.
  $order_statuses = _uc_reports_order_statuses();
  $sql_frag = " FROM {uc_orders} as o WHERE o.order_status IN {$order_statuses} AND created >= {$time} and created < {$new_time}";

  // Get the total value of the orders.
  $output = array(
    'income' => 0,
  );
  $orders = db_query("SELECT o.order_total " . $sql_frag);
  while ($order = db_fetch_object($orders)) {
    $output['income'] += $order->order_total;
  }

  // Get the total amount of orders.
  $count = db_result(db_query("SELECT COUNT(o.order_total) " . $sql_frag));
  $output['total'] = $count;

  // Average for this period.
  $output['average'] = $count != 0 ? round($output['income'] / $count, 2) : 0;
  return $output;
}