You are here

public function ShipmentController::listOrderShipments in Ubercart 8.4

Displays a list of shipments for an order.

Parameters

\Drupal\uc_order\OrderInterface $uc_order: The order object.

Return value

array|\Symfony\Component\HttpFoundation\RedirectResponse A render array, or redirect response if there are no shipments.

1 string reference to 'ShipmentController::listOrderShipments'
uc_fulfillment.routing.yml in shipping/uc_fulfillment/uc_fulfillment.routing.yml
shipping/uc_fulfillment/uc_fulfillment.routing.yml

File

shipping/uc_fulfillment/src/Controller/ShipmentController.php, line 124

Class

ShipmentController
Controller routines for shipments.

Namespace

Drupal\uc_fulfillment\Controller

Code

public function listOrderShipments(OrderInterface $uc_order) {
  $header = [
    $this
      ->t('Shipment ID'),
    $this
      ->t('Name'),
    $this
      ->t('Company'),
    $this
      ->t('Destination'),
    $this
      ->t('Ship date'),
    $this
      ->t('Estimated delivery'),
    $this
      ->t('Tracking number'),
    $this
      ->t('Actions'),
  ];
  $shipments = Shipment::loadByOrder($uc_order
    ->id());
  $rows = [];
  foreach ($shipments as $shipment) {
    $row = [];

    // Shipment ID.
    $row[] = [
      'data' => [
        '#plain_text' => $shipment
          ->id(),
      ],
    ];

    // Destination address.
    $destination = $shipment
      ->getDestination();

    // Name.
    $row[] = [
      'data' => [
        '#plain_text' => $destination
          ->getFirstName() . ' ' . $destination
          ->getLastName(),
      ],
    ];

    // Company.
    $row[] = [
      'data' => [
        '#plain_text' => $destination
          ->getCompany(),
      ],
    ];

    // Destination.
    $row[] = [
      'data' => [
        '#plain_text' => $destination
          ->getCity() . ', ' . $destination
          ->getZone() . ' ' . $destination
          ->getPostalCode(),
      ],
    ];

    // Ship date.
    $row[] = \Drupal::service('date.formatter')
      ->format($shipment
      ->getShipDate(), 'uc_store');

    // Estimated delivery.
    $row[] = \Drupal::service('date.formatter')
      ->format($shipment
      ->getExpectedDelivery(), 'uc_store');

    // Tracking number.
    $row[] = empty($shipment
      ->getTrackingNumber()) ? $this
      ->t('n/a') : [
      'data' => [
        '#plain_text' => $shipment
          ->getTrackingNumber(),
      ],
    ];

    // Actions.
    $ops[] = [
      '#type' => 'operations',
      '#links' => [
        'view' => [
          'title' => $this
            ->t('View'),
          'url' => Url::fromRoute('uc_fulfillment.view_shipment', [
            'uc_order' => $uc_order
              ->id(),
            'uc_shipment' => $shipment
              ->id(),
          ]),
        ],
        'edit' => [
          'title' => $this
            ->t('Edit'),
          'url' => Url::fromRoute('uc_fulfillment.edit_shipment', [
            'uc_order' => $uc_order
              ->id(),
            'uc_shipment' => $shipment
              ->id(),
          ]),
        ],
        'print' => [
          'title' => $this
            ->t('Print'),
          'url' => Url::fromRoute('uc_fulfillment.print_shipment', [
            'uc_order' => $uc_order
              ->id(),
            'uc_shipment' => $shipment
              ->id(),
          ]),
        ],
        'packing_slip' => [
          'title' => $this
            ->t('Packing slip'),
          'url' => Url::fromRoute('uc_fulfillment.packing_slip', [
            'uc_order' => $uc_order
              ->id(),
            'uc_shipment' => $shipment
              ->id(),
          ]),
        ],
        'delete' => [
          'title' => $this
            ->t('Delete'),
          'url' => Url::fromRoute('uc_fulfillment.delete_shipment', [
            'uc_order' => $uc_order
              ->id(),
            'uc_shipment' => $shipment
              ->id(),
          ]),
        ],
      ],
    ];
    $row[] = [
      'data' => $ops,
    ];
    $rows[] = $row;
  }
  if (empty($rows)) {
    if (count(Package::loadByOrder($uc_order
      ->id())) == 0) {
      $this
        ->messenger()
        ->addWarning($this
        ->t("This order's products have not been organized into packages."));
      return $this
        ->redirect('uc_fulfillment.new_package', [
        'uc_order' => $uc_order
          ->id(),
      ]);
    }
    else {
      $this
        ->messenger()
        ->addWarning($this
        ->t('No shipments have been made for this order.'));
      return $this
        ->redirect('uc_fulfillment.new_shipment', [
        'uc_order' => $uc_order
          ->id(),
      ]);
    }
  }
  $build['shipments'] = [
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
  ];
  return $build;
}