123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- <?php
- /**
- * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
- */
- namespace Temando\Shipping\Model\ResourceModel\Order;
- use Magento\Framework\Exception\CouldNotSaveException;
- use Magento\Framework\Exception\NoSuchEntityException;
- use Temando\Shipping\Api\Data\Order\OrderReferenceInterface;
- use Temando\Shipping\Api\Data\Order\OrderReferenceInterfaceFactory;
- use Temando\Shipping\Model\OrderInterface;
- use Temando\Shipping\Model\ResourceModel\Repository\OrderRepositoryInterface;
- use Temando\Shipping\Rest\Adapter\OrderApiInterface;
- use Temando\Shipping\Rest\EntityMapper\OrderRequestTypeBuilder;
- use Temando\Shipping\Rest\EntityMapper\OrderResponseMapper;
- use Temando\Shipping\Rest\Exception\AdapterException;
- use Temando\Shipping\Rest\Request\OrderRequestFactory;
- use Temando\Shipping\Rest\Request\Type\OrderRequestTypeInterface;
- use Temando\Shipping\Webservice\Response\Type\OrderResponseType;
- /**
- * Temando Order Repository
- *
- * @package Temando\Shipping\Model
- * @author Christoph Aßmann <christoph.assmann@netresearch.de>
- * @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 OrderRepository implements OrderRepositoryInterface
- {
- /**
- * @var OrderApiInterface
- */
- private $apiAdapter;
- /**
- * @var OrderRequestFactory
- */
- private $requestFactory;
- /**
- * @var OrderRequestTypeBuilder
- */
- private $requestBuilder;
- /**
- * @var OrderResponseMapper
- */
- private $orderResponseMapper;
- /**
- * @var OrderReference
- */
- private $resource;
- /**
- * @var OrderReferenceInterfaceFactory
- */
- private $orderReferenceFactory;
- /**
- * OrderRepository constructor.
- * @param OrderApiInterface $apiAdapter
- * @param OrderRequestFactory $requestFactory
- * @param OrderRequestTypeBuilder $requestBuilder
- * @param OrderResponseMapper $orderResponseMapper
- * @param OrderReference $resource
- * @param OrderReferenceInterfaceFactory $orderReferenceFactory
- */
- public function __construct(
- OrderApiInterface $apiAdapter,
- OrderRequestFactory $requestFactory,
- OrderRequestTypeBuilder $requestBuilder,
- OrderResponseMapper $orderResponseMapper,
- OrderReference $resource,
- OrderReferenceInterfaceFactory $orderReferenceFactory
- ) {
- $this->apiAdapter = $apiAdapter;
- $this->requestFactory = $requestFactory;
- $this->requestBuilder = $requestBuilder;
- $this->orderResponseMapper = $orderResponseMapper;
- $this->resource = $resource;
- $this->orderReferenceFactory = $orderReferenceFactory;
- }
- /**
- * @param int $entityId
- * @return OrderReferenceInterface
- * @throws NoSuchEntityException
- */
- private function getReferenceById($entityId)
- {
- /** @var \Temando\Shipping\Model\Order\OrderReference $orderReference */
- $orderReference = $this->orderReferenceFactory->create();
- $this->resource->load($orderReference, $entityId);
- if (!$orderReference->getId()) {
- throw new NoSuchEntityException(__('Order with id "%1" does not exist.', $entityId));
- }
- return $orderReference;
- }
- /**
- * Create a regular order at the platform.
- *
- * @param OrderRequestTypeInterface $orderType
- * @return OrderResponseType
- * @throws CouldNotSaveException
- */
- private function create(OrderRequestTypeInterface $orderType)
- {
- $orderRequest = $this->requestFactory->create([
- 'order' => $orderType,
- 'action' => OrderApiInterface::ACTION_CREATE,
- ]);
- try {
- $createdOrder = $this->apiAdapter->createOrder($orderRequest);
- } catch (AdapterException $e) {
- throw new CouldNotSaveException(__('Unable to save order.'), $e);
- }
- return $this->orderResponseMapper->map($createdOrder);
- }
- /**
- * Create a regular order at the platform and allocate pickup fulfillments.
- *
- * @param OrderRequestTypeInterface $orderType
- * @return OrderResponseType
- * @throws CouldNotSaveException
- */
- private function allocatePickup(OrderRequestTypeInterface $orderType)
- {
- $orderRequest = $this->requestFactory->create([
- 'order' => $orderType,
- 'action' => OrderApiInterface::ACTION_ALLOCATE_PICKUP,
- ]);
- try {
- $createdOrder = $this->apiAdapter->createOrder($orderRequest);
- } catch (AdapterException $e) {
- throw new CouldNotSaveException(__('Unable to save order.'), $e);
- }
- return $this->orderResponseMapper->map($createdOrder);
- }
- /**
- * Create a regular order at the platform and allocate shipments.
- *
- * @param OrderRequestTypeInterface $orderType
- * @return OrderResponseType
- * @throws CouldNotSaveException
- */
- private function allocateShipment(OrderRequestTypeInterface $orderType)
- {
- $orderRequest = $this->requestFactory->create([
- 'order' => $orderType,
- 'action' => OrderApiInterface::ACTION_ALLOCATE_SHIPMENT,
- ]);
- try {
- $allocatedOrder = $this->apiAdapter->createOrder($orderRequest);
- } catch (AdapterException $e) {
- throw new CouldNotSaveException(__('Unable to allocate shipments.'), $e);
- }
- return $this->orderResponseMapper->map($allocatedOrder);
- }
- /**
- * Update a previously created order at the platform.
- *
- * @param OrderRequestTypeInterface $orderType
- * @return OrderResponseType
- * @throws CouldNotSaveException
- */
- private function update(OrderRequestTypeInterface $orderType)
- {
- $orderRequest = $this->requestFactory->create([
- 'order' => $orderType,
- 'action' => OrderApiInterface::ACTION_UPDATE,
- ]);
- try {
- $updatedOrder = $this->apiAdapter->updateOrder($orderRequest);
- } catch (AdapterException $e) {
- throw new CouldNotSaveException(__('Unable to save order.'), $e);
- }
- return $this->orderResponseMapper->map($updatedOrder);
- }
- /**
- * @param OrderInterface $order
- * @return OrderResponseType
- * @throws CouldNotSaveException
- */
- public function save(OrderInterface $order)
- {
- // build order request type
- $orderType = $this->requestBuilder->build($order);
- // may be replaced by config setting in the future.
- $isOrderAllocationEnabled = true;
- $isPaymentPending = ($order->getStatus() === OrderInterface::STATUS_AWAITING_PAYMENT);
- $isUpdate = $order->getOrderId() && $order->getSourceId();
- $pickup = $order->getPickupLocation();
- $isPickupOrder = !empty($pickup) && !empty($pickup->getPickupLocationId());
- if ($isUpdate) {
- $orderResponse = $this->update($orderType);
- } elseif ($isPickupOrder) {
- $orderResponse = $this->allocatePickup($orderType);
- } elseif ($isOrderAllocationEnabled && !$isPaymentPending) {
- $orderResponse = $this->allocateShipment($orderType);
- } else {
- $orderResponse = $this->create($orderType);
- }
- if ($order->getSourceId() && !$order->getOrderId()) {
- // persist order reference if
- // - local order entity exists
- // - remote order entity does not yet exist
- $orderReference = $this->orderReferenceFactory->create(['data' => [
- OrderReferenceInterface::EXT_ORDER_ID => $orderResponse->getOrderId(),
- OrderReferenceInterface::ORDER_ID => $order->getSourceId(),
- ]]);
- $this->saveReference($orderReference);
- }
- return $orderResponse;
- }
- /**
- * @param OrderReferenceInterface $orderReference
- * @return OrderReferenceInterface
- * @throws CouldNotSaveException
- */
- public function saveReference(OrderReferenceInterface $orderReference)
- {
- try {
- /** @var \Temando\Shipping\Model\Order\OrderReference $orderReference */
- $this->resource->save($orderReference);
- } catch (\Exception $exception) {
- throw new CouldNotSaveException(__('Unable to save order reference.'), $exception);
- }
- return $orderReference;
- }
- /**
- * @param string $orderId Temando Order ID
- * @return OrderReferenceInterface
- * @throws NoSuchEntityException
- */
- public function getReferenceByExtOrderId($orderId)
- {
- $entityId = $this->resource->getIdByExtOrderId($orderId);
- if (!$entityId) {
- throw new NoSuchEntityException(__('Order reference to "%1" does not exist.', $orderId));
- }
- return $this->getReferenceById($entityId);
- }
- /**
- * @param int $orderId
- * @return OrderReferenceInterface
- * @throws NoSuchEntityException
- */
- public function getReferenceByOrderId($orderId)
- {
- $entityId = $this->resource->getIdByOrderId($orderId);
- if (!$entityId) {
- $msg = 'Order reference for order "%1" does not exist.';
- throw new NoSuchEntityException(__($msg, $orderId));
- }
- return $this->getReferenceById($entityId);
- }
- }
|