View source
<?php
namespace Drupal\uc_fulfillment;
use Drupal\Core\Messenger\MessengerTrait;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\uc_order\Entity\OrderProduct;
class Package implements PackageInterface {
use MessengerTrait;
use StringTranslationTrait;
protected $package_id;
protected $sid;
protected $order_id;
protected $shipping_type = '';
protected $pkg_type = '';
protected $length = 1;
protected $width = 1;
protected $height = 1;
protected $length_units = '';
protected $weight = 0;
protected $weight_units = '';
protected $value = 0;
protected $currency = '';
protected $tracking_number = '';
protected $label_image;
protected $products = [];
protected $addresses = [];
protected $description = '';
protected static $packages = [];
public function id() {
return $this->package_id;
}
public function setSid($sid) {
$this->sid = $sid;
return $this;
}
public function getSid() {
return $this->sid;
}
public function setOrderId($order_id) {
$this->order_id = $order_id;
return $this;
}
public function getOrderId() {
return $this->order_id;
}
public function setShippingType($shipping_type) {
$this->shipping_type = $shipping_type;
return $this;
}
public function getShippingType() {
return $this->shipping_type;
}
public function setPackageType($pkg_type) {
$this->pkg_type = $pkg_type;
return $this;
}
public function getPackageType() {
return $this->pkg_type;
}
public function setLength($length) {
$this->length = $length;
return $this;
}
public function getLength() {
return $this->length;
}
public function setWidth($width) {
$this->width = $width;
return $this;
}
public function getWidth() {
return $this->width;
}
public function setHeight($height) {
$this->height = $height;
return $this;
}
public function getHeight() {
return $this->height;
}
public function setLengthUnits($length_units) {
$this->length_units = $length_units;
return $this;
}
public function getLengthUnits() {
return $this->length_units;
}
public function setWeight($weight) {
$this->weight = $weight;
return $this;
}
public function getWeight() {
return $this->weight;
}
public function setWeightUnits($weight_units) {
$this->weight_units = $weight_units;
return $this;
}
public function getWeightUnits() {
return $this->weight_units;
}
public function setValue($value) {
$this->value = $value;
return $this;
}
public function getValue() {
return $this->value;
}
public function setCurrency($currency) {
$this->currency = $currency;
return $this;
}
public function getCurrency() {
return $this->currency;
}
public function setTrackingNumber($tracking_number) {
$this->tracking_number = $tracking_number;
return $this;
}
public function getTrackingNumber() {
return $this->tracking_number;
}
public function setLabelImage($label_image) {
$this->label_image = $label_image;
return $this;
}
public function getLabelImage() {
return $this->label_image;
}
public function addProducts(array $products) {
$this->products = array_replace($this->products, $products);
return $this;
}
public function setProducts(array $products) {
$this->products = $products;
return $this;
}
public function getProducts() {
return $this->products;
}
public function setAddresses(array $addresses) {
$this->addresses = $addresses;
return $this;
}
public function getAddresses() {
return $this->addresses;
}
public function setDescription($description) {
$this->description = $description;
return $this;
}
public function getDescription() {
return $this->description;
}
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');
}
public static function create(array $values = NULL) {
$package = new Package();
if (isset($values)) {
foreach ($values as $key => $value) {
$package->{$key} = $value;
}
}
return $package;
}
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;
}
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 self::$packages[$package_id];
}
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->package_id = \Drupal::database()
->insert('uc_packages')
->fields($fields)
->execute();
$status = SAVED_NEW;
}
else {
\Drupal::database()
->update('uc_packages')
->fields($fields)
->condition('package_id', $this->package_id)
->execute();
$status = SAVED_UPDATED;
}
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,
]);
$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();
}
}
\Drupal::database()
->delete('uc_packaged_products')
->condition('package_id', $this->package_id)
->execute();
$insert
->execute();
}
return $status;
}
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);
}
foreach ($this->products as $id => $product) {
$order_product = OrderProduct::load($id);
if (NULL != $order_product) {
$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,
]));
}
}