Skip to content

Commit

Permalink
Distance based source selection algorithm
Browse files Browse the repository at this point in the history
A basic google map implementation is provided
  • Loading branch information
phoenix128 committed Nov 2, 2018
1 parent b7bc879 commit 7400b71
Show file tree
Hide file tree
Showing 35 changed files with 1,974 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
use Magento\Backend\Model\View\Result\Page;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\InventorySalesApi\Model\StockByWebsiteIdResolverInterface;
use Magento\InventorySourceSelectionApi\Api\Data\ItemRequestInterfaceFactory;
use Magento\InventorySourceSelectionApi\Api\Data\InventoryRequestInterfaceFactory;
use Magento\InventoryShippingAdminUi\Model\GetInventoryRequestBuilder;
use Magento\InventorySourceSelectionApi\Api\SourceSelectionServiceInterface;
use Magento\InventorySourceSelectionApi\Api\GetDefaultSourceSelectionAlgorithmCodeInterface;
use Magento\InventoryApi\Api\SourceRepositoryInterface;
Expand All @@ -29,21 +27,6 @@ class ProcessAlgorithm extends Action
*/
const ADMIN_RESOURCE = 'Magento_InventoryApi::source';

/**
* @var StockByWebsiteIdResolverInterface
*/
private $stockByWebsiteIdResolver;

/**
* @var ItemRequestInterfaceFactory
*/
private $itemRequestFactory;

/**
* @var InventoryRequestInterfaceFactory
*/
private $inventoryRequestFactory;

/**
* @var SourceSelectionServiceInterface
*/
Expand All @@ -59,40 +42,40 @@ class ProcessAlgorithm extends Action
*/
private $sourceRepository;

/**
* @var GetInventoryRequestBuilder
*/
private $getInventoryRequestBuilder;

/**
* @var array
*/
private $sources = [];

/**
* @param Context $context
* @param StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver
* @param ItemRequestInterfaceFactory $itemRequestFactory
* @param InventoryRequestInterfaceFactory $inventoryRequestFactory
* @param SourceSelectionServiceInterface $sourceSelectionService
* @param GetDefaultSourceSelectionAlgorithmCodeInterface $getDefaultSourceSelectionAlgorithmCode
* @param SourceRepositoryInterface $sourceRepository
* @param GetInventoryRequestBuilder $getInventoryRequestBuilder
*/
public function __construct(
Context $context,
StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver,
ItemRequestInterfaceFactory $itemRequestFactory,
InventoryRequestInterfaceFactory $inventoryRequestFactory,
SourceSelectionServiceInterface $sourceSelectionService,
GetDefaultSourceSelectionAlgorithmCodeInterface $getDefaultSourceSelectionAlgorithmCode,
SourceRepositoryInterface $sourceRepository
SourceRepositoryInterface $sourceRepository,
GetInventoryRequestBuilder $getInventoryRequestBuilder
) {
parent::__construct($context);
$this->stockByWebsiteIdResolver = $stockByWebsiteIdResolver;
$this->itemRequestFactory = $itemRequestFactory;
$this->inventoryRequestFactory = $inventoryRequestFactory;
$this->sourceSelectionService = $sourceSelectionService;
$this->getDefaultSourceSelectionAlgorithmCode = $getDefaultSourceSelectionAlgorithmCode;
$this->sourceRepository = $sourceRepository;
$this->getInventoryRequestBuilder = $getInventoryRequestBuilder;
}

