You are here

public function LocalStockUpdater::setLocationStockLevel in Commerce Stock 8

Set the location stock level.

Sets the stock level and last transaction for a given location and purchasable entity. Creates first stock level transaction record if none exists.

Parameters

int $location_id: The location id.

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

int $qty: The quantity.

int $last_txn: The last transaction id.

1 call to LocalStockUpdater::setLocationStockLevel()
LocalStockUpdater::updateLocationStockLevel in modules/local_storage/src/LocalStockUpdater.php
Updates the stock level of an entity at a specific location.

File

modules/local_storage/src/LocalStockUpdater.php, line 196

Class

LocalStockUpdater
Class LocalStockUpdater.

Namespace

Drupal\commerce_stock_local

Code

public function setLocationStockLevel($location_id, PurchasableEntityInterface $entity, $qty, $last_txn) {
  $existing = $this->database
    ->select('commerce_stock_location_level', 'll')
    ->fields('ll')
    ->condition('location_id', $location_id)
    ->condition('entity_id', $entity
    ->id())
    ->condition('entity_type', $entity
    ->getEntityTypeId())
    ->execute()
    ->fetch();
  if ($existing) {
    $this->database
      ->update('commerce_stock_location_level')
      ->fields([
      'qty' => $qty,
      'last_transaction_id' => $last_txn,
    ])
      ->condition('location_id', $location_id, '=')
      ->condition('entity_id', $entity
      ->id(), '=')
      ->condition('entity_type', $entity
      ->getEntityTypeId())
      ->execute();
  }
  else {
    $this->database
      ->insert('commerce_stock_location_level')
      ->fields([
      'location_id',
      'entity_id',
      'entity_type',
      'qty',
      'last_transaction_id',
    ])
      ->values([
      $location_id,
      $entity
        ->id(),
      $entity
        ->getEntityTypeId(),
      $qty,
      $last_txn,
    ])
      ->execute();
  }
}