ShipmentProcessor.php 11.9 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
<?php
/**
 * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
 */

namespace Temando\Shipping\Sync;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\ShipmentCreationArgumentsExtensionInterfaceFactory;
use Magento\Sales\Api\Data\ShipmentCreationArgumentsInterfaceFactory;
use Magento\Sales\Api\Data\ShipmentItemCreationInterface;
use Magento\Sales\Api\Data\ShipmentItemCreationInterfaceFactory;
use Magento\Sales\Api\Data\ShipmentTrackCreationInterfaceFactory;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Api\ShipmentRepositoryInterface as SalesShipmentRepositoryInterface;
use Magento\Sales\Api\ShipOrderInterface;
use Temando\Shipping\Model\DocumentationInterface;
use Temando\Shipping\Model\ResourceModel\Order\OrderReference as OrderReferenceResource;
use Temando\Shipping\Model\ResourceModel\Repository\ShipmentReferenceRepositoryInterface;
use Temando\Shipping\Model\ResourceModel\Repository\ShipmentRepositoryInterface;
use Temando\Shipping\Model\Sales\Service\ShipmentService;
use Temando\Shipping\Model\Shipment\PackageInterface;
use Temando\Shipping\Model\Shipment\PackageItemInterface;
use Temando\Shipping\Model\Shipping\Carrier;
use Temando\Shipping\Model\StreamEventInterface;
use Temando\Shipping\Sync\Exception\EventException;
use Temando\Shipping\Sync\Exception\EventProcessorException;

/**
 * Temando Shipment Event Processor
 *
 * @package Temando\Shipping\Sync
 * @author  Christoph Aßmann <christoph.assmann@netresearch.de>
 * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
 * @link    https://www.temando.com/
 */
class ShipmentProcessor implements EntityProcessorInterface
{
    /**
     * @var OrderRepositoryInterface
     */
    private $salesOrderRepository;

    /**
     * @var SalesShipmentRepositoryInterface
     */
    private $salesShipmentRepository;

    /**
     * @var ShipmentReferenceRepositoryInterface
     */
    private $shipmentReferenceRepository;

    /**
     * @var ShipmentRepositoryInterface
     */
    private $shipmentRepository;

    /**
     * @var ShipOrderInterface
     */
    private $shipOrder;

    /**
     * @var OrderReferenceResource
     */
    private $orderReferenceResource;

    /**
     * @var ShipmentItemCreationInterfaceFactory
     */
    private $itemCreationFactory;

    /**
     * @var ShipmentTrackCreationInterfaceFactory
     */
    private $trackCreationFactory;

    /**
     * @var ShipmentService
     */
    private $shipmentService;

    /**
     * @var ShipmentCreationArgumentsInterfaceFactory
     */
    private $shipmentCreationArgumentsFactory;

    /**
     * @var ShipmentCreationArgumentsExtensionInterfaceFactory
     */
    private $shipmentCreationArgumentsExtensionFactory;

    /**
     * ShipmentEventProcessor constructor.
     * @param OrderRepositoryInterface $salesOrderRepository
     * @param SalesShipmentRepositoryInterface $salesShipmentRepository
     * @param ShipmentReferenceRepositoryInterface $shipmentReferenceRepository
     * @param ShipmentRepositoryInterface $shipmentRepository
     * @param ShipOrderInterface $shipOrder
     * @param OrderReferenceResource $orderReferenceResource
     * @param ShipmentItemCreationInterfaceFactory $itemCreationFactory
     * @param ShipmentTrackCreationInterfaceFactory $trackCreationFactory
     * @param ShipmentService $shipmentService
     * @param ShipmentCreationArgumentsInterfaceFactory $shipmentCreationArgumentsFactory
     * @param ShipmentCreationArgumentsExtensionInterfaceFactory $shipmentCreationArgumentsExtensionFactory
     */
    public function __construct(
        OrderRepositoryInterface $salesOrderRepository,
        SalesShipmentRepositoryInterface $salesShipmentRepository,
        ShipmentReferenceRepositoryInterface $shipmentReferenceRepository,
        ShipmentRepositoryInterface $shipmentRepository,
        ShipOrderInterface $shipOrder,
        OrderReferenceResource $orderReferenceResource,
        ShipmentItemCreationInterfaceFactory $itemCreationFactory,
        ShipmentTrackCreationInterfaceFactory $trackCreationFactory,
        ShipmentService $shipmentService,
        ShipmentCreationArgumentsInterfaceFactory $shipmentCreationArgumentsFactory,
        ShipmentCreationArgumentsExtensionInterfaceFactory $shipmentCreationArgumentsExtensionFactory
    ) {
        $this->salesOrderRepository = $salesOrderRepository;
        $this->salesShipmentRepository = $salesShipmentRepository;
        $this->shipmentReferenceRepository = $shipmentReferenceRepository;
        $this->shipmentRepository = $shipmentRepository;
        $this->shipOrder = $shipOrder;
        $this->orderReferenceResource = $orderReferenceResource;
        $this->itemCreationFactory = $itemCreationFactory;
        $this->trackCreationFactory = $trackCreationFactory;
        $this->shipmentService = $shipmentService;
        $this->shipmentCreationArgumentsFactory = $shipmentCreationArgumentsFactory;
        $this->shipmentCreationArgumentsExtensionFactory = $shipmentCreationArgumentsExtensionFactory;
    }

    /**
     * @param OrderInterface $salesOrder
     * @param string $sku
     * @return int|null
     */
    private function getOrderItemIdBySku(OrderInterface $salesOrder, $sku)
    {
        foreach ($salesOrder->getItems() as $item) {
            if ($item->getSku() === $sku) {
                return $item->getItemId();
            }
        }

        return null;
    }

