You are here

function uc_reports_get_sales in Ubercart 7.3

Returns sales that occurred in a given time period.

Parameters

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

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

Return value

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.
2 calls to uc_reports_get_sales()
uc_reports_sales_summary in uc_reports/uc_reports.admin.inc
Displays the sales summary report.
uc_reports_sales_year in uc_reports/uc_reports.admin.inc
Displays the yearly sales report form and table.

File

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

Code

function uc_reports_get_sales($start, $interval = 'day') {

  // Add one to the granularity chosen, and use it to calc the new time.
  $end = strtotime('+1 ' . $interval, $start) - 1;

  // Set up the default SQL for getting orders with the proper status
  // within this period.
  $order_statuses = uc_reports_order_statuses();

  // Get the total value of the orders.
  $output = array(
    'income' => 0,
  );
  $orders = db_query("SELECT o.order_total FROM {uc_orders} o WHERE o.order_status IN (:statuses) AND :start <= created AND created <= :end", array(
    ':statuses' => $order_statuses,
    ':start' => $start,
    ':end' => $end,
  ));
  while ($order = $orders
    ->fetchObject()) {
    $output['income'] += $order->order_total;
  }

  // Get the total amount of orders.
  $count = db_query("SELECT COUNT(o.order_total) FROM {uc_orders} o WHERE o.order_status IN (:statuses) AND :start <= created AND created <= :end", array(
    ':statuses' => $order_statuses,
    ':start' => $start,
    ':end' => $end,
  ))
    ->fetchField();
  $output['total'] = $count;

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