Skip to content

Commit

Permalink
Merge branch 'develop' into support-multiple-msi-sources
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardPerdaan authored Feb 16, 2022
2 parents 198f10f + ff7ffd4 commit 8af8a4c
Show file tree
Hide file tree
Showing 14 changed files with 707 additions and 339 deletions.
2 changes: 1 addition & 1 deletion Api/ShippingMethodsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface ShippingMethodsInterface
/**
* @param mixed $deliveryOptions
*
* @return array[]
* @return mixed[] specifying array[] breaks the soap api
* @api
*/
public function getFromDeliveryOptions($deliveryOptions): array;
Expand Down
116 changes: 116 additions & 0 deletions Helper/CustomsDeclarationFromOrder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?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;
use MyParcelNL\Magento\Helper\Data;

class CustomsDeclarationFromOrder
{
private const CURRENCY_EURO = 'EUR';

/**
* @var mixed
*/
private $helper;

/**
* @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;
$this->helper = $this->objectManager->get(Data::class);
}

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

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

if (! $product) {
continue;
}

$totalWeight += $this->helper->getWeightTypeOfOption($product->getWeight() * $item->getQtyShipped());
$description = Str::limit($product->getName(), AbstractConsignment::DESCRIPTION_MAX_LENGTH);

$customsItem = (new MyParcelCustomsItem())
->setDescription($description)
->setAmount($item->getQtyShipped())
->setWeight($this->helper->getWeightTypeOfOption($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
*/
private function getCountryOfOrigin(Product $product): string
{
$productCountryOfOrigin = $this->objectManager
->get(ProductRepositoryInterface::class)
->getById($product->getId())
->getCountryOfManufacture();

return $productCountryOfOrigin ?? AbstractConsignment::CC_NL;
}

/**
* @param \Magento\Catalog\Model\Product $product
*
* @return int
*/
private function getHsCode(Product $product): int
{
return (int) ShipmentOptions::getAttributeValue(
'catalog_product_entity_int',
$product->getId(),
'classification'
);
}
}
Loading

0 comments on commit 8af8a4c

Please sign in to comment.