You are here

public function StockTransactions2::submitForm in Commerce Stock 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

modules/ui/src/Form/StockTransactions2.php, line 218

Class

StockTransactions2
The second part of a two part create stock transaction form.

Namespace

Drupal\commerce_stock_ui\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $transaction_type = $form_state
    ->getValue('transaction_type');
  $product_variation_id = $form_state
    ->getValue('product_variation_id');
  $source_location = $form_state
    ->getValue('source_location');
  $source_zone = $form_state
    ->getValue('source_zone');
  $qty = $form_state
    ->getValue('transaction_qty');
  $transaction_note = $form_state
    ->getValue('transaction_note');
  $product_variation = $this->productVariationStorage
    ->load($product_variation_id);
  if ($transaction_type == 'receiveStock') {
    $this->stockServiceManager
      ->receiveStock($product_variation, $source_location, $source_zone, $qty, NULL, $currency_code = NULL, $transaction_note);
    $this
      ->messenger()
      ->addMessage($this
      ->t('@qty has been added to "@variation_title" using a "Received Stock" transaction.', [
      '@qty' => $qty,
      '@variation_title' => $product_variation
        ->getTitle(),
    ]));
  }
  elseif ($transaction_type == 'sellStock') {
    $order_id = $form_state
      ->getValue('order');
    $user_id = $form_state
      ->getValue('user');
    $this->stockServiceManager
      ->sellStock($product_variation, $source_location, $source_zone, $qty, NULL, $currency_code = NULL, $order_id, $user_id, $transaction_note);
    $this
      ->messenger()
      ->addMessage($this
      ->t('@qty has been removed from "@variation_title" using a "Sell Stock" transaction.', [
      '@qty' => $qty,
      '@variation_title' => $product_variation
        ->getTitle(),
    ]));
  }
  elseif ($transaction_type == 'returnStock') {
    $order_id = $form_state
      ->getValue('order');
    $user_id = $form_state
      ->getValue('user');
    $this->stockServiceManager
      ->returnStock($product_variation, $source_location, $source_zone, $qty, NULL, $currency_code = NULL, $order_id, $user_id, $transaction_note);
    $this
      ->messenger()
      ->addMessage($this
      ->t('@qty has been added to "@variation_title" using a "Return Stock" transaction.', [
      '@qty' => $qty,
      '@variation_title' => $product_variation
        ->getTitle(),
    ]));
  }
  elseif ($transaction_type == 'moveStock') {
    $target_location = $form_state
      ->getValue('target_location');
    $target_zone = $form_state
      ->getValue('target_zone');
    $this->stockServiceManager
      ->moveStock($product_variation, $source_location, $target_location, $source_zone, $target_zone, $qty, NULL, $currency_code = NULL, $transaction_note);

    // Display notification for end users.
    $target_location_entity = \Drupal::entityTypeManager()
      ->getStorage('commerce_stock_location')
      ->load($target_location);
    $target_location_name = $target_location_entity
      ->getName();
    $source_location_entity = \Drupal::entityTypeManager()
      ->getStorage('commerce_stock_location')
      ->load($source_location);
    $source_location_name = $source_location_entity
      ->getName();
    $this
      ->messenger()
      ->addMessage($this
      ->t('@qty has been moved from "@source_location" to "@target_location" for "@variation_title" using a "Move Stock" transaction.', [
      '@qty' => $qty,
      '@variation_title' => $product_variation
        ->getTitle(),
      '@source_location' => $source_location_name,
      '@target_location' => $target_location_name,
    ]));
  }
}