You are here

class ShipmentController in Ubercart 8.4

Controller routines for shipments.

Hierarchy

Expanded class hierarchy of ShipmentController

File

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

Namespace

Drupal\uc_fulfillment\Controller
View source
class ShipmentController extends ControllerBase {

  /**
   * The page title callback for shipment views.
   *
   * @param \Drupal\uc_order\OrderInterface $uc_order
   *   The shipment's order.
   * @param \Drupal\uc_fulfillment\ShipmentInterface $uc_shipment
   *   The ID of shipment.
   *
   * @return string
   *   The page title.
   */
  public function pageTitle(OrderInterface $uc_order, ShipmentInterface $uc_shipment) {
    return $this
      ->t('Shipment @id', [
      '@id' => $uc_shipment
        ->id(),
    ]);
  }

  /**
   * Default method to create a shipment from packages.
   *
   * @param \Drupal\uc_order\OrderInterface $uc_order
   *   The order object.
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The request.
   *
   * @return array|\Symfony\Component\HttpFoundation\RedirectResponse
   *   A render array, or a redirect response if there are selected packages.
   */
  public function makeShipment(OrderInterface $uc_order, Request $request) {
    $method_id = $request->query
      ->get('method_id');
    $request->query
      ->remove('method_id');
    $package_ids = $request->query
      ->all();
    if (count($package_ids) > 0) {

      //      $breadcrumb = drupal_get_breadcrumb();
      //      $breadcrumb[] = Link::createFromRoute($this->t('Shipments'), 'uc_fulfillment.shipments', ['uc_order' => $uc_order->id()]);
      //      drupal_set_breadcrumb($breadcrumb);
      // Find FulfillmentMethod plugins.
      $manager = \Drupal::service('plugin.manager.uc_fulfillment.method');
      $methods = FulfillmentMethod::loadMultiple();
      if (isset($methods[$method_id])) {
        $method = $methods[$method_id];
      }
      else {

        // The selected fulfullment isn't available, so use built-in "Manual" shipping.
        $method = $methods['manual'];
      }
      $plugin = $manager
        ->createInstance($method
        ->getPluginId(), $method
        ->getPluginConfiguration());
      return $plugin
        ->fulfillOrder($uc_order, $package_ids);
    }
    else {
      $this
        ->messenger()
        ->addWarning($this
        ->t('There is no sense in making a shipment with no packages on it, right?'));
      return $this
        ->redirect('uc_fulfillment.new_shipment', [
        'uc_order' => $uc_order
          ->id(),
      ]);
    }
  }

  /**
   * Shows a printer-friendly version of a shipment.
   *
   * @param \Drupal\uc_order\OrderInterface $uc_order
   *   The order object.
   * @param \Drupal\uc_fulfillment\ShipmentInterface $uc_shipment
   *   The ID of shipment.
   * @param bool $print
   *   Whether to generate a printable version.
   * @param bool $labels
   *   Whether to include mailing labels.
   *
   * @return array|string
   *   A render array or HTML markup in a form suitable for printing.
   */
  public function printShipment(OrderInterface $uc_order, ShipmentInterface $uc_shipment, $print = FALSE, $labels = TRUE) {
    $packing_slip = [
      '#theme' => 'uc_packing_slip',
      '#order' => $uc_order,
      '#shipment' => $uc_shipment,
      '#labels' => $labels,
      '#op' => $print ? 'print' : 'view',
    ];
    if ($print) {
      $build = [
        '#theme' => 'uc_packing_slip_page',
        '#content' => $packing_slip,
      ];
      $markup = \Drupal::service('renderer')
        ->renderPlain($build);
      $response = new Response($markup);
      $response->headers
        ->set('Content-Type', 'text/html; charset=utf-8');
      return $response;
    }
    return $packing_slip;
  }

