You are here

class Package in Ubercart 8.4

Defines the Package class.

Hierarchy

Expanded class hierarchy of Package

8 files declare their use of Package
Manual.php in shipping/uc_fulfillment/src/Plugin/Ubercart/FulfillmentMethod/Manual.php
NewPackageForm.php in shipping/uc_fulfillment/src/Form/NewPackageForm.php
NewShipmentForm.php in shipping/uc_fulfillment/src/Form/NewShipmentForm.php
PackageController.php in shipping/uc_fulfillment/src/Controller/PackageController.php
PackageConverter.php in shipping/uc_fulfillment/src/ParamConverter/PackageConverter.php

... See full list

6 string references to 'Package'
hook_uc_order_actions in uc_order/uc_order.api.php
Adds links to local tasks for orders on the admin's list of orders.
NewPackageForm::buildForm in shipping/uc_fulfillment/src/Form/NewPackageForm.php
Form constructor.
NewShipmentForm::buildForm in shipping/uc_fulfillment/src/Form/NewShipmentForm.php
Form constructor.
PackageTest::testPackagesUi in shipping/uc_fulfillment/tests/src/Functional/PackageTest.php
Tests the User Interface for packaging products.
uc_fulfillment_uc_order_actions in shipping/uc_fulfillment/uc_fulfillment.module
Implements hook_uc_order_actions().

... See full list

File

shipping/uc_fulfillment/src/Package.php, line 12

Namespace

Drupal\uc_fulfillment
View source
class Package implements PackageInterface {
  use MessengerTrait;
  use StringTranslationTrait;

  /* These variables map to DB columns. */

  /**
   * Package ID.
   *
   * @var int
   */
  protected $package_id;

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

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

  /**
   * Package shipping type.
   *
   * @var string
   */
  protected $shipping_type = '';

  /**
   * Package package type.
   *
   * @var string
   */
  protected $pkg_type = '';

  /**
   * Package length.
   *
   * @var float
   */
  protected $length = 1;

  /**
   * Package width.
   *
   * @var float
   */
  protected $width = 1;

  /**
   * Package height.
   *
   * @var float
   */
  protected $height = 1;

  /**
   * Package length units.
   *
   * @var string
   */
  protected $length_units = '';

  /**
   * Package weight.
   *
   * @var float
   */
  protected $weight = 0;

  /**
   * Package weight units.
   *
   * @var string
   */
  protected $weight_units = '';

  /**
   * Package monetary value.
   *
   * @var float
   */
  protected $value = 0;

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

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

  /**
   * Package shipping label image.
   *
   * @var string
   */
  protected $label_image;

  /* These variables don't map to DB columns. */

  /**
   * Products contained in this shipment.
   *
   * @var \Drupal\uc_order\OrderProductInterface[]
   */
  protected $products = [];

  /**
   * Array of ship-from addresses for products in this package.
   *
   * @var \Drupal\uc_store\Address[]
   */
  protected $addresses = [];

  /**
   * Package description.
   *
   * @var string
   */
  protected $description = '';

  /**
   * Cache for loaded packages.
   *
   * @var array
   */
  protected static $packages = [];

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  /**
   * {@inheritdoc}
   */
  public function addProducts(array $products) {
    $this->products = array_replace($this->products, $products);
    return $this;
  }

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

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

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

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

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

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

  /**
   * Constructor.
   */
  protected function __construct() {
    $store_config = \Drupal::config('uc_store.settings');
    $this->weight_units = $store_config
      ->get('weight.units');
    $this->length_units = $store_config
      ->get('length.units');
    $this->currency = $store_config
      ->get('currency.code');
  }

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

  /**
   * Loads packages for a given order.
   *
   * @param int $order_id
   *   An order ID.
   *
   * @return \Drupal\uc_fulfillment\Package[]
   *   Array of Package objects for the given order.
   */
  public static function loadByOrder($order_id) {
    $packages = [];
    $result = \Drupal::database()
      ->query('SELECT package_id FROM {uc_packages} WHERE order_id = :id ORDER BY package_id', [
      ':id' => $order_id,
    ]);
    while ($package_id = $result
      ->fetchField()) {
      $packages[] = Package::load($package_id);
    }
    return $packages;
  }

