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(pps): add customs declaration to order #636

Merged
merged 9 commits into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
117 changes: 117 additions & 0 deletions Helper/CustomsDeclarationFromOrder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace MyParcelNL\Magento\Helper;

use Magento\Catalog\Model\Product;
use Magento\Framework\ObjectManagerInterface;
use Magento\Sales\Model\Order;
use MyParcelNL\Magento\Model\Sales\TrackTraceHolder;
use MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment;
use MyParcelNL\Sdk\src\Model\CustomsDeclaration;
use MyParcelNL\Sdk\src\Model\MyParcelCustomsItem;
use Magento\Catalog\Api\ProductRepositoryInterface;
use MyParcelNL\Sdk\src\Support\Str;

class CustomsDeclarationFromOrder
{
private const CURRENCY_EURO = 'EUR';

/**
* @var \Magento\Framework\App\ObjectManager
*/
private $objectManager;

/**
* @var \Magento\Sales\Model\Order
*/
private $order;

/**
* @param \Magento\Sales\Model\Order $order
* @param \Magento\Framework\ObjectManagerInterface $objectManager
*/
public function __construct(Order $order, ObjectManagerInterface $objectManager)
{
$this->order = $order;
$this->objectManager = $objectManager;
}

/**
* @return \MyParcelNL\Sdk\src\Model\CustomsDeclaration
* @throws \MyParcelNL\Sdk\src\Exception\MissingFieldException
*/
public function createCustomsDeclaration(): CustomsDeclaration
{
$customsDeclaration = new CustomsDeclaration();
$totalWeight = 0;

foreach ($this->order->getItems() as $item) {
$product = $item->getProduct();

if (! $product) {
continue;
}

$totalWeight += $product->getWeight();

$customsItem = (new MyParcelCustomsItem())
->setDescription($this->getItemDescription($product->getName()))
->setAmount($item->getQtyShipped())
->setWeight($product->getWeight())
->setItemValueArray([
'amount' => TrackTraceHolder::getCentsByPrice($product->getPrice()),
'currency' => $this->order->getOrderCurrency()->getCode() ?? self::CURRENCY_EURO,
])
->setCountry($this->getCountryOfOrigin($product))
->setClassification($this->getHsCode($product));

$customsDeclaration->addCustomsItem($customsItem);
}

$customsDeclaration
->setContents(AbstractConsignment::PACKAGE_CONTENTS_COMMERCIAL_GOODS)
->setInvoice($this->order->getIncrementId())
->setWeight($totalWeight);

return $customsDeclaration;
}

/**
* @param \Magento\Catalog\Model\Product $product
*
* @return string
*/
public function getCountryOfOrigin(Product $product): string
{
$productCountryOfOrigin = $this->objectManager
->get(ProductRepositoryInterface::class)
->getById($product->getId())
->getCountryOfManufacture();

return $productCountryOfOrigin ?? AbstractConsignment::CC_NL;
}

/**
* @param $product
*
* @return int
*/
public function getHsCode($product): int
Mark-Ernst marked this conversation as resolved.
Show resolved Hide resolved
{
return (int) ShipmentOptions::getAttributeValue(
'catalog_product_entity_int',
$product->getId(),
'classification'
);
}

/**
* @param $description
*
* @return string
*/
public function getItemDescription($description): string
Mark-Ernst marked this conversation as resolved.
Show resolved Hide resolved
{
return Str::limit($description, AbstractConsignment::DESCRIPTION_MAX_LENGTH);
}
}
11 changes: 5 additions & 6 deletions Helper/ShipmentOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function hasAgeCheck(): bool
return false;
}

$ageCheckFromOptions = self::getValueOfOptionWhenSet($this->options, 'age_check');
$ageCheckFromOptions = self::getValueOfOptionWhenSet('age_check', $this->options);
Mark-Ernst marked this conversation as resolved.
Show resolved Hide resolved
$ageCheckOfProduct = self::getAgeCheckFromProduct($this->order->getItems());
$ageCheckFromSettings = self::$defaultOptions->getDefaultOptionsWithoutPrice(self::AGE_CHECK);

