123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- <?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;
- }
- }
|