Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2878092: "Each item in its own box" combines items with a qty higher than 1 #8

Open
wants to merge 8 commits into
base: 8.x-1.x
Choose a base branch
from
7 changes: 7 additions & 0 deletions modules/dangerous/config/schema/commerce_fedex_dangerous.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
commerce_fedex.fedex_plugin.plugin.dangerous:
type: commerce_fedex_service
label: 'Commerce Fedex Dry Ice Service'
mapping:
contact_number:
type: string
label: 'Contact Number'
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace Drupal\commerce_fedex_dangerous\Plugin\Commerce\EntityTrait;

use Drupal\commerce_fedex_dangerous\PurchasableEntityHazardousBase;
use Drupal\commerce\BundleFieldDefinition;
use Drupal\commerce\Plugin\Commerce\EntityTrait\EntityTraitBase;
use Drupal\commerce_fedex\Plugin\Commerce\ShippingMethod\FedEx;
use NicholasCreativeMedia\FedExPHP\Enums\DangerousGoodsAccessibilityType;

/**
* Provides the "fedex_dangerous" trait.
Expand All @@ -13,14 +16,22 @@
* entity_types = {"commerce_product_variation"}
* )
*/
class PurchasableEntityDangerousGoods extends PurchasableEntityHazardousBase {
class PurchasableEntityDangerousGoods extends EntityTraitBase {

/**
* {@inheritdoc}
*/
public function buildFieldDefinitions() {
$fields = $this->baseFields();
$id = $this->getPluginId();

$fields[$id . '_accessibility'] = BundleFieldDefinition::create('list_string')
->setLabel($this->t('Require Dangerous Goods/Hazardous Materials Shipping'))
->setCardinality(1)
->setSetting('allowed_values', [0 => "None"] + FedEx::enumToList(DangerousGoodsAccessibilityType::getValidValues()))
->setDisplayOptions('form', [
'type' => 'options_select',
'weight' => 95,
]);
return $fields;
}

Expand Down
114 changes: 114 additions & 0 deletions modules/dangerous/src/Plugin/Commerce/FedEx/DangerousGoodsPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

namespace Drupal\commerce_fedex_dangerous\Plugin\Commerce\FedEx;

use Drupal\commerce_fedex\Plugin\Commerce\FedEx\FedExPluginBase;
use Drupal\commerce_shipping\Entity\ShipmentInterface;
use Drupal\commerce_shipping\ShipmentItem;
use Drupal\Core\Form\FormStateInterface;
use NicholasCreativeMedia\FedExPHP\Enums\PackageSpecialServiceType;
use NicholasCreativeMedia\FedExPHP\Structs\DangerousGoodsDetail;
use NicholasCreativeMedia\FedExPHP\Structs\PackageSpecialServicesRequested;
use NicholasCreativeMedia\FedExPHP\Structs\RequestedPackageLineItem;

/**
* Providex the FedEx Dangerous Goods Service Plugin.
*
* @CommerceFedExPlugin(
* id = "dangerous",
* label = @Translation("FedEx Dangerous Goods"),
* options_label = @Translation("Dangerous Goods Shipment Options"),
* options_description = @Translation("Enter your global shipping options for Dangerous Goods shipments")
* )
*/
class DangerousGoodsPlugin extends FedExPluginBase {
const NOT_DANGEROUS = 0;

/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return ['contact_number' => ''] + parent::defaultConfiguration();

}

/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['contact_number'] = [
'#type' => 'textfield',
'#title' => $this->t("Contact Number"),
'#description' => $this->t('Enter the Phone number of your dangerous goods/hazardous materials contact person'),
'#default_value' => $this->configuration['contact_number'],
];
return $form;
}

/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this->configuration['contact_number'] = $form_state->getValue('contact_number');
}

/**
* {@inheritdoc}
*/
public function adjustPackage(RequestedPackageLineItem $package, array $shipment_items, ShipmentInterface $shipment) {
$status = $this->getDangerousStatus(reset($shipment_items));

if ($status === static::NOT_DANGEROUS) {
return $package;
}

$special_services_requested = $package->getSpecialServicesRequested();
if (empty($special_services_requested)) {
$special_services_requested = new PackageSpecialServicesRequested();
}
$special_services_requested->addToSpecialServiceTypes(PackageSpecialServiceType::VALUE_DANGEROUS_GOODS);
$dangerous_goods_detail = $special_services_requested->getDangerousGoodsDetail();
if (empty($dangerous_goods_detail)) {
$dangerous_goods_detail = new DangerousGoodsDetail();
}
$dangerous_goods_detail->setAccessibility($status);
$special_services_requested->setDangerousGoodsDetail($dangerous_goods_detail);
$package->setSpecialServicesRequested($special_services_requested);

return $package;
}

/**
* {@inheritdoc}
*/
public function splitPackage(array $shipment_items, ShipmentInterface $shipment) {
$packages = [];
foreach ($shipment_items as $shipment_item) {
$packages[$this->getDangerousStatus($shipment_item)][] = $shipment_item;
}
return array_values($packages);
}

/**
* Returns the DG status of an item.
*
* @param \Drupal\commerce_shipping\ShipmentItem $shipment_item
* The item to check.
*
* @return mixed
* The DG status.
*/
protected function getDangerousStatus(ShipmentItem $shipment_item) {
$storage = \Drupal::entityTypeManager()->getStorage('commerce_order_item');
/** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */
$order_item = $storage->load($shipment_item->getOrderItemId());
$purchased_entity = $order_item->getPurchasedEntity();
if (!$purchased_entity->hasField('fedex_dangerous_accessibility') || $purchased_entity->get('fedex_dangerous_accessibility')->isEmpty()) {
return static::NOT_DANGEROUS;
}
return $purchased_entity->get('fedex_dangerous_accessibility')->value;
}

}
96 changes: 0 additions & 96 deletions modules/dangerous/src/PurchasableEntityHazardousBase.php

This file was deleted.

13 changes: 12 additions & 1 deletion modules/dry_ice/config/schema/commerce_fedex_dry_ice.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,15 @@ commerce_fedex.fedex_plugin.plugin.dry_ice:
type: string
label: 'Package Type'
weight:
type: mapping
type: field.value.physical_measurement
label: 'Weight'
intl:
type: mapping
label: 'International'
mapping:
package_type:
type: string
label: 'Package Type'
weight:
type: field.value.physical_measurement
label: 'Weight'
Loading