You are here

class Shipment in Ubercart 8.4

Defines the Shipment class.

Hierarchy

Expanded class hierarchy of Shipment

8 files declare their use of Shipment
Manual.php in shipping/uc_fulfillment/src/Plugin/Ubercart/FulfillmentMethod/Manual.php
PackageCancelForm.php in shipping/uc_fulfillment/src/Form/PackageCancelForm.php
PackageController.php in shipping/uc_fulfillment/src/Controller/PackageController.php
ShipmentController.php in shipping/uc_fulfillment/src/Controller/ShipmentController.php
ShipmentConverter.php in shipping/uc_fulfillment/src/ParamConverter/ShipmentConverter.php

... See full list

3 string references to 'Shipment'
ShipmentSaveEventTest::testShipmentSaveEvent in shipping/uc_fulfillment/tests/src/Unit/Integration/Event/ShipmentSaveEventTest.php
Tests the event metadata.
uc_fulfillment.rules.events.yml in shipping/uc_fulfillment/uc_fulfillment.rules.events.yml
shipping/uc_fulfillment/uc_fulfillment.rules.events.yml
uc_fulfillment_views_data in shipping/uc_fulfillment/uc_fulfillment.views.inc
Implements hook_views_data().

File

shipping/uc_fulfillment/src/Shipment.php, line 15

Namespace

Drupal\uc_fulfillment
View source
class Shipment implements ShipmentInterface {
  use MessengerTrait;
  use StringTranslationTrait;

  /**
   * Shipment ID.
   *
   * @var int
   */
  protected $sid;

  /**
   * Order ID of this shipment.
   *
   * @var int
   */
  protected $orderId;

  /**
   * Name of the shipping method.
   *
   * @var string
   */
  protected $shipping_method = '';

  /**
   * Shipping quote accessorials.
   *
   * @var array
   */
  protected $accessorials = '';

  /**
   * Name of the common carrier.
   *
   * @var string
   */
  protected $carrier = '';

  /**
   * Shipment transaction ID.
   *
   * @var string
   */
  protected $transaction_id = '';

  /**
   * Shipment tracking number.
   *
   * @var string
   */
  protected $tracking_number = '';

  /**
   * Ship date timestamp.
   *
   * @var int
   */
  protected $ship_date = 0;

  /**
   * Expected delivery timestamp.
   *
   * @var int
   */
  protected $expected_delivery = 0;

  /**
   * Name of the status.
   *
   * @var float
   */
  protected $cost = 0;

  /**
   * Currency code.
   *
   * @var string
   */
  protected $currency = '';

  /**
   * Last modified timestamp.
   *
   * @var int
   */
  protected $changed = 0;

  /**
   * Packages contained in this shipment.
   *
   * @var \Drupal\uc_fulfillment\Package[]
   */
  protected $packages = [];

  /**
   * Shipment origin address.
   *
   * @var \Drupal\uc_store\Address
   */
  protected $origin;

  /**
   * Shipment destination address.
   *
   * @var \Drupal\uc_store\Address
   */
  protected $destination;

  /**
   * {@inheritdoc}
   */
  public function id() {
    return $this->sid;
  }

