Skip to content

Commit

Permalink
feat(carriers): add ups shipping options (#873)
Browse files Browse the repository at this point in the history
  • Loading branch information
joerivanveen authored Nov 14, 2024
1 parent 1d57acf commit b223ac7
Show file tree
Hide file tree
Showing 13 changed files with 200 additions and 5 deletions.
1 change: 1 addition & 0 deletions Adapter/ShipmentOptionsFromAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function __construct(array $inputData)
{
$options = $inputData;
$this->signature = (bool) ($options['signature'] ?? false);
$this->collect = (bool) ($options['collect'] ?? false);
$this->receipt_code = (bool) ($options['receipt_code'] ?? false);
$this->only_recipient = (bool) ($options['only_recipient'] ?? false);
$this->large_format = (bool) ($options['large_format'] ?? false);
Expand Down
8 changes: 5 additions & 3 deletions Block/Sales/NewShipmentForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ class NewShipmentForm
/**
* @var array
*/
private $shipmentOptionsExplanation;
private $shipmentOptionsExplanationMap;


public function __construct()
{
$this->shipmentOptionsHumanMap = [
AbstractConsignment::SHIPMENT_OPTION_SIGNATURE => __('Signature on receipt'),
AbstractConsignment::SHIPMENT_OPTION_COLLECT => __('Collect package'),
AbstractConsignment::SHIPMENT_OPTION_RECEIPT_CODE => __('Receiving code'),
AbstractConsignment::SHIPMENT_OPTION_ONLY_RECIPIENT => __('Home address only'),
AbstractConsignment::SHIPMENT_OPTION_AGE_CHECK => __('Age check 18+'),
Expand All @@ -57,8 +58,9 @@ public function __construct()
AbstractConsignment::SHIPMENT_OPTION_RETURN => __('Return if no answer'),
AbstractConsignment::SHIPMENT_OPTION_SAME_DAY_DELIVERY => __('Same day delivery'),
];
$this->shipmentOptionsExplanation = [
$this->shipmentOptionsExplanationMap = [
AbstractConsignment::SHIPMENT_OPTION_RECEIPT_CODE => __('Insurance is mandatory and will be set. Other shipment options will be removed.'),
AbstractConsignment::SHIPMENT_OPTION_COLLECT => __('The package will be collected at the location for this webshop. Please make sure you completely filled in the address including the company name in the MyParcel Backoffice. You can do so under ‘Shop settings’ -> ‘Company details’ under the tab ‘Return address’. Verify that you selected the shop corresponding to the API key you are using in your Magento installation.'),
];
}

Expand Down Expand Up @@ -90,6 +92,6 @@ public function getShipmentOptionsHumanMap(): array
*/
public function getShipmentOptionsExplanationMap(): array
{
return $this->shipmentOptionsExplanation;
return $this->shipmentOptionsExplanationMap;
}
}
8 changes: 8 additions & 0 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment;
use MyParcelNL\Sdk\src\Model\Consignment\DropOffPoint;
use MyParcelNL\Sdk\src\Services\CheckApiKeyService;
use MyParcelNL\Sdk\src\Services\CountryCodes;

class Data extends AbstractHelper
{
Expand Down Expand Up @@ -269,6 +270,13 @@ public function consignmentHasShipmentOption(AbstractConsignment $consignment, s
AbstractConsignment::SHIPMENT_OPTION_SIGNATURE], true);
}

// UPS shipment options are available for all countries in the EU
if (CarrierUPS::NAME === $consignment->getCarrierName()
&& in_array($consignment->getCountry(), CountryCodes::EU_COUNTRIES, false)
) {
return true;
}

// No shipment options available in any other case
return false;
}
Expand Down
9 changes: 9 additions & 0 deletions Helper/ShipmentOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class ShipmentOptions
private const ONLY_RECIPIENT = 'only_recipient';
private const SAME_DAY_DELIVERY = 'same_day_delivery';
private const SIGNATURE = 'signature';
private const COLLECT = AbstractConsignment::SHIPMENT_OPTION_COLLECT;
private const RECEIPT_CODE = AbstractConsignment::SHIPMENT_OPTION_RECEIPT_CODE;
private const RETURN = 'return';
private const AGE_CHECK = 'age_check';
Expand Down Expand Up @@ -125,6 +126,13 @@ public function hasReceiptCode(): bool
return self::getValueOfOptionWhenSet(self::RECEIPT_CODE, $this->options) ?? $this->optionIsEnabled(self::RECEIPT_CODE);
}

