You are here

public function ShipmentEditForm::buildForm in Ubercart 8.4

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

shipping/uc_fulfillment/src/Form/ShipmentEditForm.php, line 70

Class

ShipmentEditForm
Creates or edits a shipment.

Namespace

Drupal\uc_fulfillment\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, OrderInterface $uc_order = NULL, ShipmentInterface $uc_shipment = NULL) {
  $this->order_id = $uc_order
    ->id();
  $this->shipment = $uc_shipment;
  $addresses = [];
  $form['packages'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Packages'),
    '#tree' => TRUE,
  ];
  if (NULL != $this->shipment
    ->getOrigin()) {
    $addresses[] = $this->shipment
      ->getOrigin();
  }
  foreach ($this->shipment
    ->getPackages() as $id => $package) {
    foreach ($package
      ->getAddresses() as $address) {
      if (!in_array($address, $addresses)) {
        $addresses[] = $address;
      }
    }

    // Create list of products and get a representative product (last one in
    // the loop) to use for some default values.
    $product_list = [];
    $declared_value = 0;
    $products_weight = 0;
    foreach ($package
      ->getProducts() as $product) {
      $product_list[] = $product->qty . ' x ' . $product->model;
      $declared_value += $product->qty * $product->price;
      $products_weight += $product->qty * $product->weight * uc_weight_conversion($product->weight_units, $package
        ->getWeightUnits());
    }
    $pkg_form = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Package @id', [
        '@id' => $id,
      ]),
    ];
    $pkg_form['products'] = [
      '#theme' => 'item_list',
      '#items' => $product_list,
    ];
    $pkg_form['pkg_type'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Package type'),
      '#default_value' => $package
        ->getPackageType(),
      '#description' => $this
        ->t('For example: Box, pallet, tube, envelope, etc.'),
    ];
    if (isset($method) && is_array($method['ship']['pkg_types'])) {
      $pkg_form['pkg_type']['#type'] = 'select';
      $pkg_form['pkg_type']['#options'] = $method['ship']['pkg_types'];
      $pkg_form['pkg_type']['#description'] = '';
    }
    $pkg_form['declared_value'] = [
      '#type' => 'uc_price',
      '#title' => $this
        ->t('Declared value'),
      '#default_value' => !empty($package
        ->getValue()) ? $package
        ->getValue() : $declared_value,
    ];
    $pkg_form['weight'] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'uc-inline-form',
          'clearfix',
        ],
      ],
      '#description' => $this
        ->t('Weight of the package. Default value is sum of product weights in the package.'),
      '#weight' => 15,
    ];
    $pkg_form['weight']['weight'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Weight'),
      '#min' => 0,
      '#step' => 'any',
      '#default_value' => !empty($package
        ->getWeight()) ? $package
        ->getWeight() : $products_weight,
      '#size' => 10,
    ];
    $pkg_form['weight']['units'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Units'),
      '#options' => [
        'lb' => $this
          ->t('Pounds'),
        'kg' => $this
          ->t('Kilograms'),
        'oz' => $this
          ->t('Ounces'),
        'g' => $this
          ->t('Grams'),
      ],
      '#default_value' => $package
        ->getWeightUnits(),
    ];
    $pkg_form['dimensions'] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'uc-inline-form',
          'clearfix',
        ],
      ],
      '#title' => $this
        ->t('Dimensions'),
      '#description' => $this
        ->t('Physical dimensions of the packaged product.'),
      '#weight' => 20,
    ];
    $pkg_form['dimensions']['length'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Length'),
      '#min' => 0,
      '#step' => 'any',
      '#default_value' => $package
        ->getLength(),
      '#size' => 8,
    ];
    $pkg_form['dimensions']['width'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Width'),
      '#min' => 0,
      '#step' => 'any',
      '#default_value' => $package
        ->getWidth(),
      '#size' => 8,
    ];
    $pkg_form['dimensions']['height'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Height'),
      '#min' => 0,
      '#step' => 'any',
      '#default_value' => $package
        ->getHeight(),
      '#size' => 8,
    ];
    $pkg_form['dimensions']['units'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Units of measurement'),
      '#options' => [
        'in' => $this
          ->t('Inches'),
        'ft' => $this
          ->t('Feet'),
        'cm' => $this
          ->t('Centimeters'),
        'mm' => $this
          ->t('Millimeters'),
      ],
      '#default_value' => $package
        ->getLengthUnits(),
    ];
    $pkg_form['tracking_number'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Tracking number'),
      '#default_value' => $package
        ->getTrackingNumber(),
    ];
    $form['packages'][$id] = $pkg_form;
  }

  // If the destination address has been edited, copy it to the
  // order's delivery address.
  if ($this->shipment
    ->getDestination()) {
    $uc_order
      ->setAddress('delivery', $this->shipment
      ->getDestination());
  }
  $form += \Drupal::formBuilder()
    ->getForm('\\Drupal\\uc_fulfillment\\Form\\AddressForm', $addresses, $uc_order);
  $form['shipment'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Shipment data'),
  ];

  // Determine shipping option chosen by the customer.
  $message = '';
  if (isset($uc_order->quote['method'])) {

    // Order has a quote attached.
    $method = $uc_order->quote['method'];
    $methods = \Drupal::moduleHandler()
      ->invokeAll('uc_fulfillment_method');
    if (isset($methods[$method])) {

      // Quote is from a currently-active shipping method.
      $services = $methods[$method]['quote']['accessorials'];
      $method = $services[$uc_order->quote['accessorials']];
    }
    $message = $this
      ->t('Customer selected "@method" as the shipping method and paid @rate', [
      '@method' => $method,
      '@rate' => uc_currency_format($uc_order->quote['rate']),
    ]);
  }
  else {

    // No quotes for this order.
    $message = $this
      ->t('There are no shipping quotes attached to this order. Customer was not charged for shipping.');
  }

  // Inform administrator of customer's shipping choice.
  $form['shipment']['shipping_choice'] = [
    '#type' => 'container',
    '#markup' => $message,
  ];
  $form['shipment']['shipping_method'] = [
    '#type' => 'hidden',
    '#value' => $this->shipment
      ->getShippingMethod(),
  ];
  $form['shipment']['carrier'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Carrier'),
    '#default_value' => $this->shipment
      ->getCarrier(),
  ];
  $form['shipment']['accessorials'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Shipment options'),
    '#default_value' => $this->shipment
      ->getAccessorials(),
    '#description' => $this
      ->t('Short notes about the shipment, e.g. residential, overnight, etc.'),
  ];
  $form['shipment']['transaction_id'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Transaction ID'),
    '#default_value' => $this->shipment
      ->getTransactionId(),
  ];
  $form['shipment']['tracking_number'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Tracking number'),
    '#default_value' => $this->shipment
      ->getTrackingNumber(),
  ];
  $ship_date = $this->time
    ->getRequestTime();
  if ($this->shipment
    ->getShipDate()) {
    $ship_date = $this->shipment
      ->getShipDate();
  }
  $exp_delivery = $this->time
    ->getRequestTime();
  if ($this->shipment
    ->getExpectedDelivery()) {
    $exp_delivery = $this->shipment
      ->getExpectedDelivery();
  }
  $form['shipment']['ship_date'] = [
    '#type' => 'datetime',
    '#title' => $this
      ->t('Ship date'),
    '#date_date_element' => 'date',
    '#date_time_element' => 'none',
    '#default_value' => DrupalDateTime::createFromTimestamp($ship_date),
  ];
  $form['shipment']['expected_delivery'] = [
    '#type' => 'datetime',
    '#title' => $this
      ->t('Expected delivery'),
    '#date_date_element' => 'date',
    '#date_time_element' => 'none',
    '#default_value' => DrupalDateTime::createFromTimestamp($exp_delivery),
  ];
  $form['shipment']['cost'] = [
    '#type' => 'uc_price',
    '#title' => $this
      ->t('Shipping cost'),
    '#default_value' => $this->shipment
      ->getCost(),
  ];
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save shipment'),
    '#weight' => 10,
  ];
  return $form;
}