  /**
   * {@inheritdoc}
   */
  public function setOrderId($orderId) {
    $this->orderId = $orderId;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getOrderId() {
    return $this->orderId;
  }

  /**
   * {@inheritdoc}
   */
  public function setShippingMethod($shipping_method) {
    $this->shipping_method = $shipping_method;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getShippingMethod() {
    return $this->shipping_method;
  }

  /**
   * {@inheritdoc}
   */
  public function setAccessorials($accessorials) {
    $this->accessorials = $accessorials;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getAccessorials() {
    return $this->accessorials;
  }

  /**
   * {@inheritdoc}
   */
  public function setCarrier($carrier) {
    $this->carrier = $carrier;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getCarrier() {
    return $this->carrier;
  }

  /**
   * {@inheritdoc}
   */
  public function setTransactionId($transaction_id) {
    $this->transaction_id = $transaction_id;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getTransactionId() {
    return $this->transaction_id;
  }

  /**
   * {@inheritdoc}
   */
  public function setTrackingNumber($tracking_number) {
    $this->tracking_number = $tracking_number;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getTrackingNumber() {
    return $this->tracking_number;
  }

  /**
   * {@inheritdoc}
   */
  public function setShipDate($ship_date) {
    $this->ship_date = $ship_date;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getShipDate() {
    return $this->ship_date;
  }

  /**
   * {@inheritdoc}
   */
  public function setExpectedDelivery($expected_delivery) {
    $this->expected_delivery = $expected_delivery;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getExpectedDelivery() {
    return $this->expected_delivery;
  }

  /**
   * {@inheritdoc}
   */
  public function setCost($cost) {
    $this->cost = $cost;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getCost() {
    return $this->cost;
  }

  /**
   * {@inheritdoc}
   */
  public function setCurrency($currency) {
    $this->currency = $currency;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getCurrency() {
    return $this->currency;
  }

  /**
   * {@inheritdoc}
   */
  public function setChanged($changed) {
    $this->changed = $changed;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getChanged() {
    return $this->changed;
  }

  /**
   * {@inheritdoc}
   */
  public function setPackages(array $packages) {
    $this->packages = $packages;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getPackages() {
    return $this->packages;
  }

  /**
   * {@inheritdoc}
   */
  public function setOrigin(AddressInterface $origin) {
    $this->origin = $origin;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getOrigin() {
    return $this->origin;
  }

  /**
   * {@inheritdoc}
   */
  public function setDestination(AddressInterface $destination) {
    $this->destination = $destination;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getDestination() {
    return $this->destination;
  }

  /**
   * Constructor.
   */
  protected function __construct() {
  }

  /**
   * Creates a Shipment.
   *
   * @param array $values
   *   (optional) Array of initialization values.
   *
   * @return \Drupal\uc_fulfillment\Shipment
   *   A Shipment object.
   */
  public static function create(array $values = NULL) {
    $shipment = new Shipment();
    if (isset($values)) {
      foreach ($values as $key => $value) {
        $shipment->{$key} = $value;
      }
    }
    return $shipment;
  }

  /**
   * Loads a shipment and its packages for a given order.
   *
   * @param int $orderId
   *   An order ID.
   *
   * @return \Drupal\uc_fulfillment\Shipment[]
   *   Array of shipment objects for the given order.
   */
  public static function loadByOrder($orderId) {
    $shipments = [];
    $result = \Drupal::database()
      ->query('SELECT sid FROM {uc_shipments} WHERE order_id = :id', [
      ':id' => $orderId,
    ]);
    while ($shipment_id = $result
      ->fetchField()) {
      $shipments[] = Shipment::load($shipment_id);
    }
    return $shipments;
  }

  /**
   * Loads a shipment and its packages.
   *
   * @param int $shipment_id
   *   The shipment ID.
   *
   * @return \Drupal\uc_fulfillment\Shipment|null
   *   The Shipment object, or NULL if there isn't one.
   */
  public static function load($shipment_id) {
    $shipment = NULL;
    $result = \Drupal::database()
      ->query('SELECT * FROM {uc_shipments} WHERE sid = :sid', [
      ':sid' => $shipment_id,
    ]);
    if ($assoc = $result
      ->fetchAssoc()) {
      $shipment = Shipment::create();
      $origin_fields = [];
      $destination_fields = [];
      foreach ($assoc as $key => $value) {
        $subkey = substr($key, 0, 2);
        if ($subkey == 'o_') {
          $origin_fields[substr($key, 2)] = $value;
        }
        elseif ($subkey == 'd_') {
          $destination_fields[substr($key, 2)] = $value;
        }
        else {
          $shipment->{$key} = $value;
        }
      }

      // Reconstitute Address objects from individual fields.
      $shipment
        ->setOrigin(Address::create($origin_fields));
      $shipment
        ->setDestination(Address::create($destination_fields));
      $result2 = \Drupal::database()
        ->query('SELECT package_id FROM {uc_packages} WHERE sid = :sid', [
        ':sid' => $shipment_id,
      ]);
      $packages = [];
      foreach ($result2 as $package) {
        $packages[$package->package_id] = Package::load($package->package_id);
      }
      $shipment
        ->setPackages($packages);
      $extra = \Drupal::moduleHandler()
        ->invokeAll('uc_shipment', [
        'load',
        $shipment,
      ]);
      if (is_array($extra)) {
        foreach ($extra as $key => $value) {
          $shipment->{$key} = $value;
        }
      }
    }
    return $shipment;
  }

  /**
   * Saves this shipment.
   */
  public function save() {
    $this->changed = \Drupal::time()
      ->getCurrentTime();

    // Break Address objects into individual fields for saving.
    $fields = [];
    if (isset($this->origin)) {
      foreach ($this->origin as $field => $value) {
        $field = 'o_' . $field;
        $fields[$field] = $value;
      }
    }
    if (isset($this->destination)) {
      foreach ($this->destination as $field => $value) {
        $field = 'd_' . $field;
        $fields[$field] = $value;
      }
    }

    // Yuck.
    $fields += [
      'order_id' => $this->orderId,
      'shipping_method' => $this->shipping_method,
      'accessorials' => $this->accessorials,
      'carrier' => $this->carrier,
      'transaction_id' => $this->transaction_id,
      'tracking_number' => $this->tracking_number,
      'ship_date' => $this->ship_date,
      'expected_delivery' => $this->expected_delivery,
      'cost' => $this->cost,
      'currency' => $this->currency,
      'changed' => $this->changed,
    ];
    if (!isset($this->sid)) {
      $this->sid = \Drupal::database()
        ->insert('uc_shipments')
        ->fields($fields)
        ->execute();
      $this->is_new = TRUE;
    }
    else {
      \Drupal::database()
        ->update('uc_shipments')
        ->fields($fields)
        ->condition('sid', $this->sid, '=')
        ->execute();
      $this->is_new = FALSE;
    }
    if (is_array($this->packages)) {
      foreach ($this->packages as $package) {
        $package
          ->setSid($this->sid);

        // Since the products haven't changed, we take them out of the object so
        // that they are not deleted and re-inserted.
        $products = $package
          ->getProducts();
        $package
          ->setProducts([]);
        $package
          ->save();

        // But they're still necessary for hook_uc_shipment(), so they're added
        // back in.
        $package
          ->setProducts($products);
      }
    }
    \Drupal::moduleHandler()
      ->invokeAll('uc_shipment', [
      'save',
      $this,
    ]);
    $order = Order::load($this->orderId);

    /* rules_invoke_event('uc_shipment_save', $order, $this); */
    $event = new ShipmentSaveEvent($order, $this);
    \Drupal::service('event_dispatcher')
      ->dispatch($event::EVENT_NAME, $event);
  }

  /**
   * Deletes this shipment.
   */
  public function delete() {
    \Drupal::database()
      ->update('uc_packages')
      ->fields([
      'sid' => NULL,
      'tracking_number' => NULL,
      'label_image' => NULL,
    ])
      ->condition('sid', $this->sid)
      ->execute();
    \Drupal::database()
      ->delete('uc_shipments')
      ->condition('sid', $this->sid)
      ->execute();
    foreach ($this->packages as $package) {
      if ($package
        ->getLabelImage()) {
        file_delete($package
          ->getLabelImage());
        $package
          ->setLabelImage('');
      }
    }
    \Drupal::moduleHandler()
      ->invokeAll('uc_shipment', [
      'delete',
      $this,
    ]);
    $this
      ->messenger()
      ->addMessage($this
      ->t('Shipment @id has been deleted.', [
      '@id' => $this->sid,
    ]));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
Shipment::$accessorials protected property Shipping quote accessorials.
Shipment::$carrier protected property Name of the common carrier.
Shipment::$changed protected property Last modified timestamp.
Shipment::$cost protected property Name of the status.
Shipment::$currency protected property Currency code.
Shipment::$destination protected property Shipment destination address.
Shipment::$expected_delivery protected property Expected delivery timestamp.
Shipment::$orderId protected property Order ID of this shipment.
Shipment::$origin protected property Shipment origin address.
Shipment::$packages protected property Packages contained in this shipment.
Shipment::$shipping_method protected property Name of the shipping method.
Shipment::$ship_date protected property Ship date timestamp.
Shipment::$sid protected property Shipment ID.
Shipment::$tracking_number protected property Shipment tracking number.
Shipment::$transaction_id protected property Shipment transaction ID.
Shipment::create public static function Creates a Shipment.
Shipment::delete public function Deletes this shipment.
Shipment::getAccessorials public function Returns the shipping quote accessorials of this shipment. Overrides ShipmentInterface::getAccessorials
Shipment::getCarrier public function Returns the name of the common carrier. Overrides ShipmentInterface::getCarrier
Shipment::getChanged public function Returns the last modified timestamp. Overrides ShipmentInterface::getChanged
Shipment::getCost public function Returns the shipping cost for this shipment. Overrides ShipmentInterface::getCost
Shipment::getCurrency public function Returns the currency code used for the shipping cost. Overrides ShipmentInterface::getCurrency
Shipment::getDestination public function Returns the destination address for this shipment. Overrides ShipmentInterface::getDestination
Shipment::getExpectedDelivery public function Returns the expected delivery timestamp. Overrides ShipmentInterface::getExpectedDelivery
Shipment::getOrderId public function Returns the order ID of this shipment. Overrides ShipmentInterface::getOrderId
Shipment::getOrigin public function Returns the origin address for this shipment. Overrides ShipmentInterface::getOrigin
Shipment::getPackages public function Returns the packages in this shipment. Overrides ShipmentInterface::getPackages
Shipment::getShipDate public function Returns the ship date timestamp of this shipment. Overrides ShipmentInterface::getShipDate
Shipment::getShippingMethod public function Returns the name of the shipping method. Overrides ShipmentInterface::getShippingMethod
Shipment::getTrackingNumber public function Returns the tracking number of this shipment. Overrides ShipmentInterface::getTrackingNumber
Shipment::getTransactionId public function Returns the transaction ID of this shipment. Overrides ShipmentInterface::getTransactionId
Shipment::id public function
Shipment::load public static function Loads a shipment and its packages.
Shipment::loadByOrder public static function Loads a shipment and its packages for a given order.
Shipment::save public function Saves this shipment.
Shipment::setAccessorials public function Sets the shipping quote accessorials of this shipment. Overrides ShipmentInterface::setAccessorials
Shipment::setCarrier public function Sets the common carrier name to the given value. Overrides ShipmentInterface::setCarrier
Shipment::setChanged public function Sets the last modified timestamp. Overrides ShipmentInterface::setChanged
Shipment::setCost public function Sets the shipping cost for this shipment. Overrides ShipmentInterface::setCost
Shipment::setCurrency public function Sets the currency code used for the shipping cost. Overrides ShipmentInterface::setCurrency
Shipment::setDestination public function Sets the destination address for this shipment. Overrides ShipmentInterface::setDestination
Shipment::setExpectedDelivery public function Sets the expected delivery timestamp. Overrides ShipmentInterface::setExpectedDelivery
Shipment::setOrderId public function Sets the order ID to the given value. Overrides ShipmentInterface::setOrderId
Shipment::setOrigin public function Sets the origin address for this shipment. Overrides ShipmentInterface::setOrigin
Shipment::setPackages public function Sets the packages in this shipment. Overrides ShipmentInterface::setPackages
Shipment::setShipDate public function Sets the ship date timestamp to the given value. Overrides ShipmentInterface::setShipDate
Shipment::setShippingMethod public function Sets the shipping method to the given value. Overrides ShipmentInterface::setShippingMethod
Shipment::setTrackingNumber public function Sets the tracking number of this shipment. Overrides ShipmentInterface::setTrackingNumber
Shipment::setTransactionId public function Sets the transaction ID of this shipment. Overrides ShipmentInterface::setTransactionId
Shipment::__construct protected function Constructor.
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.