Expand Down Expand Up @@ -153,7 +153,7 @@ public static function getAgeCheckFromProduct($products): ?bool
*
* @return null|string
*/
private static function getAttributeValue(string $tableName, string $entityId, string $column): ?string
public static function getAttributeValue(string $tableName, string $entityId, string $column): ?string
{
$objectManager = ObjectManager::getInstance();
$resource = $objectManager->get(ResourceConnection::class);
Expand Down Expand Up @@ -213,12 +213,12 @@ public static function getValueFromAttribute(
}

/**
* @param array $options
* @param string $key
* @param array $options
*
* @return bool|null boolean value of the option named $key, or null when not set in $options
*/
public static function getValueOfOptionWhenSet(array $options, string $key): ?bool
public static function getValueOfOptionWhenSet(string $key, array $options): ?bool
{
if ($options[$key] || array_key_exists($key, $options)) {
return (bool)$options[$key];
Expand All @@ -232,8 +232,7 @@ public static function getValueOfOptionWhenSet(array $options, string $key): ?bo
*/
public function hasLargeFormat(): bool
Mark-Ernst marked this conversation as resolved.
Show resolved Hide resolved
{
return self::getValueOfOptionWhenSet($this->options, 'large_format')
?? self::$defaultOptions->getDefaultLargeFormat(self::LARGE_FORMAT);
return $this->optionIsEnabled(self::LARGE_FORMAT);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions Model/Sales/MagentoOrderCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Magento\Sales\Model\Order;
use Magento\Store\Model\ScopeInterface;
use MyParcelNL\Magento\Adapter\OrderLineOptionsFromOrderAdapter;
use MyParcelNL\Magento\Helper\CustomsDeclarationFromOrder;
use MyParcelNL\Magento\Helper\ShipmentOptions;
use MyParcelNL\Magento\Model\Source\DefaultOptions;
use MyParcelNL\Magento\Model\Source\ReturnInTheBox;
Expand Down Expand Up @@ -193,15 +194,12 @@ public function setFulfilment(): self
);
$myparcelDeliveryOptions = $magentoOrder['myparcel_delivery_options'] ?? '';
$deliveryOptions = json_decode($myparcelDeliveryOptions, true);
$deliveryOptions['date'] = $deliveryOptions['date'] ?? date('Y-m-d H:i:s');

if ($deliveryOptions && $deliveryOptions['isPickup']) {
$deliveryOptions['packageType'] = AbstractConsignment::PACKAGE_TYPE_PACKAGE_NAME;
}

$deliveryOptions['shipmentOptions'] = $shipmentOptionsHelper->getShipmentOptions();
$deliveryOptions['date'] = $deliveryOptions['date'] ?? date('Y-m-d H:i:s');

try {
// create new instance from known json
$deliveryOptionsAdapter = DeliveryOptionsAdapterFactory::create((array) $deliveryOptions);
Expand All @@ -212,7 +210,7 @@ public function setFulfilment(): self
$deliveryOptionsAdapter = DeliveryOptionsAdapterFactory::create($deliveryOptions);
}

$this->order = $magentoOrder;
$this->order = $magentoOrder;

$this->setBillingRecipient();
$this->setShippingRecipient($deliveryOptionsAdapter);
Expand Down Expand Up @@ -247,6 +245,8 @@ public function setFulfilment(): self
}

$order->setOrderLines($orderLines);
$customsDeclarationAdapter = new CustomsDeclarationFromOrder($this->order, $this->objectManager);
$order->setCustomsDeclaration($customsDeclarationAdapter->createCustomsDeclaration());
$orderCollection->push($order);
}

Expand Down Expand Up @@ -312,7 +312,7 @@ public function getBillingRecipient(): ?Recipient
*/
public function setShippingRecipient(AbstractDeliveryOptionsAdapter $deliveryOptions): self
{
$carrier = ConsignmentFactory::createByCarrierName($deliveryOptions->getCarrier());
$carrier = ConsignmentFactory::createByCarrierName('postnl');
Mark-Ernst marked this conversation as resolved.
Show resolved Hide resolved
$street = implode(
' ',
$this->order->getShippingAddress()
Expand Down
4 changes: 2 additions & 2 deletions Model/Sales/TrackTraceHolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ private function getAgeCheck(Track $magentoTrack, $address, array $options = [])
return false;
}

$ageCheckFromOptions = ShipmentOptions::getValueOfOptionWhenSet($options, 'age_check');
$ageCheckFromOptions = ShipmentOptions::getValueOfOptionWhenSet('age_check', $options);
$ageCheckOfProduct = ShipmentOptions::getAgeCheckFromProduct($magentoTrack);
$ageCheckFromSettings = self::$defaultOptions->getDefaultOptionsWithoutPrice('age_check');

Expand Down Expand Up @@ -445,7 +445,7 @@ private function calculateTotalWeight(Track $magentoTrack, int $totalWeight = 0)
*
* @return int
*/
private function getCentsByPrice(float $price): int
public static function getCentsByPrice(float $price): int
{
return (int) $price * 100;
}
Expand Down