QuotingDataInitializer.php 7.67 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
<?php
/**
 * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
 */
namespace Temando\Shipping\Model\Checkout\RateRequest;

use Magento\Framework\Exception\LocalizedException;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Api\Data\AddressExtensionInterface;
use Magento\Quote\Api\Data\AddressExtensionInterfaceFactory;
use Magento\Quote\Model\Quote\Address\RateRequest;
use Temando\Shipping\Model\OrderInterfaceBuilder;
use Temando\Shipping\Model\ResourceModel\Repository\AddressRepositoryInterface;
use Temando\Shipping\Model\ResourceModel\Repository\CollectionPointSearchRepositoryInterface;
use Temando\Shipping\Model\ResourceModel\Repository\PickupLocationSearchRepositoryInterface;
use Temando\Shipping\Model\ResourceModel\Repository\QuoteCollectionPointRepositoryInterface;
use Temando\Shipping\Model\ResourceModel\Repository\QuotePickupLocationRepositoryInterface;

/**
 * Temando Quoting Data Initializer.
 *
 * @package Temando\Shipping\Model
 * @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 QuotingDataInitializer
{
    /**
     * @var Extractor
     */
    private $rateRequestExtractor;

    /**
     * @var AddressRepositoryInterface
     */
    private $checkoutAddressRepository;

    /**
     * @var AddressExtensionInterfaceFactory
     */
    private $addressExtensionFactory;

    /**
     * @var CollectionPointSearchRepositoryInterface
     */
    private $collectionPointSearchRepository;

    /**
     * @var QuoteCollectionPointRepositoryInterface
     */
    private $collectionPointRepository;

    /**
     * @var PickupLocationSearchRepositoryInterface
     */
    private $pickupLocationSearchRepository;

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

    /**
     * @var OrderInterfaceBuilder
     */
    private $orderBuilder;

    /**
     * QuotingDataInitializer constructor.
     * @param Extractor $rateRequestExtractor
     * @param AddressRepositoryInterface $checkoutAddressRepository
     * @param AddressExtensionInterfaceFactory $addressExtensionFactory
     * @param CollectionPointSearchRepositoryInterface $collectionPointSearchRepository
     * @param QuoteCollectionPointRepositoryInterface $collectionPointRepository
     * @param PickupLocationSearchRepositoryInterface $pickupLocationSearchRepository
     * @param QuotePickupLocationRepositoryInterface $pickupLocationRepository
     * @param OrderInterfaceBuilder $orderBuilder
     */
    public function __construct(
        Extractor $rateRequestExtractor,
        AddressRepositoryInterface $checkoutAddressRepository,
        AddressExtensionInterfaceFactory $addressExtensionFactory,
        CollectionPointSearchRepositoryInterface $collectionPointSearchRepository,
        QuoteCollectionPointRepositoryInterface $collectionPointRepository,
        PickupLocationSearchRepositoryInterface $pickupLocationSearchRepository,
        QuotePickupLocationRepositoryInterface $pickupLocationRepository,
        OrderInterfaceBuilder $orderBuilder
    ) {
        $this->rateRequestExtractor = $rateRequestExtractor;
        $this->checkoutAddressRepository = $checkoutAddressRepository;
        $this->addressExtensionFactory = $addressExtensionFactory;
        $this->collectionPointSearchRepository = $collectionPointSearchRepository;
        $this->collectionPointRepository = $collectionPointRepository;
        $this->pickupLocationSearchRepository = $pickupLocationSearchRepository;
        $this->pickupLocationRepository = $pickupLocationRepository;
        $this->orderBuilder = $orderBuilder;
    }

    /**
     * If consumer selected any value-added services during checkout, these
     * are added to the shipping address here. Shipping address is part of the
     * rate request, which gets added to the order builder separately.
     *
     * @param AddressInterface $address
     * @return bool
     */
    private function setCheckoutFields(AddressInterface $address)
    {
        try {
            $checkoutAddress = $this->checkoutAddressRepository->getByQuoteAddressId($address->getId());
            $checkoutFields = $checkoutAddress->getServiceSelection();
        } catch (LocalizedException $e) {
            $checkoutFields = [];
        }

        $addressExtension = $address->getExtensionAttributes();
        if ($addressExtension instanceof AddressExtensionInterface) {
            $addressExtension->setCheckoutFields($checkoutFields);
        } else {
            $addressExtension = $this->addressExtensionFactory->create(['data' => [
                'checkout_fields' => $checkoutFields,
            ]]);
        }
        $address->setExtensionAttributes($addressExtension);

        return !empty($checkoutFields);
    }

    /**
     * If consumer selected a collection point or triggered a search for
     * collection points during checkout, these entities will be added to the
     * order builder here.
     *
     * @param AddressInterface $address
     * @return bool
     */
    private function setCollectionPoint(AddressInterface $address)
    {
        try {
            $collectionPointSearchRequest = $this->collectionPointSearchRepository->get($address->getId());
            $this->orderBuilder->setCollectionPointSearchRequest($collectionPointSearchRequest);
        } catch (LocalizedException $exception) {
            $collectionPointSearchRequest = null;
        }

        try {
            $collectionPoint = $this->collectionPointRepository->getSelected($address->getId());
            $this->orderBuilder->setCollectionPoint($collectionPoint);
        } catch (LocalizedException $exception) {
            $collectionPoint = null;
        }

        return ($collectionPointSearchRequest || $collectionPoint);
    }

    /**
     * If consumer selected a pickup location or triggered a search for
     * pickup locations during checkout, these entities will be added to the
     * order builder here.
     *
     * @param AddressInterface $address
     * @return bool
     */
    private function setPickupLocation(AddressInterface $address)
    {
        try {
            $pickupLocationSearchRequest = $this->pickupLocationSearchRepository->get($address->getId());
            $this->orderBuilder->setPickupLocationSearchRequest($pickupLocationSearchRequest);
        } catch (LocalizedException $exception) {
            $pickupLocationSearchRequest = null;
        }

        try {
            $pickupLocation = $this->pickupLocationRepository->getSelected($address->getId());
            $this->orderBuilder->setPickupLocation($pickupLocation);
        } catch (LocalizedException $exception) {
            $pickupLocation = null;
        }

        return ($pickupLocationSearchRequest || $pickupLocation);
    }

    /**
     * Create a Temando order for quoting purposes.
     *
     * The order is being built from the quote and rate request.
     * The order may include
     * - dynamic checkout fields,
     * - delivery location selected during checkout.
     *
     * @param RateRequest $rateRequest
     * @return \Temando\Shipping\Model\OrderInterface
     * @throws LocalizedException
     */
    public function getOrder(RateRequest $rateRequest)
    {
        $shippingAddress = $this->rateRequestExtractor->getShippingAddress($rateRequest);

        $this->setCheckoutFields($shippingAddress);
        $this->setCollectionPoint($shippingAddress) || $this->setPickupLocation($shippingAddress);
        $this->orderBuilder->setRateRequest($rateRequest);

        /** @var \Temando\Shipping\Model\OrderInterface $order */
        $order = $this->orderBuilder->create();

        return $order;
    }
}