  /**
   * Loads a package and its products.
   *
   * @param int $package_id
   *   The package ID.
   *
   * @return \Drupal\uc_fulfillment\Package|null
   *   The Package object, or NULL if there isn't one with the given ID.
   */
  public static function load($package_id) {
    if (!isset(self::$packages[$package_id])) {
      $result = \Drupal::database()
        ->query('SELECT * FROM {uc_packages} WHERE package_id = :id', [
        ':id' => $package_id,
      ]);
      if ($assoc = $result
        ->fetchAssoc()) {
        $package = Package::create($assoc);
        $products = [];
        $description = [];
        $addresses = [];
        $result = \Drupal::database()
          ->query('SELECT op.order_product_id, pp.qty, op.weight__value AS weight, op.weight__units as weight_units, op.nid, op.title, op.model, op.price, op.data FROM {uc_packaged_products} pp LEFT JOIN {uc_order_products} op ON op.order_product_id = pp.order_product_id WHERE pp.package_id = :id ORDER BY op.order_product_id', [
          ':id' => $package->package_id,
        ]);
        foreach ($result as $product) {
          $address = uc_quote_get_default_shipping_address($product->nid);
          if (!in_array($address, $addresses)) {
            $addresses[] = $address;
          }
          $description[] = $product->qty . ' x ' . $product->model;
          $product->data = unserialize($product->data);
          $products[$product->order_product_id] = $product;
        }
        $package->addresses = $addresses;
        $package->description = implode(', ', $description);
        $package->products = $products;
        if ($package->label_image && ($image = file_load($package->label_image))) {
          $package->label_image = $image;
        }
        self::$packages[$package_id] = $package;
        return $package;
      }
      else {
        return NULL;
      }
    }

    // Return package from cache.
    return self::$packages[$package_id];
  }

  /**
   * Saves this package.
   */
  public function save() {
    $status = '';
    $fields = [
      'order_id' => $this->order_id,
      'shipping_type' => $this->shipping_type,
      'pkg_type' => $this->pkg_type,
      'length' => $this->length,
      'width' => $this->width,
      'height' => $this->height,
      'length_units' => $this->length_units,
      'weight' => $this->weight,
      'weight_units' => $this->weight_units,
      'value' => $this->value,
      'currency' => $this->currency,
      'tracking_number' => $this->tracking_number,
    ];
    if ($this->sid) {
      $fields['sid'] = $this->sid;
    }
    if ($this->label_image) {
      $fields['label_image'] = $this->label_image->fid;
    }
    if (!$this->package_id) {

      // This is a new package, do an INSERT.
      $this->package_id = \Drupal::database()
        ->insert('uc_packages')
        ->fields($fields)
        ->execute();
      $status = SAVED_NEW;
    }
    else {

      // This is a package we're modifying, do an UPDATE.
      \Drupal::database()
        ->update('uc_packages')
        ->fields($fields)
        ->condition('package_id', $this->package_id)
        ->execute();
      $status = SAVED_UPDATED;
    }

    // Now take care of saving the product relations.
    if ($this->products) {
      $insert = \Drupal::database()
        ->insert('uc_packaged_products')
        ->fields([
        'package_id',
        'order_product_id',
        'qty',
      ]);
      foreach ($this->products as $id => $product) {
        $insert
          ->values([
          'package_id' => $this->package_id,
          'order_product_id' => $id,
          'qty' => $product->qty,
        ]);

        // Save the package_id to the OrderProduct.
        // 'package_id' is a key in the serialized 'data' array.
        // data->package_id is an array keyed by package ID with a
        // value equal to the quantity in that package.
        $order_product = OrderProduct::load($id);
        if (NULL != $order_product) {
          $package_array = (array) $order_product->data->package_id;
          $package_array[intval($this->package_id)] = (int) $product->qty;
          $order_product->data->package_id = $package_array;
          $order_product
            ->save();
        }
      }

      // Remove any old data for this package.
      \Drupal::database()
        ->delete('uc_packaged_products')
        ->condition('package_id', $this->package_id)
        ->execute();

      // Write the current data for this package.
      $insert
        ->execute();
    }
    return $status;
  }

