You are here

public function Reports::get_sales in Ubercart 8.4

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 Reports::get_sales()
Reports::sales in uc_report/src/Controller/Reports.php
Displays the sales summary report.
Reports::yearSales in uc_report/src/Controller/Reports.php
Displays the yearly sales report form and table.

File

uc_report/src/Controller/Reports.php, line 1004

Class

Reports
Provides reports for Ubercart.

Namespace

Drupal\uc_report\Controller

Code

public function 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_report_order_statuses();

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

  // Get the total amount of orders.
  $count = $this->database
    ->query("SELECT COUNT(o.order_total) FROM {uc_orders} o WHERE o.order_status IN (:statuses[]) AND :start <= created AND created <= :end", [
    ':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;
}