public function hasCollect(): bool
{
$collectFromOptions = self::getValueOfOptionWhenSet(self::COLLECT, $this->options);

return $collectFromOptions ?? $this->optionIsEnabled(self::COLLECT);
}

/**
* @return bool
*/
Expand Down Expand Up @@ -399,6 +407,7 @@ public function getShipmentOptions(): array
self::RETURN => $this->hasReturn(),
self::ONLY_RECIPIENT => $this->hasOnlyRecipient(),
self::SIGNATURE => $this->hasSignature(),
self::COLLECT => $this->hasCollect(),
self::RECEIPT_CODE => $this->hasReceiptCode(),
self::AGE_CHECK => $this->hasAgeCheck(),
self::LARGE_FORMAT => $this->hasLargeFormat(),
Expand Down
8 changes: 8 additions & 0 deletions Model/Quote/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ private function getDeliveryData(?string $packageType = null): array
$canHaveMonday = $consignment->canHaveExtraOption(AbstractConsignment::EXTRA_OPTION_DELIVERY_MONDAY);
$canHaveMorning = $consignment->canHaveDeliveryType(AbstractConsignment::DELIVERY_TYPE_MORNING_NAME);
$canHaveEvening = $consignment->canHaveDeliveryType(AbstractConsignment::DELIVERY_TYPE_EVENING_NAME);
$canHaveExpress = $consignment->canHaveDeliveryType(AbstractConsignment::DELIVERY_TYPE_EXPRESS_NAME);
$canHaveSignature = $consignment->canHaveShipmentOption(AbstractConsignment::SHIPMENT_OPTION_SIGNATURE);
$canHaveCollect = $consignment->canHaveShipmentOption(AbstractConsignment::SHIPMENT_OPTION_COLLECT);
$canHaveReceiptCode = $consignment->canHaveShipmentOption(AbstractConsignment::SHIPMENT_OPTION_RECEIPT_CODE);
$canHaveOnlyRecipient = $consignment->canHaveShipmentOption(AbstractConsignment::SHIPMENT_OPTION_ONLY_RECIPIENT);
$canHaveAgeCheck = $consignment->canHaveShipmentOption(AbstractConsignment::SHIPMENT_OPTION_AGE_CHECK);
Expand All @@ -212,6 +214,7 @@ private function getDeliveryData(?string $packageType = null): array
$eveningFee = $canHaveEvening ? $this->helper->getMethodPrice($carrierPath, 'evening/fee') : 0;
$sameDayFee = $canHaveSameDay ? (int) $this->helper->getMethodPrice($carrierPath, 'delivery/same_day_delivery_fee') : 0;
$signatureFee = $canHaveSignature ? $this->helper->getMethodPrice($carrierPath, 'delivery/signature_fee', false) : 0;
$collectFee = $canHaveCollect ? $this->helper->getMethodPrice($carrierPath, 'delivery/collect_fee', false) : 0;
$receiptCodeFee = $canHaveReceiptCode ? $this->helper->getMethodPrice($carrierPath, 'delivery/receipt_code_fee', false) : 0;
$onlyRecipientFee = $canHaveOnlyRecipient ? $this->helper->getMethodPrice($carrierPath, 'delivery/only_recipient_fee', false) : 0;
$isAgeCheckActive = $canHaveAgeCheck && $this->isAgeCheckActive($carrierPath);
Expand All @@ -220,6 +223,7 @@ private function getDeliveryData(?string $packageType = null): array
$allowStandardDelivery = $this->helper->getBoolConfig($carrierPath, 'delivery/active');
$allowMorningDelivery = ! $isAgeCheckActive && $canHaveMorning && $this->helper->getBoolConfig($carrierPath, 'morning/active');
$allowEveningDelivery = ! $isAgeCheckActive && $canHaveEvening && $this->helper->getBoolConfig($carrierPath, 'evening/active');
$allowExpressDelivery = $canHaveExpress && $this->helper->getBoolConfig($carrierPath, 'express/active');
$allowDeliveryOptions = ! $this->package->deliveryOptionsDisabled
&& ($allowPickup || $allowStandardDelivery || $allowMorningDelivery || $allowEveningDelivery);

