-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Distance based source selection algorithm
A basic google map implementation is provided
- Loading branch information
1 parent
b7bc879
commit 7400b71
Showing
35 changed files
with
1,974 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
app/code/Magento/InventoryShippingAdminUi/Model/GetInventoryRequestBuilder.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
...InventoryShippingAdminUi/Model/InventoryRequestBuilder/DefaultInventoryRequestBuilder.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
...nventoryShippingAdminUi/Model/InventoryRequestBuilder/DistanceInventoryRequestBuilder.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
app/code/Magento/InventoryShippingAdminUi/Model/InventoryRequestBuilderInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.