You are here

public function LocalStockChecker::getLocationStockTransactionSum in Commerce Stock 8

Gets the sum of all stock transactions between a range of transactions.

Parameters

int $location_id: The location id.

\Drupal\commerce\PurchasableEntityInterface $entity: The purchasable entity.

int $min: The minimum transaction number.

int $max: The maximum transaction number.

Return value

int The sum of stock transactions for a given location and purchasable entity.

1 call to LocalStockChecker::getLocationStockTransactionSum()
LocalStockChecker::getLocationsStockLevels in modules/local_storage/src/LocalStockChecker.php
Gets the stock levels for a set of locations.

File

modules/local_storage/src/LocalStockChecker.php, line 125

Class

LocalStockChecker
The stock checker implementation for the local stock module.

Namespace

Drupal\commerce_stock_local

Code

public function getLocationStockTransactionSum($location_id, PurchasableEntityInterface $entity, $min, $max) {
  $query = $this->database
    ->select('commerce_stock_transaction', 'txn')
    ->fields('txn', [
    'location_id',
  ])
    ->condition('location_id', $location_id)
    ->condition('entity_id', $entity
    ->id())
    ->condition('entity_type', $entity
    ->getEntityTypeId())
    ->condition('id', $min, '>');
  if ($max) {
    $query
      ->condition('id', $max, '<=');
  }
  $query
    ->addExpression('SUM(qty)', 'qty');
  $query
    ->groupBy('location_id');
  $result = $query
    ->execute()
    ->fetch();
  return $result ? $result->qty : 0;
}