Ready.php 7.13 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
<?php
/**
 * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
 */

namespace Temando\Shipping\Controller\Adminhtml\Pickup;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Sales\Api\OrderRepositoryInterface;
use Temando\Shipping\Model\Pickup\Email\Sender\PickupSender;
use Temando\Shipping\Model\Pickup\PickupLoader;
use Temando\Shipping\Model\Pickup\PickupManagementFactory;
use Temando\Shipping\Model\PickupInterface;
use Temando\Shipping\Model\PickupInterfaceFactory;
use Temando\Shipping\Model\PickupProviderInterface;
use Temando\Shipping\Model\ResourceModel\Repository\OrderPickupLocationRepositoryInterface;
use Temando\Shipping\Model\ResourceModel\Repository\PickupRepositoryInterface;

/**
 * Mark Pickup Ready Action
 *
 * @package Temando\Shipping\Controller
 * @author  Sebastian Ertner <sebastian.ertner@netresearch.de>
 * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
 * @link    https://www.temando.com/
 */
class Ready extends Action
{
    const ADMIN_RESOURCE = 'Temando_Shipping::pickups';

    /**
     * @var PickupLoader
     */
    private $pickupLoader;

    /**
     * @var PickupManagementFactory
     */
    private $pickupManagementFactory;

    /**
     * @var PickupProviderInterface
     */
    private $pickupProvider;

    /**
     * @var OrderRepositoryInterface
     */
    private $orderRepository;

    /**
     * @var PickupRepositoryInterface
     */
    private $pickupRepository;

    /**
     * @var OrderPickupLocationRepositoryInterface
     */
    private $pickupLocationRepository;

    /**
     * @var PickupInterfaceFactory
     */
    private $pickupFactory;

    /**
     * @var PickupSender
     */
    private $pickupSender;

    /**
     * Ready constructor.
     * @param Context $context
     * @param PickupLoader $pickupLoader
     * @param PickupManagementFactory $pickupManagementFactory
     * @param PickupProviderInterface $pickupProvider
     * @param OrderRepositoryInterface $orderRepository
     * @param PickupRepositoryInterface $pickupRepository
     * @param OrderPickupLocationRepositoryInterface $pickupLocationRepository
     * @param PickupInterfaceFactory $pickupFactory
     * @param PickupSender $pickupSender
     */
    public function __construct(
        Context $context,
        PickupLoader $pickupLoader,
        PickupManagementFactory $pickupManagementFactory,
        PickupProviderInterface $pickupProvider,
        OrderRepositoryInterface $orderRepository,
        PickupRepositoryInterface $pickupRepository,
        OrderPickupLocationRepositoryInterface $pickupLocationRepository,
        PickupInterfaceFactory $pickupFactory,
        PickupSender $pickupSender
    ) {
        $this->pickupLoader = $pickupLoader;
        $this->pickupManagementFactory = $pickupManagementFactory;
        $this->pickupProvider = $pickupProvider;
        $this->orderRepository = $orderRepository;
        $this->pickupRepository = $pickupRepository;
        $this->pickupLocationRepository = $pickupLocationRepository;
        $this->pickupFactory = $pickupFactory;
        $this->pickupSender = $pickupSender;

        parent::__construct($context);
    }

    /**
     * Update pickup item quantities, pickup status, and send customer confirmation email.
     *
     * @return ResultInterface
     */
    public function execute()
    {
        // load pickup data
        $pickupData = $this->getRequest()->getParam('pickup');
        $requestedItems = $pickupData['items'];

        $orderId = $this->getRequest()->getParam('sales_order_id', 0);
        $pickupId = $this->getRequest()->getParam('pickup_id', '');

        $pickups = $this->pickupLoader->load($orderId, $pickupId);
        $this->pickupLoader->register($pickups, $orderId, $pickupId);

        /** @var \Temando\Shipping\Model\Pickup $pickup */
        $pickup = $this->pickupProvider->getPickup();
        /** @var \Magento\Sales\Model\Order $order */
        $order = $this->pickupProvider->getOrder();

        /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
        $resultRedirect->setUrl($this->_redirect->getRefererUrl());

        // perform sanity checks
        if (!$order->canShip()) {
            $this->messageManager->addErrorMessage('Order cannot be fulfilled.');
            return $resultRedirect;
        }

        $pickupManagement = $this->pickupManagementFactory->create([
            'pickups' => $pickups,
        ]);
        $requestedItems = $pickupManagement->getRequestedItems($requestedItems, $order->getAllVisibleItems());
        if (empty($requestedItems)) {
            $this->messageManager->addErrorMessage('No items available to fulfill.');
            return $resultRedirect;
        }

        try {
            $pickup->setData(PickupInterface::ITEMS, $requestedItems);
            $pickup->setData(PickupInterface::STATE, PickupInterface::STATE_READY);
            $this->pickupRepository->save($pickup);
            $this->messageManager->addSuccessMessage('Pickup is ready for collection.');
        } catch (CouldNotSaveException $e) {
            $this->messageManager->addErrorMessage('Failed to update pickup status.');
            return $resultRedirect;
        }

        try {
            // update order status
            $order->setIsInProcess(true);
            $this->orderRepository->save($order);
        } catch (\Exception $exception) {
            $this->messageManager->addErrorMessage('Failed to update order items.');
        }

        // send ready email
        try {
            $this->pickupSender->setPickupReady();
            $this->pickupSender->send($order);
            $this->messageManager->addSuccessMessage('Email confirmation was sent.');
        } catch (\Exception $exception) {
            $this->messageManager->addErrorMessage('Email confirmation could not be sent.');
        }

        $remainingItems = $pickupManagement->getOpenItems($order->getAllVisibleItems());
        if (empty($remainingItems)) {
            // done
            return $resultRedirect;
        }

        try {
            // create new pickup at platform
            $addressId = $order->getShippingAddress()->getId();
            $pickupLocation = $this->pickupLocationRepository->get($addressId);
            $newPickup = $this->pickupFactory->create(['data' => [
                PickupInterface::LOCATION_ID => $pickupLocation->getPickupLocationId(),
                PickupInterface::ORDER_ID => $pickup->getOrderId(),
                PickupInterface::ORDER_REFERENCE => $pickup->getOrderReference() ?: $order->getIncrementId(),
                PickupInterface::ITEMS => $remainingItems,
            ]]);
            $this->pickupRepository->save($newPickup);
        } catch (LocalizedException $exception) {
            $this->messageManager->addErrorMessage('Failed to create a new pickup fulfillment.');
        }

        return $resultRedirect;
    }
}