Expand All @@ -232,6 +236,7 @@ private function getDeliveryData(?string $packageType = null): array
$myParcelConfig['carrierSettings'][$carrier] = [
'allowDeliveryOptions' => $allowDeliveryOptions,
'allowStandardDelivery' => $allowStandardDelivery,
'allowCollect' => $canHaveCollect && $this->helper->getBoolConfig($carrierPath, 'delivery/collect_active'),
'allowSignature' => $canHaveSignature && $this->helper->getBoolConfig($carrierPath, 'delivery/signature_active'),
'allowReceiptCode' => $canHaveReceiptCode && $this->helper->getBoolConfig($carrierPath, 'delivery/receipt_code_active'),
'allowOnlyRecipient' => $canHaveOnlyRecipient && $this->helper->getBoolConfig($carrierPath, 'delivery/only_recipient_active'),
Expand All @@ -240,17 +245,20 @@ private function getDeliveryData(?string $packageType = null): array
'allowPickupLocations' => $canHavePickup && $this->isPickupAllowed($carrierPath),
'allowMondayDelivery' => $canHaveMonday && $this->helper->getBoolConfig($carrierPath, 'delivery/monday_active'),
'allowSameDayDelivery' => $canHaveSameDay && $this->helper->getBoolConfig($carrierPath, 'delivery/same_day_delivery_active'),
'allowExpressDelivery' => $allowExpressDelivery,

'dropOffDays' => $this->getDropOffDays($carrierPath),

'priceSignature' => $signatureFee,
'priceCollect' => $collectFee,
'priceReceiptCode' => $receiptCodeFee,
'priceOnlyRecipient' => $onlyRecipientFee,
'priceStandardDelivery' => $showTotalPrice ? $basePrice : 0,
'priceMondayDelivery' => $mondayFee,
'priceMorningDelivery' => $morningFee,
'priceEveningDelivery' => $eveningFee,
'priceSameDayDelivery' => $sameDayFee,
'priceExpressDelivery' => $allowExpressDelivery ? $this->helper->getMethodPrice($carrierPath, 'express/fee') : 0,
'priceSameDayDeliveryAndOnlyRecipient' => $sameDayFee + $onlyRecipientFee,

'priceMorningSignature' => ($morningFee + $signatureFee),
Expand Down
1 change: 1 addition & 0 deletions Model/Sales/MagentoCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ abstract class MagentoCollection implements MagentoCollectionInterface
'carrier' => 'postnl',
'positions' => null,
'signature' => null,
'collect' => null,
'receipt_code' => null,
'only_recipient' => null,
'return' => null,
Expand Down
1 change: 1 addition & 0 deletions Model/Sales/TrackTraceHolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ public function convertDataFromMagentoToApi(Track $magentoTrack, array $options)
->setDropOffPoint($dropOffPoint)
->setOnlyRecipient($this->shipmentOptionsHelper->hasOnlyRecipient())
->setSignature($this->shipmentOptionsHelper->hasSignature())
->setCollect($this->shipmentOptionsHelper->hasCollect())
->setReceiptCode($this->shipmentOptionsHelper->hasReceiptCode())
->setReturn($this->shipmentOptionsHelper->hasReturn())
->setSameDayDelivery($this->shipmentOptionsHelper->hasSameDayDelivery())
Expand Down
2 changes: 1 addition & 1 deletion Model/Source/DefaultOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function __construct(Order $order, Data $helper)
/**
* Get default of the option
*
* @param string $option 'only_recipient'|'signature'|'receipt_code'|'return'|'large_format'
* @param string $option 'only_recipient'|'signature'|'collect'|'receipt_code'|'return'|'large_format'
* @param string $carrier
*
* @return bool
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"type": "magento2-module",
"require": {
"php": "^7.1 || ^8.0",
"myparcelnl/sdk": "~v7.18",
"myparcelnl/sdk": "~v7.19",
"magento/framework": ">=101.0.8 <102 || >=102.0.1"
},
"require-dev": {
Expand Down
Loading

0 comments on commit b223ac7

Please sign in to comment.