You are here

function _commerce_reports_stock_calculate_dataset in Commerce Reporting 7.4

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

Calculate all the stock reports data.

Returns data for all products, or specific item.

@todo : This is dirty and heavy. Can we trim this up / streamline? Our view is technically querying all products twice :/

Parameters

null|string $sku: Optional SKU to return data for.

null|string $key: Optional data key to return.

bool $reset: Boolean to reset cache or not.

Return value

mixed Returns an array of data, or string if $key was set.

4 calls to _commerce_reports_stock_calculate_dataset()
commerce_reports_stock_handler_field_monthlysales::render in modules/stock/includes/views/handlers/commerce_reports_stock_handler_field_monthlysales.inc
@inheritdoc
commerce_reports_stock_handler_field_stocklifetime::render in modules/stock/includes/views/handlers/commerce_reports_stock_handler_field_stocklifetime.inc
@inheritdoc
commerce_reports_stock_handler_field_weeklysales::render in modules/stock/includes/views/handlers/commerce_reports_stock_handler_field_weeklysales.inc
@inheritdoc
commerce_reports_stock_views_pre_execute in modules/stock/commerce_reports_stock.module
Implements hook_views_pre_execute().

File

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

Code

function _commerce_reports_stock_calculate_dataset($sku = NULL, $key = NULL, $reset = FALSE) {
  $data =& drupal_static(__FUNCTION__);

  // If there is no static cache for dataset yet or a reset was specified...
  if (!isset($data) || $reset) {
    $products = _commerce_reports_stock_get_stock_enabled_products();
    $start = variable_get('commerce_reports_stock_historyperiod', '3 months ago');
    $start = strtotime($start);
    $weekly_sales = _commerce_reports_stock_api_sales('W', $start);
    $monthly_sales = _commerce_reports_stock_api_sales('M', $start);
    $data = array();
    $lifetimes = array();
    $in_stock = array();

    // Go through each product and make calculations.
    foreach ($products as $product) {

      /** @var EntityDrupalWrapper $product_wrapper */
      $product_wrapper = entity_metadata_wrapper('commerce_product', $product);
      $sku = (string) $product_wrapper->sku
        ->value();

      // Provide a default value.
      $stock = 0;

      // @todo: This assumes Commerce Simple Stock was utilized.
      if ($product_wrapper
        ->__isset('commerce_stock')) {
        $stock = (int) $product_wrapper->commerce_stock
          ->value();
      }

      // Compute the number of weeks since start.
      $now = new DateTime();

      // Check if that product is more recent than the history period.
      if ($start < $product_wrapper->created
        ->value()) {
        $start_datetime = new DateTime();
        $start_datetime
          ->setTimestamp($product_wrapper->created
          ->value());
      }
      else {
        $start_datetime = new DateTime(variable_get('commerce_reports_stock_historyperiod', '3 months ago'));
      }
      $number_of_weeks = max($now
        ->diff($start_datetime)->days / 7, 1);
      $weekly_burn = 0;
      if (isset($weekly_sales[$sku])) {
        $weekly_burn = _commerce_reports_stock_calculate_average_sales($weekly_sales[$sku], $number_of_weeks);
      }

      // Compute the number of month since start.
      $number_of_month = max($now
        ->diff($start_datetime)->m, 1);
      $monthly_burn = 0;
      if (isset($monthly_sales[$sku])) {
        $monthly_burn = _commerce_reports_stock_calculate_average_sales($monthly_sales[$sku], $number_of_month);
      }
      $lifetimes[$sku] = _commerce_reports_stock_calculate_lifetime($stock, $weekly_burn);
      $in_stock[$sku] = (bool) $stock;
      $data[$sku] = array(
        'sku' => $sku,
        'stock' => $stock,
        'weeklysales' => sprintf('%0.1f', $weekly_burn),
        'monthlysales' => sprintf('%0.1f', $monthly_burn),
        'lifetime' => $lifetimes[$sku],
      );
    }
    array_multisort($in_stock, SORT_NUMERIC, $lifetimes, SORT_NUMERIC, $data);
  }

  // Check to see if we should return specific data, or whole array.
  if ($key !== NULL && $sku !== NULL) {
    if (isset($data[$sku]) && isset($data[$sku][$key])) {
      return $data[$sku][$key];
    }
    else {
      return $key == 'lifetime' ? 1000 : 0;
    }

    // If we have a key return that, else the whole sku entry.
  }
  elseif ($sku !== NULL) {
    if (isset($data[$sku])) {
      return $data[$sku];
    }
    else {
      return array(
        'sku' => $sku,
        'stock' => $stock,
        'weeklysales' => 0,
        'monthlysales' => 0,
        'lifetime' => 1000,
      );
    }
  }
  else {
    return $data;
  }
}