Skip to content

Commit

Permalink
Add "Ready for Pickup" button on the Order page in Admin Panel which …
Browse files Browse the repository at this point in the history
…notifies Customer that Order could be picked up #2034.

Added check if order is ready for pickup
  • Loading branch information
novikor committed Mar 28, 2019
1 parent 25faaf6 commit 2ccfeb7
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 9 deletions.
93 changes: 93 additions & 0 deletions InventoryInStorePickup/Model/Order/IsReadyForPickup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryInStorePickup\Model\Order;

use Magento\InventoryApi\Api\Data\SourceItemInterface;

class IsReadyForPickup
{
/**
* @var \Magento\Sales\Api\OrderRepositoryInterface
*/
private $orderRepository;

/**
* @var \Magento\InventoryApi\Api\SourceItemRepositoryInterface
*/
private $sourceItemRepository;

/**
* @var \Magento\Framework\Api\SearchCriteriaBuilderFactory
*/
private $searchCriteriaBuilderFactory;

/**
* IsReadyForPickup constructor.
*
* @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
* @param \Magento\InventoryApi\Api\SourceItemRepositoryInterface $sourceItemRepository
* @param \Magento\Framework\Api\SearchCriteriaBuilderFactory $searchCriteriaBuilder
*/
public function __construct(
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
\Magento\InventoryApi\Api\SourceItemRepositoryInterface $sourceItemRepository,
\Magento\Framework\Api\SearchCriteriaBuilderFactory $searchCriteriaBuilder
) {
$this->orderRepository = $orderRepository;
$this->sourceItemRepository = $sourceItemRepository;
$this->searchCriteriaBuilderFactory = $searchCriteriaBuilder;
}

/**
* @param int $orderId
*
* @return bool
*/
public function execute(int $orderId):bool
{
$order = $this->orderRepository->get($orderId);

if ($sourceCode = $order->getExtensionAttributes()->getPickupLocationCode()) {
foreach ($order->getItems() as $item) {
if (!$this->isItemFulfilled($item->getSku(), $sourceCode, (float)$item->getQtyOrdered())) {
return false;
}
}

return true;
}

return false;
}

/**
* @param string $sku
* @param string $sourceCode
* @param float $qtyOrdered
*
* @return bool
*/
private function isItemFulfilled(string $sku, string $sourceCode, float $qtyOrdered):bool
{
$searchCriteria = $this->searchCriteriaBuilderFactory
->create()
->addFilter(SourceItemInterface::SOURCE_CODE, $sourceCode)
->addFilter(SourceItemInterface::SKU, $sku)
->create();

$sourceItems = $this->sourceItemRepository->getList($searchCriteria);
if ($sourceItems->getTotalCount()) {
/** @var SourceItemInterface $sourceItem */
$sourceItem = current($sourceItems->getItems());

return $sourceItem->getQuantity() >= $qtyOrdered;
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* Get Pickup Location identifier by order identifier.
*/
class GetPickupLocationByOrderId
class GetPickupLocationCodeByOrderId
{
private const ORDER_ID = 'order_id';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ class SaveOrderPickupLocation
private $connection;

/**
* GetPickupLocationByOrderId constructor.
*
* @param \Magento\Framework\App\ResourceConnection $connection
*/
public function __construct(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Magento\InventoryInStorePickup\Plugin\Sales\Order;

use Magento\InventoryInStorePickup\Model\ResourceModel\OrderPickupLocation\GetPickupLocationByOrderId;
use Magento\InventoryInStorePickup\Model\ResourceModel\OrderPickupLocation\GetPickupLocationCodeByOrderId;
use Magento\Sales\Api\Data\OrderExtensionFactory;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
Expand All @@ -23,17 +23,17 @@ class GetPickupLocationForOrderPlugin
private $orderExtensionFactory;

/**
* @var GetPickupLocationByOrderId
* @var GetPickupLocationCodeByOrderId
*/
private $getPickupLocationByOrderId;

/**
* @param OrderExtensionFactory $orderExtensionFactory
* @param GetPickupLocationByOrderId $getPickupLocationByOrderId
* @param OrderExtensionFactory $orderExtensionFactory
* @param GetPickupLocationCodeByOrderId $getPickupLocationByOrderId
*/
public function __construct(
OrderExtensionFactory $orderExtensionFactory,
GetPickupLocationByOrderId $getPickupLocationByOrderId
GetPickupLocationCodeByOrderId $getPickupLocationByOrderId
) {
$this->orderExtensionFactory = $orderExtensionFactory;
$this->getPickupLocationByOrderId = $getPickupLocationByOrderId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,36 @@ class ReadyForPickup extends \Magento\Backend\Block\Widget\Form\Container
*/
protected $_blockGroup = 'Magento_Sales';

/**
* @var \Magento\Sales\Block\Adminhtml\Order\View
*/
private $viewBlock;

/**
* @var \Magento\InventoryInStorePickup\Model\Order\IsReadyForPickup
*/
private $isReadyForPickup;

/**
* ReadyForPickup constructor.
*
* @param \Magento\Backend\Block\Widget\Context $context
* @param \Magento\Sales\Block\Adminhtml\Order\View $viewBlock
* @param \Magento\InventoryInStorePickup\Model\Order\IsReadyForPickup $isReadyForPickup
* @param array $data
*/
public function __construct(
\Magento\Backend\Block\Widget\Context $context,
\Magento\Sales\Block\Adminhtml\Order\View $viewBlock,
\Magento\InventoryInStorePickup\Model\Order\IsReadyForPickup $isReadyForPickup,
array $data = []
) {
$this->viewBlock = $viewBlock;
$this->isReadyForPickup = $isReadyForPickup;

parent::__construct($context, $data);
}

/**
* Rendering Ready for Pickup button
*/
Expand All @@ -32,7 +62,7 @@ protected function _construct()
$this->_controller = 'adminhtml_order';
$this->_mode = 'view';

if ($this->isEmailsSendingAllowed()) {/* TODO: add is order ready check */
if ($this->isDisplayButton()) {
$message = __('Are you sure you want to notify the customer that order is ready for pickup?');
$this->addButton(
'ready_for_pickup',
Expand All @@ -56,4 +86,13 @@ private function isEmailsSendingAllowed():bool
{
return $this->_authorization->isAllowed(self::ADMIN_SALES_EMAIL_RESOURCE);
}

/**
* @return bool
*/
private function isDisplayButton():bool
{
return $this->isEmailsSendingAllowed()
&& $this->isReadyForPickup->execute((int)$this->viewBlock->getOrderId());
}
}

0 comments on commit 2ccfeb7

Please sign in to comment.