View source
<?php
namespace Drupal\commerce_fedex_dry_ice\Plugin\Commerce\FedEx;
use Drupal\address\Plugin\Field\FieldType\AddressItem;
use Drupal\commerce_fedex\Plugin\Commerce\FedEx\FedExPluginBase;
use Drupal\commerce_fedex\Plugin\Commerce\ShippingMethod\FedEx;
use Drupal\commerce_shipping\Entity\ShipmentInterface;
use Drupal\commerce_shipping\PackageTypeManagerInterface;
use Drupal\commerce_shipping\ShipmentItem;
use Drupal\Core\Form\FormStateInterface;
use Drupal\physical\Weight;
use Drupal\physical\WeightUnit;
use NicholasCreativeMedia\FedExPHP\Enums\PackageSpecialServiceType;
use NicholasCreativeMedia\FedExPHP\Structs\PackageSpecialServicesRequested;
use NicholasCreativeMedia\FedExPHP\Structs\RequestedPackageLineItem;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DryIcePlugin extends FedExPluginBase {
const NOT_DRY_ICE = 0;
const DRY_ICE = 1;
protected $packageTypeManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, PackageTypeManagerInterface $package_type_manager) {
$this->packageTypeManager = $package_type_manager;
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this
->setConfiguration($configuration);
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('plugin.manager.commerce_package_type'));
}
public function defaultConfiguration() {
$config = [
'domestic' => [
'package_type' => 'custom_box',
'weight' => [
'number' => 0,
'unit' => WeightUnit::KILOGRAM,
],
],
'intl' => [
'package_type' => 'custom_box',
'weight' => [
'number' => 0,
'unit' => WeightUnit::KILOGRAM,
],
],
] + parent::defaultConfiguration();
return $config;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$package_types = $this->packageTypeManager
->getDefinitionsByShippingMethod('fedex');
$package_types = array_map(function ($package_type) {
return $package_type['label'];
}, $package_types);
$form['domestic']['package_type'] = [
'#type' => 'select',
'#title' => $this
->t('Dry ice package type'),
'#options' => $package_types,
'#default_value' => $this->configuration['domestic']['package_type'],
'#required' => TRUE,
'#access' => count($package_types) > 1,
];
$form['domestic']['weight'] = [
'#type' => 'physical_measurement',
'#measurement_type' => 'weight',
'#title' => $this
->t('Dry Ice Weight'),
'#default_value' => $this->configuration['domestic']['weight'],
'#size' => 5,
'#maxlength' => 4,
'#required' => TRUE,
];
$form['intl']['package_type'] = [
'#type' => 'select',
'#title' => $this
->t('International dry ice package type'),
'#options' => $package_types,
'#default_value' => $this->configuration['intl']['package_type'],
'#required' => TRUE,
'#access' => count($package_types) > 1,
];
$form['intl']['weight'] = [
'#type' => 'physical_measurement',
'#measurement_type' => 'weight',
'#title' => $this
->t('International dry Ice Weight'),
'#default_value' => $this->configuration['intl']['weight'],
'#size' => 5,
'#maxlength' => 4,
'#required' => TRUE,
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
if (!$form_state
->getErrors()) {
$values = $form_state
->getValues();
$this->configuration['domestic']['package_type'] = $values['domestic']['package_type'];
$this->configuration['domestic']['weight'] = $values['domestic']['weight'];
$this->configuration['intl']['package_type'] = $values['intl']['package_type'];
$this->configuration['intl']['weight'] = $values['intl']['weight'];
}
}
public function adjustPackage(RequestedPackageLineItem $package, array $shipment_items, ShipmentInterface $shipment) {
$type = $this
->getType($shipment);
if (!$this
->verifyPackage($shipment_items, $type)) {
throw new \Exception("Package cannot be shipped, mix of Dry Ice and non Dry Ice Items");
}
if ($this
->isDryIceItem(reset($shipment_items), $type)) {
$special_services_requested = $package
->getSpecialServicesRequested();
if (empty($special_services_requested)) {
$special_services_requested = new PackageSpecialServicesRequested();
}
$dry_ice_weight = new Weight($this->configuration[$type]['weight']['number'], $this->configuration[$type]['weight']['unit']);
$special_services_requested
->addToSpecialServiceTypes(PackageSpecialServiceType::VALUE_DRY_ICE);
$special_services_requested
->setDryIceWeight(FedEx::physicalWeightToFedex($dry_ice_weight));
$package_type = $this->packageTypeManager
->createInstance($this->configuration[$type]['package_type']);
$package
->setDimensions(Fedex::packageToFedexDimensions($package_type));
$package
->setWeight(FedEx::packageTotalWeight($shipment_items, $package_type, $dry_ice_weight));
$package
->setSpecialServicesRequested($special_services_requested);
}
$package = parent::adjustPackage($package, $shipment_items, $shipment);
return $package;
}
public function splitPackage(array $shipment_items, ShipmentInterface $shipment) {
$type = $this
->getType($shipment);
$packages = [
static::NOT_DRY_ICE => [],
static::DRY_ICE => [],
];
foreach ($shipment_items as $shipment_item) {
if ($this
->isDryIceItem($shipment_item, $type)) {
$packages[static::DRY_ICE][] = $shipment_item;
}
else {
$packages[static::NOT_DRY_ICE][] = $shipment_item;
}
}
return $packages;
}
protected function verifyPackage(array $shipment_items, $type) {
if (!is_string($type)) {
$varType = gettype($type);
throw new \Exception("Shipment type must be string. {$varType} given.");
}
$dryIceBox = $this
->isDryIceBox($shipment_items, $type);
foreach ($shipment_items as $shipment_item) {
if ($dryIceBox xor $this
->isDryIceItem($shipment_item, $type)) {
return FALSE;
}
}
return TRUE;
}
protected function isDryIceBox(array $shipment_items, $type) {
if (!is_string($type)) {
$varType = gettype($type);
throw new \Exception("Shipment type must be string. {$varType} given.");
}
return $this
->isDryIceItem(reset($shipment_items), $type);
}
protected function isDryIceItem(ShipmentItem $shipment_item, $type) {
if (!is_string($type)) {
$varType = gettype($type);
throw new \Exception("Shipment type must be string. {$varType} given.");
}
$storage = \Drupal::entityTypeManager()
->getStorage('commerce_order_item');
$order_item = $storage
->load($shipment_item
->getOrderItemId());
$purchased_entity = $order_item
->getPurchasedEntity();
return $purchased_entity
->hasField('fedex_dry_ice_' . $type) && !$purchased_entity
->get('fedex_dry_ice_' . $type)
->isEmpty() && $purchased_entity
->get('fedex_dry_ice_' . $type)
->first()
->getValue()['value'] == 1;
}
protected function getType(ShipmentInterface $shipment) {
$shipping_address = $shipment
->getShippingProfile()
->get('address')
->first();
$domestic = $shipping_address instanceof AddressItem ? $shipment
->getOrder()
->getStore()
->getAddress()
->getCountryCode() == $shipping_address
->getCountryCode() : FALSE;
return $domestic ? 'domestic' : 'intl';
}
}