  /**
   * Deletes this package.
   */
  public function delete() {
    \Drupal::database()
      ->delete('uc_packages')
      ->condition('package_id', $this->package_id)
      ->execute();
    \Drupal::database()
      ->delete('uc_packaged_products')
      ->condition('package_id', $this->package_id)
      ->execute();
    if ($this->label_image) {
      file_usage_delete($this->label_image, 'uc_fulfillment', 'package', $this->package_id);
      file_delete($this->label_image);
    }

    // Remove the package_id from the OrderProduct.
    foreach ($this->products as $id => $product) {
      $order_product = OrderProduct::load($id);
      if (NULL != $order_product) {

        // There may be multiple packages per OrderProduct.
        $package_array = $order_product->data->package_id;
        unset($package_array[$this->package_id]);
        $order_product->data->package_id = $package_array;
        $order_product
          ->save();
      }
    }
    $this
      ->messenger()
      ->addMessage($this
      ->t('Package @id has been deleted.', [
      '@id' => $this->package_id,
    ]));
  }

}

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.
Package::$addresses protected property Array of ship-from addresses for products in this package.
Package::$currency protected property Currency code.
Package::$description protected property Package description.
Package::$height protected property Package height.
Package::$label_image protected property Package shipping label image.
Package::$length protected property Package length.
Package::$length_units protected property Package length units.
Package::$order_id protected property Order ID of this shipment.
Package::$packages protected static property Cache for loaded packages.
Package::$package_id protected property Package ID.
Package::$pkg_type protected property Package package type.
Package::$products protected property Products contained in this shipment.
Package::$shipping_type protected property Package shipping type.
Package::$sid protected property Shipment ID.
Package::$tracking_number protected property Package tracking number.
Package::$value protected property Package monetary value.
Package::$weight protected property Package weight.
Package::$weight_units protected property Package weight units.
Package::$width protected property Package width.
Package::addProducts public function Adds products to this package. Overrides PackageInterface::addProducts
Package::create public static function Creates a Package.
Package::delete public function Deletes this package.
Package::getAddresses public function Returns the list of ship-from addresses for products in this package. Overrides PackageInterface::getAddresses
Package::getCurrency public function Returns the currency code used for the package value. Overrides PackageInterface::getCurrency
Package::getDescription public function Returns the package description. Overrides PackageInterface::getDescription
Package::getHeight public function Returns the package height. Overrides PackageInterface::getHeight
Package::getLabelImage public function Returns package label image. Overrides PackageInterface::getLabelImage
Package::getLength public function Returns the package length. Overrides PackageInterface::getLength
Package::getLengthUnits public function Returns the package units of length. Overrides PackageInterface::getLengthUnits
Package::getOrderId public function Returns the order ID of this shipment. Overrides PackageInterface::getOrderId
Package::getPackageType public function Returns the package type. Overrides PackageInterface::getPackageType
Package::getProducts public function Returns all the products in this package. Overrides PackageInterface::getProducts
Package::getShippingType public function Returns the shipping type. Overrides PackageInterface::getShippingType
Package::getSid public function Returns the shipment ID. Overrides PackageInterface::getSid
Package::getTrackingNumber public function Returns the package tracking number. Overrides PackageInterface::getTrackingNumber
Package::getValue public function Returns the package monetary value. Overrides PackageInterface::getValue
Package::getWeight public function Returns the package weight. Overrides PackageInterface::getWeight
Package::getWeightUnits public function Returns the package units of weight. Overrides PackageInterface::getWeightUnits
Package::getWidth public function Returns the package width. Overrides PackageInterface::getWidth
Package::id public function
Package::load public static function Loads a package and its products.
Package::loadByOrder public static function Loads packages for a given order.
Package::save public function Saves this package.
Package::setAddresses public function Sets the list of ship-from addresses for this package. Overrides PackageInterface::setAddresses
Package::setCurrency public function Sets the currency code used for the package value. Overrides PackageInterface::setCurrency
Package::setDescription public function Sets the package description. Overrides PackageInterface::setDescription
Package::setHeight public function Sets the package height. Overrides PackageInterface::setHeight
Package::setLabelImage public function Sets package label image. Overrides PackageInterface::setLabelImage
Package::setLength public function Sets the package length. Overrides PackageInterface::setLength
Package::setLengthUnits public function Sets the package units of length. Overrides PackageInterface::setLengthUnits
Package::setOrderId public function Sets the order ID of this shipment. Overrides PackageInterface::setOrderId
Package::setPackageType public function Sets the package type to the given value. Overrides PackageInterface::setPackageType
Package::setProducts public function Sets all the products in this package. Overrides PackageInterface::setProducts
Package::setShippingType public function Sets the shipping type to the given value. Overrides PackageInterface::setShippingType
Package::setSid public function Sets the shipment ID. Overrides PackageInterface::setSid
Package::setTrackingNumber public function Sets the package tracking number. Overrides PackageInterface::setTrackingNumber
Package::setValue public function Sets the package monetary value. Overrides PackageInterface::setValue
Package::setWeight public function Sets the package weight. Overrides PackageInterface::setWeight
Package::setWeightUnits public function Sets the package units of weight. Overrides PackageInterface::setWeightUnits
Package::setWidth public function Sets the package width. Overrides PackageInterface::setWidth
Package::__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.