  /**
   * Displays a list of shipments for an order.
   *
   * @param \Drupal\uc_order\OrderInterface $uc_order
   *   The order object.
   *
   * @return array|\Symfony\Component\HttpFoundation\RedirectResponse
   *   A render array, or redirect response if there are no shipments.
   */
  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;
  }

  /**
   * Displays shipment details.
   *
   * @param \Drupal\uc_order\OrderInterface $uc_order
   *   The order object.
   * @param \Drupal\uc_fulfillment\ShipmentInterface $uc_shipment
   *   The shipment.
   *
   * @return array
   *   A render array.
   */
  public function viewShipment(OrderInterface $uc_order, ShipmentInterface $uc_shipment) {

    // Origin address.
    $build['pickup_address'] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'order-pane',
          'pos-left',
        ],
      ],
    ];
    $build['pickup_address']['title'] = [
      '#type' => 'container',
      '#markup' => $this
        ->t('Pickup Address:'),
      '#attributes' => [
        'class' => [
          'order-pane-title',
        ],
      ],
    ];
    $build['pickup_address']['address'] = [
      '#type' => 'container',
      '#markup' => $uc_shipment
        ->getOrigin(),
    ];

    // Destination address.
    $build['delivery_address'] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'order-pane',
          'pos-left',
        ],
      ],
    ];
    $build['delivery_address']['title'] = [
      '#type' => 'container',
      '#markup' => $this
        ->t('Delivery Address:'),
      '#attributes' => [
        'class' => [
          'order-pane-title',
        ],
      ],
    ];
    $build['delivery_address']['address'] = [
      '#type' => 'container',
      '#markup' => $uc_shipment
        ->getDestination(),
    ];

    // Fulfillment schedule.
    $rows = [];
    $rows[] = [
      $this
        ->t('Ship date:'),
      \Drupal::service('date.formatter')
        ->format($uc_shipment
        ->getShipDate(), 'uc_store'),
    ];
    $rows[] = [
      $this
        ->t('Expected delivery:'),
      \Drupal::service('date.formatter')
        ->format($uc_shipment
        ->getExpectedDelivery(), 'uc_store'),
    ];
    $build['schedule'] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'order-pane',
          'abs-left',
        ],
      ],
    ];
    $build['schedule']['title'] = [
      '#type' => 'container',
      '#markup' => $this
        ->t('Schedule:'),
      '#attributes' => [
        'class' => [
          'order-pane-title',
        ],
      ],
    ];
    $build['schedule']['table'] = [
      '#theme' => 'table',
      '#rows' => $rows,
      '#attributes' => [
        'style' => 'width: auto',
      ],
    ];

    // Shipment details.
    $rows = [];
    $rows[] = [
      $this
        ->t('Carrier:'),
      [
        'data' => [
          '#plain_text' => $uc_shipment
            ->getCarrier(),
        ],
      ],
    ];
    if ($uc_shipment
      ->getTransactionId()) {
      $rows[] = [
        $this
          ->t('Transaction ID:'),
        [
          'data' => [
            '#plain_text' => $uc_shipment
              ->getTransactionId(),
          ],
        ],
      ];
    }
    if ($uc_shipment
      ->getTrackingNumber()) {
      $rows[] = [
        $this
          ->t('Tracking number:'),
        [
          'data' => [
            '#plain_text' => $uc_shipment
              ->getTrackingNumber(),
          ],
        ],
      ];
    }
    $methods = \Drupal::moduleHandler()
      ->invokeAll('uc_fulfillment_method');
    if (isset($methods[$uc_shipment
      ->getShippingMethod()]['quote']['accessorials'][$uc_shipment
      ->getAccessorials()])) {
      $rows[] = [
        $this
          ->t('Services:'),
        $methods[$uc_shipment
          ->getShippingMethod()]['quote']['accessorials'][$uc_shipment
          ->getAccessorials()],
      ];
    }
    else {
      $rows[] = [
        $this
          ->t('Services:'),
        $uc_shipment
          ->getAccessorials(),
      ];
    }
    $rows[] = [
      $this
        ->t('Cost:'),
      [
        'data' => [
          '#theme' => 'uc_price',
          '#price' => $uc_shipment
            ->getCost(),
        ],
      ],
    ];
    $build['details'] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'order-pane',
          'abs-left',
        ],
      ],
    ];
    $build['details']['title'] = [
      '#type' => 'container',
      '#markup' => $this
        ->t('Shipment Details:'),
      '#attributes' => [
        'class' => [
          'order-pane-title',
        ],
      ],
    ];
    $build['details']['table'] = [
      '#theme' => 'table',
      '#rows' => $rows,
      '#attributes' => [
        'style' => 'width:auto',
      ],
    ];

    // Packages.
    foreach ($uc_shipment
      ->getPackages() as $package) {
      $build['packages'][] = $this
        ->viewPackage($package);
    }
    return $build;
  }

  /**
   * Returns an address from an object.
   *
   * @param \Drupal\uc_fulfillment\ShipmentInterface $uc_shipment
   *   A Shipment object.
   * @param string $type
   *   The key prefix to use to extract the address.
   *
   * @return \Drupal\uc_store\AddressInterface
   *   An address object.
   */
  protected function getAddress(ShipmentInterface $uc_shipment, $type) {
    $name = $shipment->{$type . '_first_name'} . ' ' . $shipment->{$type . '_last_name'};
    $address = Address::create();
    $address
      ->setFirstName($shipment->{$type . '_first_name'})
      ->setLastName($shipment->{$type . '_last_name'})
      ->setCompany($shipment->{$type . '_company'})
      ->setStreet1($shipment->{$type . '_street1'})
      ->setStreet2($shipment->{$type . '_street2'})
      ->setCity($shipment->{$type . '_city'})
      ->setZone($shipment->{$type . '_zone'})
      ->setPostalCode($shipment->{$type . '_postal_code'})
      ->setCountry($shipment->{$type . '_country'});
    return $address;
  }

  /**
   * Displays the details of a package.
   *
   * @param \Drupal\uc_fulfillment\PackageInterface $package
   *   The package object.
   *
   * @return array
   *   A render array.
   */
  public function viewPackage(PackageInterface $package) {
    $shipment = Shipment::load($package
      ->getSid());
    $build = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'order-pane',
          'pos-left',
        ],
      ],
    ];
    $build['title'] = [
      '#type' => 'container',
      '#markup' => $this
        ->t('Package %id:', [
        '%id' => $package
          ->id(),
      ]),
      '#attributes' => [
        'class' => [
          'order-pane-title',
        ],
      ],
    ];
    $rows = [];
    $rows[] = [
      $this
        ->t('Contents:'),
      [
        'data' => [
          '#markup' => $package
            ->getDescription(),
        ],
      ],
    ];
    if ($shipment) {
      $methods = \Drupal::moduleHandler()
        ->invokeAll('uc_fulfillment_method');
      if (isset($methods[$shipment
        ->getShippingMethod()])) {
        $pkg_type = $methods[$shipment
          ->getShippingMethod()]['ship']['pkg_types'][$package
          ->getPackageType()];
      }
    }
    $rows[] = [
      $this
        ->t('Package type:'),
      isset($pkg_type) ? $pkg_type : [
        'data' => [
          '#plain_text' => $package
            ->getPackageType(),
        ],
      ],
    ];
    if ($package
      ->getLength() && $package
      ->getWidth() && $package
      ->getHeight()) {
      $units = $package
        ->getLengthUnits();
      $rows[] = [
        $this
          ->t('Dimensions:'),
        $this
          ->t('@l x @w x @h', [
          '@l' => uc_length_format($package
            ->getLength(), $units),
          '@w' => uc_length_format($package
            ->getWidth(), $units),
          '@h' => uc_length_format($package
            ->getHeight(), $units),
        ]),
      ];
    }
    if ($package
      ->getWeight()) {
      $units = $package
        ->getWeightUnits();
      $rows[] = [
        $this
          ->t('Weight:'),
        uc_weight_format($package
          ->getWeight(), $units),
      ];
    }
    $rows[] = [
      $this
        ->t('Insured value:'),
      [
        'data' => [
          '#theme' => 'uc_price',
          '#price' => $package
            ->getValue(),
        ],
      ],
    ];
    if ($package
      ->getTrackingNumber()) {
      $rows[] = [
        $this
          ->t('Tracking number:'),
        [
          'data' => [
            '#plain_text' => $package
              ->getTrackingNumber(),
          ],
        ],
      ];
    }
    if ($shipment && $package
      ->getLabelImage() && file_exists($package
      ->getLabelImage()->uri)) {
      $rows[] = [
        $this
          ->t('Label:'),
        [
          'data' => [
            '#type' => 'link',
            '#title' => $this
              ->t('Click to view.'),
            '#url' => Url::fromUri('admin/store/orders/' . $package
              ->getOrderId() . '/shipments/labels/' . $shipment
              ->getShippingMethod() . '/' . $package
              ->getLabelImage()->uri),
          ],
        ],
      ];
    }
    else {
      $rows[] = [
        $this
          ->t('Label:'),
        $this
          ->t('n/a'),
      ];
    }
    $build['package'] = [
      '#theme' => 'table',
      '#rows' => $rows,
      'attributes' => [
        'style' => 'width:auto;',
      ],
    ];
    return $build;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityManager protected property The entity manager.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 40
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityManager Deprecated protected function Retrieves the entity manager service.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
ControllerBase::state protected function Returns the state storage service.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
ShipmentController::getAddress protected function Returns an address from an object.
ShipmentController::listOrderShipments public function Displays a list of shipments for an order.
ShipmentController::makeShipment public function Default method to create a shipment from packages.
ShipmentController::pageTitle public function The page title callback for shipment views.
ShipmentController::printShipment public function Shows a printer-friendly version of a shipment.
ShipmentController::viewPackage public function Displays the details of a package.
ShipmentController::viewShipment public function Displays shipment details.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.