View source
<?php
namespace Drupal\uc_fulfillment;
use Drupal\Core\Messenger\MessengerTrait;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\uc_fulfillment\Event\ShipmentSaveEvent;
use Drupal\uc_order\Entity\Order;
use Drupal\uc_store\Address;
use Drupal\uc_store\AddressInterface;
class Shipment implements ShipmentInterface {
use MessengerTrait;
use StringTranslationTrait;
protected $sid;
protected $orderId;
protected $shipping_method = '';
protected $accessorials = '';
protected $carrier = '';
protected $transaction_id = '';
protected $tracking_number = '';
protected $ship_date = 0;
protected $expected_delivery = 0;
protected $cost = 0;
protected $currency = '';
protected $changed = 0;
protected $packages = [];
protected $origin;
protected $destination;
public function id() {
return $this->sid;
}
public function setOrderId($orderId) {
$this->orderId = $orderId;
return $this;
}
public function getOrderId() {
return $this->orderId;
}
public function setShippingMethod($shipping_method) {
$this->shipping_method = $shipping_method;
return $this;
}
public function getShippingMethod() {
return $this->shipping_method;
}
public function setAccessorials($accessorials) {
$this->accessorials = $accessorials;
return $this;
}
public function getAccessorials() {
return $this->accessorials;
}
public function setCarrier($carrier) {
$this->carrier = $carrier;
return $this;
}
public function getCarrier() {
return $this->carrier;
}
public function setTransactionId($transaction_id) {
$this->transaction_id = $transaction_id;
return $this;
}
public function getTransactionId() {
return $this->transaction_id;
}
public function setTrackingNumber($tracking_number) {
$this->tracking_number = $tracking_number;
return $this;
}
public function getTrackingNumber() {
return $this->tracking_number;
}
public function setShipDate($ship_date) {
$this->ship_date = $ship_date;
return $this;
}
public function getShipDate() {
return $this->ship_date;
}
public function setExpectedDelivery($expected_delivery) {
$this->expected_delivery = $expected_delivery;
return $this;
}
public function getExpectedDelivery() {
return $this->expected_delivery;
}
public function setCost($cost) {
$this->cost = $cost;
return $this;
}
public function getCost() {
return $this->cost;
}
public function setCurrency($currency) {
$this->currency = $currency;
return $this;
}
public function getCurrency() {
return $this->currency;
}
public function setChanged($changed) {
$this->changed = $changed;
return $this;
}
public function getChanged() {
return $this->changed;
}
public function setPackages(array $packages) {
$this->packages = $packages;
return $this;
}
public function getPackages() {
return $this->packages;
}
public function setOrigin(AddressInterface $origin) {
$this->origin = $origin;
return $this;
}
public function getOrigin() {
return $this->origin;
}
public function setDestination(AddressInterface $destination) {
$this->destination = $destination;
return $this;
}
public function getDestination() {
return $this->destination;
}
protected function __construct() {
}
public static function create(array $values = NULL) {
$shipment = new Shipment();
if (isset($values)) {
foreach ($values as $key => $value) {
$shipment->{$key} = $value;
}
}
return $shipment;
}
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;
}
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;
}
}
$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;
}
public function save() {
$this->changed = \Drupal::time()
->getCurrentTime();
$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;
}
}
$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);
$products = $package
->getProducts();
$package
->setProducts([]);
$package
->save();
$package
->setProducts($products);
}
}
\Drupal::moduleHandler()
->invokeAll('uc_shipment', [
'save',
$this,
]);
$order = Order::load($this->orderId);
$event = new ShipmentSaveEvent($order, $this);
\Drupal::service('event_dispatcher')
->dispatch($event::EVENT_NAME, $event);
}
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,
]));
}
}