You are here

public function StockTransactions2::buildForm in Commerce Stock 8

Form constructor.

Parameters

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

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

Return value

array The form structure.

Overrides FormInterface::buildForm

File

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

Class

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

Namespace

Drupal\commerce_stock_ui\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  if ($this->request->query
    ->has('commerce_product_v_id')) {
    $variation_id = $this->request->query
      ->get('commerce_product_v_id');
  }
  else {
    return $this
      ->redirect('commerce_stock_ui.stock_transactions1');
  }
  $product_variation = $this->productVariationStorage
    ->load($variation_id);
  $stockService = $this->stockServiceManager
    ->getService($product_variation);
  $locations = $stockService
    ->getStockChecker()
    ->getLocationList(TRUE);
  $location_options = [];

  /** @var \Drupal\commerce_stock\StockLocationInterface $location */
  foreach ($locations as $location) {
    $location_options[$location
      ->getId()] = $location
      ->getName();
  }
  $form['transaction_type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Transaction type'),
    '#options' => [
      'receiveStock' => $this
        ->t('Receive stock'),
      'sellStock' => $this
        ->t('Sell stock'),
      'returnStock' => $this
        ->t('Return stock'),
      'moveStock' => $this
        ->t('Move stock'),
    ],
  ];
  $form['product_variation_id'] = [
    '#type' => 'value',
    '#value' => $variation_id,
  ];
  $form['source'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Source location'),
  ];
  $form['source']['source_location'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Location'),
    '#options' => $location_options,
  ];
  $form['source']['source_zone'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Zone/Bins'),
    '#description' => $this
      ->t('The location zone (bins)'),
    '#size' => 60,
    '#maxlength' => 50,
  ];
  $form['target'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Move target'),
    '#states' => [
      'visible' => [
        ':input[name="transaction_type"]' => [
          'value' => 'moveStock',
        ],
      ],
    ],
  ];
  $form['target']['target_location'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Target Location'),
    '#options' => $location_options,
  ];
  $form['target']['target_zone'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Zone/Bins'),
    '#description' => $this
      ->t('The location zone (bins)'),
    '#size' => 60,
    '#maxlength' => 50,
  ];
  $form['user'] = [
    '#type' => 'entity_autocomplete',
    '#title' => $this
      ->t('Optional user'),
    '#target_type' => 'user',
    '#selection_handler' => 'default',
    '#states' => [
      'visible' => [
        ':input[name="transaction_type"]' => [
          [
            'value' => 'sellStock',
          ],
          [
            'value' => 'returnStock',
          ],
        ],
      ],
    ],
  ];
  $form['order'] = [
    '#type' => 'entity_autocomplete',
    '#title' => $this
      ->t('Optional order number'),
    '#target_type' => 'commerce_order',
    '#selection_handler' => 'default',
    '#states' => [
      'visible' => [
        ':input[name="transaction_type"]' => [
          [
            'value' => 'sellStock',
          ],
          [
            'value' => 'returnStock',
          ],
        ],
      ],
    ],
  ];
  $form['transaction_qty'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Quantity'),
    '#default_value' => '1',
    '#step' => '0.01',
    '#required' => TRUE,
  ];
  $form['transaction_note'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Note'),
    '#description' => $this
      ->t('A note for the transaction'),
    '#maxlength' => 64,
    '#size' => 64,
  ];
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Submit'),
  ];
  return $form;
}