/**
* @inheritdoc
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function execute(): ResultInterface
{
Expand All @@ -106,21 +89,8 @@ public function execute(): ResultInterface
$defaultCode = $this->getDefaultSourceSelectionAlgorithmCode->execute();
$algorithmCode = !empty($postRequest['algorithmCode']) ? $postRequest['algorithmCode'] : $defaultCode;

//TODO: maybe need to add exception when websiteId empty
$websiteId = $postRequest['websiteId'] ?? 1;
$stockId = (int)$this->stockByWebsiteIdResolver->execute((int)$websiteId)->getStockId();

$result = $requestItems = [];
foreach ($requestData as $data) {
$requestItems[] = $this->itemRequestFactory->create([
'sku' => $data['sku'],
'qty' => $data['qty']
]);
}
$inventoryRequest = $this->inventoryRequestFactory->create([
'stockId' => $stockId,
'items' => $requestItems
]);
$inventoryRequestBuilder = $this->getInventoryRequestBuilder->execute($algorithmCode);
$inventoryRequest = $inventoryRequestBuilder->execute($request);

$sourceSelectionResult = $this->sourceSelectionService->execute($inventoryRequest, $algorithmCode);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryShippingAdminUi\Model;

use Magento\Framework\Exception\NoSuchEntityException;

/**
* Get an inventory request builder
*/
class GetInventoryRequestBuilder
{
/**
* @var array
*/
private $inventoryRequestBuilders;

/**
* GetInventoryRequestBuilder constructor.
*
* @param array $inventoryRequestBuilders
* @SuppressWarnings(PHPMD.LongVariable)
*/
public function __construct(
array $inventoryRequestBuilders = []
) {
$this->inventoryRequestBuilders = $inventoryRequestBuilders;

foreach ($this->inventoryRequestBuilders as $code => $builder) {
if (!($builder instanceof InventoryRequestBuilderInterface)) {
throw new \InvalidArgumentException(
'Distance provider ' . $code . ' must implement InventoryRequestBuilderInterface'
);
}
}
}

/**
* Get the inventory request builder for the source selection algorythm
*
* @param string $code
* @return \Magento\InventoryShippingAdminUi\Model\InventoryRequestBuilderInterface
* @throws NoSuchEntityException
*/
public function execute(string $code): InventoryRequestBuilderInterface
{
if (!isset($this->inventoryRequestBuilders[$code])) {
throw new NoSuchEntityException(__('Unknown inventory request builder %1', $code));
}

return $this->inventoryRequestBuilders[$code];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryShippingAdminUi\Model\InventoryRequestBuilder;

use Magento\Framework\App\RequestInterface;
use Magento\InventorySalesApi\Model\StockByWebsiteIdResolverInterface;
use Magento\InventoryShippingAdminUi\Model\InventoryRequestBuilderInterface;
use Magento\InventorySourceSelectionApi\Api\Data\InventoryRequestInterface;
use Magento\InventorySourceSelectionApi\Api\Data\InventoryRequestInterfaceFactory;
use Magento\InventorySourceSelectionApi\Api\Data\ItemRequestInterfaceFactory;

/**
* @inheritdoc
*/
class DefaultInventoryRequestBuilder implements InventoryRequestBuilderInterface
{
/**
* @var StockByWebsiteIdResolverInterface
*/
private $stockByWebsiteIdResolver;

/**
* @var ItemRequestInterfaceFactory
*/
private $itemRequestFactory;

/**
* @var InventoryRequestInterfaceFactory
*/
private $inventoryRequestFactory;

/**
* Priority constructor.
* @param StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver
* @param InventoryRequestInterfaceFactory $inventoryRequestFactory
* @param ItemRequestInterfaceFactory $itemRequestFactory
*/
public function __construct(
StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver,
InventoryRequestInterfaceFactory $inventoryRequestFactory,
ItemRequestInterfaceFactory $itemRequestFactory
) {
$this->stockByWebsiteIdResolver = $stockByWebsiteIdResolver;
$this->itemRequestFactory = $itemRequestFactory;
$this->inventoryRequestFactory = $inventoryRequestFactory;
}

/**
* @inheritdoc
*/
public function execute(RequestInterface $request): InventoryRequestInterface
{
$postRequest = $request->getPost()->toArray();
$requestData = $postRequest['requestData'];

//TODO: maybe need to add exception when websiteId empty
$websiteId = $postRequest['websiteId'] ?? 1;
$stockId = (int) $this->stockByWebsiteIdResolver->execute((int)$websiteId)->getStockId();

$requestItems = [];
foreach ($requestData as $data) {
$requestItems[] = $this->itemRequestFactory->create([
'sku' => $data['sku'],
'qty' => $data['qty']
]);
}
$inventoryRequest = $this->inventoryRequestFactory->create([
'stockId' => $stockId,
'items' => $requestItems
]);

return $inventoryRequest;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryShippingAdminUi\Model\InventoryRequestBuilder;

use Magento\Framework\App\RequestInterface;
use Magento\InventorySalesApi\Model\StockByWebsiteIdResolverInterface;
use Magento\InventoryShippingAdminUi\Model\InventoryRequestBuilderInterface;
use Magento\InventorySourceSelection\Model\GetAddressRequestFromOrder;
use Magento\InventorySourceSelectionApi\Api\Data\InventoryRequestExtensionInterfaceFactory;
use Magento\InventorySourceSelectionApi\Api\Data\InventoryRequestInterface;
use Magento\InventorySourceSelectionApi\Api\Data\InventoryRequestInterfaceFactory;
use Magento\InventorySourceSelectionApi\Api\Data\ItemRequestInterfaceFactory;
use Magento\Sales\Api\OrderItemRepositoryInterface;

/**
* @inheritdoc
*/
class DistanceInventoryRequestBuilder implements InventoryRequestBuilderInterface
{
/**
* @var StockByWebsiteIdResolverInterface
*/
private $stockByWebsiteIdResolver;

/**
* @var ItemRequestInterfaceFactory
*/
private $itemRequestFactory;

/**
* @var InventoryRequestInterfaceFactory
*/
private $inventoryRequestFactory;

/**
* @var GetAddressRequestFromOrder
*/
private $getAddressRequestFromOrder;

/**
* @var OrderItemRepositoryInterface
*/
private $orderItemRepository;

/**
* @var InventoryRequestExtensionInterfaceFactory
*/
private $inventoryRequestExtensionInterfaceFactory;

/**
* Priority constructor.
* @param StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver
* @param InventoryRequestInterfaceFactory $inventoryRequestFactory
* @param ItemRequestInterfaceFactory $itemRequestFactory
* @param OrderItemRepositoryInterface $orderItemRepository
* @param GetAddressRequestFromOrder $getAddressRequestFromOrder
* @param InventoryRequestExtensionInterfaceFactory $inventoryRequestExtensionInterfaceFactory
*/
public function __construct(
StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver,
InventoryRequestInterfaceFactory $inventoryRequestFactory,
ItemRequestInterfaceFactory $itemRequestFactory,
OrderItemRepositoryInterface $orderItemRepository,
GetAddressRequestFromOrder $getAddressRequestFromOrder,
InventoryRequestExtensionInterfaceFactory $inventoryRequestExtensionInterfaceFactory
) {
$this->stockByWebsiteIdResolver = $stockByWebsiteIdResolver;
$this->itemRequestFactory = $itemRequestFactory;
$this->inventoryRequestFactory = $inventoryRequestFactory;
$this->getAddressRequestFromOrder = $getAddressRequestFromOrder;
$this->orderItemRepository = $orderItemRepository;
$this->inventoryRequestExtensionInterfaceFactory = $inventoryRequestExtensionInterfaceFactory;
}

/**
* @inheritdoc
*/
public function execute(RequestInterface $request): InventoryRequestInterface
{
$postRequest = $request->getPost()->toArray();
$requestData = $postRequest['requestData'];

//TODO: maybe need to add exception when websiteId empty
$websiteId = $postRequest['websiteId'] ?? 1;
$stockId = (int) $this->stockByWebsiteIdResolver->execute((int)$websiteId)->getStockId();

$requestItems = [];
foreach ($requestData as $data) {
$requestItems[] = $this->itemRequestFactory->create([
'sku' => $data['sku'],
'qty' => $data['qty']
]);
}

$orderItemId = (int) $requestData[0]['orderItem'];
$orderItem = $this->orderItemRepository->get($orderItemId);

$address = $this->getAddressRequestFromOrder->execute((int) $orderItem->getOrderId());

$inventoryRequest = $this->inventoryRequestFactory->create([
'stockId' => $stockId,
'items' => $requestItems
]);

$extensionAttributes = $this->inventoryRequestExtensionInterfaceFactory->create();
$extensionAttributes->setDestinationAddress($address);
$inventoryRequest->setExtensionAttributes($extensionAttributes);

return $inventoryRequest;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryShippingAdminUi\Model;

use Magento\Framework\App\RequestInterface;
use Magento\InventorySourceSelectionApi\Api\Data\InventoryRequestInterface;

interface InventoryRequestBuilderInterface
{
/**
* Source selection results provider
*
* @param RequestInterface $request
* @return InventoryRequestInterface
*/
public function execute(RequestInterface $request): InventoryRequestInterface;
}
Loading

0 comments on commit 7400b71

Please sign in to comment.