class Shipment in Ubercart 8.4
Defines the Shipment class.
Hierarchy
- class \Drupal\uc_fulfillment\Shipment implements ShipmentInterface uses MessengerTrait, StringTranslationTrait
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
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_fulfillmentView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
Shipment:: |
protected | property | Shipping quote accessorials. | |
Shipment:: |
protected | property | Name of the common carrier. | |
Shipment:: |
protected | property | Last modified timestamp. | |
Shipment:: |
protected | property | Name of the status. | |
Shipment:: |
protected | property | Currency code. | |
Shipment:: |
protected | property | Shipment destination address. | |
Shipment:: |
protected | property | Expected delivery timestamp. | |
Shipment:: |
protected | property | Order ID of this shipment. | |
Shipment:: |
protected | property | Shipment origin address. | |
Shipment:: |
protected | property | Packages contained in this shipment. | |
Shipment:: |
protected | property | Name of the shipping method. | |
Shipment:: |
protected | property | Ship date timestamp. | |
Shipment:: |
protected | property | Shipment ID. | |
Shipment:: |
protected | property | Shipment tracking number. | |
Shipment:: |
protected | property | Shipment transaction ID. | |
Shipment:: |
public static | function | Creates a Shipment. | |
Shipment:: |
public | function | Deletes this shipment. | |
Shipment:: |
public | function |
Returns the shipping quote accessorials of this shipment. Overrides ShipmentInterface:: |
|
Shipment:: |
public | function |
Returns the name of the common carrier. Overrides ShipmentInterface:: |
|
Shipment:: |
public | function |
Returns the last modified timestamp. Overrides ShipmentInterface:: |
|
Shipment:: |
public | function |
Returns the shipping cost for this shipment. Overrides ShipmentInterface:: |
|
Shipment:: |
public | function |
Returns the currency code used for the shipping cost. Overrides ShipmentInterface:: |
|
Shipment:: |
public | function |
Returns the destination address for this shipment. Overrides ShipmentInterface:: |
|
Shipment:: |
public | function |
Returns the expected delivery timestamp. Overrides ShipmentInterface:: |
|
Shipment:: |
public | function |
Returns the order ID of this shipment. Overrides ShipmentInterface:: |
|
Shipment:: |
public | function |
Returns the origin address for this shipment. Overrides ShipmentInterface:: |
|
Shipment:: |
public | function |
Returns the packages in this shipment. Overrides ShipmentInterface:: |
|
Shipment:: |
public | function |
Returns the ship date timestamp of this shipment. Overrides ShipmentInterface:: |
|
Shipment:: |
public | function |
Returns the name of the shipping method. Overrides ShipmentInterface:: |
|
Shipment:: |
public | function |
Returns the tracking number of this shipment. Overrides ShipmentInterface:: |
|
Shipment:: |
public | function |
Returns the transaction ID of this shipment. Overrides ShipmentInterface:: |
|
Shipment:: |
public | function | ||
Shipment:: |
public static | function | Loads a shipment and its packages. | |
Shipment:: |
public static | function | Loads a shipment and its packages for a given order. | |
Shipment:: |
public | function | Saves this shipment. | |
Shipment:: |
public | function |
Sets the shipping quote accessorials of this shipment. Overrides ShipmentInterface:: |
|
Shipment:: |
public | function |
Sets the common carrier name to the given value. Overrides ShipmentInterface:: |
|
Shipment:: |
public | function |
Sets the last modified timestamp. Overrides ShipmentInterface:: |
|
Shipment:: |
public | function |
Sets the shipping cost for this shipment. Overrides ShipmentInterface:: |
|
Shipment:: |
public | function |
Sets the currency code used for the shipping cost. Overrides ShipmentInterface:: |
|
Shipment:: |
public | function |
Sets the destination address for this shipment. Overrides ShipmentInterface:: |
|
Shipment:: |
public | function |
Sets the expected delivery timestamp. Overrides ShipmentInterface:: |
|
Shipment:: |
public | function |
Sets the order ID to the given value. Overrides ShipmentInterface:: |
|
Shipment:: |
public | function |
Sets the origin address for this shipment. Overrides ShipmentInterface:: |
|
Shipment:: |
public | function |
Sets the packages in this shipment. Overrides ShipmentInterface:: |
|
Shipment:: |
public | function |
Sets the ship date timestamp to the given value. Overrides ShipmentInterface:: |
|
Shipment:: |
public | function |
Sets the shipping method to the given value. Overrides ShipmentInterface:: |
|
Shipment:: |
public | function |
Sets the tracking number of this shipment. Overrides ShipmentInterface:: |
|
Shipment:: |
public | function |
Sets the transaction ID of this shipment. Overrides ShipmentInterface:: |
|
Shipment:: |
protected | function | Constructor. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |