You are here

function _commerce_reports_stock_api_sales in Commerce Reporting 7.4

Same name and namespace in other branches
  1. 7.3 modules/stock/commerce_reports_stock.module \_commerce_reports_stock_api_sales()

Group sales by year, month, day or week.

Parameters

string $interval: Interval to group sales by. Valid options: D, W, M, Y.

int $start: Timestamp to check creation after.

Return value

array An array of products.

1 call to _commerce_reports_stock_api_sales()
_commerce_reports_stock_calculate_dataset in modules/stock/commerce_reports_stock.module
Calculate all the stock reports data.

File

modules/stock/commerce_reports_stock.module, line 228
This module provides advanced stock reporting for Drupal Commerce.

Code

function _commerce_reports_stock_api_sales($interval = 'D', $start = 0) {
  $formats = array(
    'D' => '%Y-%m-%d',
    'W' => '%Y-%u',
    'M' => '%Y-%m',
    'Y' => '%Y',
  );
  $format = $formats[$interval];
  $query = sprintf("\n       SELECT DATE_FORMAT(FROM_UNIXTIME(o.created), '%s') AS date,\n              line_item_label AS sku,\n              SUM(quantity) AS sales\n         FROM commerce_line_item li\n    LEFT JOIN commerce_order o\n           ON li.order_id = o.order_id\n        WHERE o.status = 'completed'\n          AND li.type = 'product'\n          AND o.created >= :created\n     GROUP BY li.line_item_label, date", $format);
  $res = db_query($query, array(
    ':created' => $start,
  ));
  $data = array();
  foreach ($res as $row) {
    $data[$row->sku][$row->date] = (int) $row->sales;
  }
  return $data;
}