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

feat: add receipt code #867

Merged
merged 8 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Adapter/ShipmentOptionsFromAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace MyParcelNL\Magento\Adapter;

use MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter;
use MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractShipmentOptionsAdapter;

class ShipmentOptionsFromAdapter extends AbstractShipmentOptionsAdapter
Expand All @@ -16,8 +15,9 @@ class ShipmentOptionsFromAdapter extends AbstractShipmentOptionsAdapter
*/
public function __construct(array $inputData)
{
$options = $inputData ?? [];
$options = $inputData;
$this->signature = (bool) ($options['signature'] ?? 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);
$this->age_check = (bool) ($options['age_check'] ?? false);
Expand Down
40 changes: 27 additions & 13 deletions Block/Sales/NewShipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@

namespace MyParcelNL\Magento\Block\Sales;

use Exception;
use Magento\Backend\Block\Template\Context;
use Magento\CatalogInventory\Api\StockConfigurationInterface;
use Magento\CatalogInventory\Api\StockRegistryInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Registry;
use Magento\Sales\Block\Adminhtml\Items\AbstractItems;
use MyParcelNL\Magento\Helper\Checkout;
use MyParcelNL\Magento\Model\Sales\MagentoOrderCollection;
use MyParcelNL\Magento\Model\Source\DefaultOptions;
use MyParcelNL\Magento\Helper\Data;
use MyParcelNL\Magento\Model\Sales\MagentoOrderCollection;
use MyParcelNL\Magento\Model\Sales\TrackTraceHolder;
use MyParcelNL\Magento\Model\Source\DefaultOptions;
use MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment;

class NewShipment extends AbstractItems
Expand Down Expand Up @@ -72,13 +73,14 @@ class NewShipment extends AbstractItems
* @param \Magento\Framework\ObjectManagerInterface $objectManager
*/
public function __construct(
Context $context,
Data $helper,
StockRegistryInterface $stockRegistry,
Context $context,
Data $helper,
StockRegistryInterface $stockRegistry,
StockConfigurationInterface $stockConfiguration,
Registry $registry,
ObjectManagerInterface $objectManager
) {
Registry $registry,
ObjectManagerInterface $objectManager
)
{
// Set order
$this->order = $registry->registry('current_shipment')->getOrder();
$this->dataHelper = $helper;
Expand All @@ -97,8 +99,8 @@ public function __construct(
}

/**
* @param string $option 'signature', 'only_recipient'
* @param string $carrier
* @param string $option 'signature', 'only_recipient'
* @param string $carrier
*
* @return bool
*/
Expand All @@ -110,8 +112,8 @@ public function hasDefaultOption(string $option, string $carrier): bool
/**
* Get default value of age check
*
* @param string $carrier
* @param string $option
* @param string $carrier
* @param string $option
*
* @return bool
*/
Expand All @@ -123,7 +125,7 @@ public function hasDefaultOptionsWithoutPrice(string $carrier, string $option):
/**
* Get default value of insurance based on order grand total
*
* @param string $carrier
* @param string $carrier
*
* @return int
*/
Expand Down Expand Up @@ -171,6 +173,18 @@ public function getCountry()
return $this->order->getShippingAddress()->getCountryId();
}

public function getDeliveryType(): int
{
try {
$deliveryTypeName = json_decode($this->order->getData(Checkout::FIELD_DELIVERY_OPTIONS), true)['deliveryType'];
$deliveryType = AbstractConsignment::DELIVERY_TYPES_NAMES_IDS_MAP[$deliveryTypeName];
} catch (Exception $e) {
$deliveryType = AbstractConsignment::DEFAULT_DELIVERY_TYPE;
}

return $deliveryType;
}

public function consignmentHasShipmentOption(AbstractConsignment $consignment, string $shipmentOption): bool
{
return $this->dataHelper->consignmentHasShipmentOption($consignment, $shipmentOption);
Expand Down
55 changes: 38 additions & 17 deletions Block/Sales/NewShipmentForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace MyParcelNL\Magento\Block\Sales;

use Exception;
use MyParcelNL\Sdk\src\Factory\ConsignmentFactory;
use MyParcelNL\Sdk\src\Model\Carrier\CarrierDHLEuroplus;
use MyParcelNL\Sdk\src\Model\Carrier\CarrierDHLForYou;
Expand All @@ -14,44 +15,56 @@

class NewShipmentForm
{
public const ALLOWED_CARRIER_CLASSES = [
CarrierPostNL::class,
CarrierDHLForYou::class,
CarrierDHLEuroplus::class,
CarrierDHLParcelConnect::class,
CarrierUPS::class,
CarrierDPD::class,
];
public const ALLOWED_CARRIER_CLASSES
= [
CarrierPostNL::class,
CarrierDHLForYou::class,
CarrierDHLEuroplus::class,
CarrierDHLParcelConnect::class,
CarrierUPS::class,
CarrierDPD::class,
];

public const PACKAGE_TYPE_HUMAN_MAP = [
AbstractConsignment::PACKAGE_TYPE_PACKAGE => 'Package',
AbstractConsignment::PACKAGE_TYPE_MAILBOX => 'Mailbox',
AbstractConsignment::PACKAGE_TYPE_LETTER => 'Letter',
AbstractConsignment::PACKAGE_TYPE_DIGITAL_STAMP => 'Digital stamp',
AbstractConsignment::PACKAGE_TYPE_PACKAGE_SMALL => 'Small package',
];
public const PACKAGE_TYPE_HUMAN_MAP
= [
AbstractConsignment::PACKAGE_TYPE_PACKAGE => 'Package',
AbstractConsignment::PACKAGE_TYPE_MAILBOX => 'Mailbox',
AbstractConsignment::PACKAGE_TYPE_LETTER => 'Letter',
AbstractConsignment::PACKAGE_TYPE_DIGITAL_STAMP => 'Digital stamp',
AbstractConsignment::PACKAGE_TYPE_PACKAGE_SMALL => 'Small package',
];

/**
* @var array
*/
private $shipmentOptionsHumanMap;

/**
* @var array
*/
private $shipmentOptionsExplanation;


public function __construct()
{
$this->shipmentOptionsHumanMap = [
$this->shipmentOptionsHumanMap = [
AbstractConsignment::SHIPMENT_OPTION_SIGNATURE => __('Signature on receipt'),
AbstractConsignment::SHIPMENT_OPTION_RECEIPT_CODE => __('Receiving code'),
AbstractConsignment::SHIPMENT_OPTION_ONLY_RECIPIENT => __('Home address only'),
AbstractConsignment::SHIPMENT_OPTION_AGE_CHECK => __('Age check 18+'),
AbstractConsignment::SHIPMENT_OPTION_HIDE_SENDER => __('Hide sender'),
AbstractConsignment::SHIPMENT_OPTION_LARGE_FORMAT => __('Large package'),
AbstractConsignment::SHIPMENT_OPTION_RETURN => __('Return if no answer'),
AbstractConsignment::SHIPMENT_OPTION_SAME_DAY_DELIVERY => __('Same day delivery'),
];
$this->shipmentOptionsExplanation = [
AbstractConsignment::SHIPMENT_OPTION_RECEIPT_CODE => __('Insurance is mandatory and will be set. Other shipment options will be removed.'),
];
}

/**
* @return AbstractConsignment[]
* @throws \Exception
* @throws Exception
*/
public function getCarrierSpecificAbstractConsignments(): array
{
Expand All @@ -71,4 +84,12 @@ public function getShipmentOptionsHumanMap(): array
{
return $this->shipmentOptionsHumanMap;
}

/**
* @return array
*/
public function getShipmentOptionsExplanationMap(): array
{
return $this->shipmentOptionsExplanation;
}
}
62 changes: 32 additions & 30 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ class Data extends AbstractHelper
public const XML_PATH_DPD_SETTINGS = 'myparcelnl_magento_dpd_settings/';
public const XML_PATH_LOCALE_WEIGHT_UNIT = 'general/locale/weight_unit';
public const DEFAULT_WEIGHT = 1000;
public const CARRIERS_XML_PATH_MAP = [
CarrierPostNL::NAME => self::XML_PATH_POSTNL_SETTINGS,
CarrierDHLForYou::NAME => self::XML_PATH_DHLFORYOU_SETTINGS,
CarrierDHLEuroplus::NAME => self::XML_PATH_DHLEUROPLUS_SETTINGS,
CarrierDHLParcelConnect::NAME => self::XML_PATH_DHLPARCELCONNECT_SETTINGS,
CarrierUPS::NAME => self::XML_PATH_UPS_SETTINGS,
CarrierDPD::NAME => self::XML_PATH_DPD_SETTINGS,
];
public const CARRIERS_XML_PATH_MAP
= [
CarrierPostNL::NAME => self::XML_PATH_POSTNL_SETTINGS,
CarrierDHLForYou::NAME => self::XML_PATH_DHLFORYOU_SETTINGS,
CarrierDHLEuroplus::NAME => self::XML_PATH_DHLEUROPLUS_SETTINGS,
CarrierDHLParcelConnect::NAME => self::XML_PATH_DHLPARCELCONNECT_SETTINGS,
CarrierUPS::NAME => self::XML_PATH_UPS_SETTINGS,
CarrierDPD::NAME => self::XML_PATH_DPD_SETTINGS,
];

/**
* @var \Magento\Framework\Module\ModuleListInterface
Expand All @@ -53,15 +54,16 @@ class Data extends AbstractHelper
/**
* Get settings by field
*
* @param Context $context
* @param ModuleListInterface $moduleList
* @param CheckApiKeyService $checkApiKeyService
* @param Context $context
* @param ModuleListInterface $moduleList
* @param CheckApiKeyService $checkApiKeyService
*/
public function __construct(
Context $context,
ModuleListInterface $moduleList,
CheckApiKeyService $checkApiKeyService
) {
)
{
parent::__construct($context);
$this->moduleList = $moduleList;
$this->checkApiKeyService = $checkApiKeyService;
Expand All @@ -71,7 +73,7 @@ public function __construct(
* Get settings by field
*
* @param $field
* @param null $storeId
* @param null $storeId
*
* @return mixed
*/
Expand All @@ -83,8 +85,8 @@ public function getConfigValue($field, $storeId = null)
/**
* Get general settings
*
* @param string $code
* @param null|int $storeId
* @param string $code
* @param null|int $storeId
*
* @return mixed
*/
Expand Down Expand Up @@ -117,9 +119,9 @@ public function getDropOffPoint(AbstractCarrier $carrier): ?DropOffPoint
/**
* Get default settings
*
* @param string $carrier
* @param string $code
* @param null $storeId
* @param string $carrier
* @param string $code
* @param null $storeId
*
* @return mixed
*/
Expand All @@ -131,8 +133,8 @@ public function getStandardConfig(string $carrier, string $code = '', $storeId =
/**
* Get carrier setting
*
* @param string $code
* @param string $carrier
* @param string $code
* @param string $carrier
*
* @return mixed
*/
Expand Down Expand Up @@ -174,7 +176,7 @@ public function apiKeyIsCorrect(): bool
$apiKey = $this->getApiKey();

return $this->checkApiKeyService->setApiKey($apiKey)
->apiKeyIsCorrect();
->apiKeyIsCorrect();
}

/**
Expand All @@ -200,7 +202,7 @@ public function hasApiKey(): bool
/**
* Get date in YYYY-MM-DD HH:MM:SS format
*
* @param string|null $date
* @param string|null $date
*
* @return string|null
*/
Expand All @@ -224,7 +226,7 @@ public function convertDeliveryDate(?string $date): ?string
/**
* Get delivery type and when it is null use 'standard'
*
* @param int|null $deliveryType
* @param int|null $deliveryType
*
* @return int
*/
Expand All @@ -238,16 +240,16 @@ public function checkDeliveryType(?int $deliveryType): int
}

/**
* @param int $orderId
* @param string $status
* @param int $orderId
* @param string $status
*/
public function setOrderStatus(int $orderId, string $status): void
{
$order = ObjectManager::getInstance()
->create('\Magento\Sales\Model\Order')
->load($orderId);
->create('\Magento\Sales\Model\Order')
->load($orderId);
$order->setState($status)
->setStatus($status);
->setStatus($status);
$order->save();
}

Expand All @@ -267,14 +269,14 @@ public function consignmentHasShipmentOption(AbstractConsignment $consignment, s
AbstractConsignment::SHIPMENT_OPTION_SIGNATURE], true);
}

// No shipment options available in any other cases
// No shipment options available in any other case
return false;
}

/**
* Get the correct weight type
*
* @param null|float $weight
* @param null|float $weight
*
* @return int
*/
Expand Down
Loading