    /**
     * Create new shipment
     *
     * @param string $extShipmentId
     * @return int Processed entity ID.
     * @throws EventException
     * @throws EventProcessorException
     */
    private function create(string $extShipmentId): int
    {
        try {
            // load external shipment
            $shipment = $this->shipmentRepository->getById($extShipmentId);
        } catch (LocalizedException $e) {
            throw EventProcessorException::processingFailed(
                StreamEventInterface::ENTITY_TYPE_SHIPMENT,
                $extShipmentId,
                $e
            );
        }

        // skip shipment event if no fulfillment with tracking number is available
        $fulfillment = $shipment->getFulfillment();
        if (!$fulfillment || !$fulfillment->getTrackingReference()) {
            throw EventException::operationSkipped(
                StreamEventInterface::ENTITY_TYPE_SHIPMENT,
                StreamEventInterface::EVENT_TYPE_CREATE,
                $extShipmentId,
                'No fulfillment information available.'
            );
        }

        // find local order id for external order
        $orderId = $this->orderReferenceResource->getOrderIdByExtOrderId($shipment->getOrderId());
        if (!$orderId) {
            throw EventProcessorException::processingFailed(
                StreamEventInterface::ENTITY_TYPE_SHIPMENT,
                $extShipmentId
            );
        }

        // items
        /** @var ShipmentItemCreationInterface[] $creationItems */
        $creationItems = [];
        $packages = $shipment->getPackages() ?: [];

        /** @var PackageItemInterface[] $fulfilledItems */
        $fulfilledItems = array_reduce($packages, function (array $items, PackageInterface $package) {
            $items = array_merge($items, $package->getItems());
            return $items;
        }, []);

        $salesOrder = $this->salesOrderRepository->get($orderId);

        foreach ($fulfilledItems as $fulfilledItem) {
            if (!isset($creationItems[$fulfilledItem->getSku()])) {
                // add new creation item
                $orderItemId = $this->getOrderItemIdBySku($salesOrder, $fulfilledItem->getSku());

                $creationItem = $this->itemCreationFactory->create();
                $creationItem->setQty($fulfilledItem->getQty());
                $creationItem->setOrderItemId($orderItemId);

                $creationItems[$fulfilledItem->getSku()] = $creationItem;
            } else {
                // increase qty of existing creation item
                $creationItem = $creationItems[$fulfilledItem->getSku()];
                $creationItem->setQty($fulfilledItem->getQty() + $creationItem->getQty());
            }
        }

        // tracking
        $tracking = $this->trackCreationFactory->create();
        $tracking->setCarrierCode(Carrier::CODE);
        $tracking->setTitle($fulfillment->getServiceName());
        $tracking->setTrackNumber($fulfillment->getTrackingReference());

        // shipping label
        /** @var DocumentationInterface $documentation */
        $documentation = current($shipment->getDocumentation());
        $labelUrl = empty($documentation) ? '' : $documentation->getUrl();

        $extensionAttributes = $this->shipmentCreationArgumentsExtensionFactory->create();
        $extensionAttributes->setExtLocationId($shipment->getOriginId());
        $extensionAttributes->setExtShipmentId($extShipmentId);
        $extensionAttributes->setExtTrackingReference($fulfillment->getTrackingReference());
        $extensionAttributes->setShippingLabel($labelUrl);

        $arguments = $this->shipmentCreationArgumentsFactory->create();
        $arguments->setExtensionAttributes($extensionAttributes);

        try {
            $shipmentId = $this->shipOrder->execute(
                $orderId,
                $creationItems, // items within partial shipments
                true, // Notify the customer (tracking email)
                false, // add comment
                null,
                [$tracking],
                [], // Package definition
                $arguments
            );
        } catch (LocalizedException $e) {
            throw EventProcessorException::processingFailed(
                StreamEventInterface::ENTITY_TYPE_SHIPMENT,
                $extShipmentId,
                $e
            );
        }

        return (int) $shipmentId;
    }

    /**
     * Update existing shipment, i.e. add tracking information, update status
     *
     * @param string $extShipmentId
     * @return int
     * @throws EventException
     * @throws EventProcessorException
     */
    private function modify(string $extShipmentId): int
    {
        try {
            $shipmentReference = $this->shipmentReferenceRepository->getByExtShipmentId($extShipmentId);
        } catch (NoSuchEntityException $e) {
            return $this->create($extShipmentId);
        }

        try {
            // read shipment from platform, trigger post-process operations
            $this->shipmentService->read($extShipmentId, (int) $shipmentReference->getShipmentId());
        } catch (NoSuchEntityException $exception) {
            // shipment does not exist at platform, skip
            throw EventException::operationSkipped(
                StreamEventInterface::ENTITY_TYPE_SHIPMENT,
                StreamEventInterface::EVENT_TYPE_MODIFY,
                $extShipmentId,
                $exception->getMessage()
            );
        } catch (LocalizedException $exception) {
            // processing local shipment failed, try again later
            throw EventProcessorException::processingFailed(
                StreamEventInterface::ENTITY_TYPE_SHIPMENT,
                $extShipmentId,
                $exception
            );
        }

        return (int) $shipmentReference->getShipmentId();
    }

    /**
     * @param string $operation
     * @param string $extShipmentId
     * @return int Processed entity ID.
     * @throws EventException
     * @throws EventProcessorException
     */
    public function execute(string $operation, string $extShipmentId): int
    {
        if ($operation == StreamEventInterface::EVENT_TYPE_MODIFY) {
            return $this->modify($extShipmentId);
        }

        if ($operation == StreamEventInterface::EVENT_TYPE_CREATE) {
            return $this->create($extShipmentId);
        }

        throw EventException::unknownOperation(
            StreamEventInterface::ENTITY_TYPE_SHIPMENT,
            $operation
        );
    }
}