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.

NotifyOrderIsReadyAndShipInterface API;
Implemented NotifyPickup controller;
Added some TODOs
  • Loading branch information
novikor committed Mar 31, 2019
1 parent ab9e1ba commit 166410d
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function __construct(
*/
public function execute(int $orderId):bool
{
/*TODO: add $order->canShip() check */
return $this->isFulfilled->execute($orderId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryInStorePickup\Model;

use Magento\InventoryInStorePickupApi\Api\IsOrderReadyForPickupInterface;
use Magento\InventoryInStorePickupApi\Api\NotifyOrderIsReadyAndShipInterface;
use Magento\InventoryInStorePickupApi\Exception\OrderIsNotReadyForPickupException;

class NotifyOrderIsReadyAndShip implements NotifyOrderIsReadyAndShipInterface
{
/**
* @var \Magento\InventoryInStorePickupApi\Api\IsOrderReadyForPickupInterface
*/
private $isOrderReadyForPickup;

/**
* @var \Magento\Sales\Api\ShipOrderInterface
*/
private $shipOrder;

/**
* NotifyOrderIsReadyAndShip constructor.
*
* @param \Magento\InventoryInStorePickupApi\Api\IsOrderReadyForPickupInterface $isOrderReadyForPickup
* @param \Magento\Sales\Api\ShipOrderInterface $shipOrder
*/
public function __construct(
IsOrderReadyForPickupInterface $isOrderReadyForPickup,
\Magento\Sales\Api\ShipOrderInterface $shipOrder
) {
$this->isOrderReadyForPickup = $isOrderReadyForPickup;
$this->shipOrder = $shipOrder;
}

/**
* {@inheritdoc}
*/
public function execute(int $orderId):int
{
if (!$this->isOrderReadyForPickup->execute($orderId)) {
throw new OrderIsNotReadyForPickupException();
}

/* TODO: send email */

return $this->shipOrder->execute($orderId);
}
}
6 changes: 6 additions & 0 deletions app/code/Magento/InventoryInStorePickup/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@
</argument>
</arguments>
</type>

<preference for="\Magento\InventoryInStorePickupApi\Api\NotifyOrderIsReadyAndShipInterface"
type="\Magento\InventoryInStorePickup\Model\NotifyOrderIsReadyAndShip"/>
<preference for="\Magento\InventoryInStorePickupApi\Api\IsOrderReadyForPickupInterface"
type="\Magento\InventoryInStorePickup\Model\IsOrderReadyForPickup"/>

</config>
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ protected function _construct()
$this->_controller = 'adminhtml_order';
$this->_mode = 'view';

/* TODO: always display but throw warnings? */
if ($this->isDisplayButton()) {
$message = __('Are you sure you want to notify the customer that order is ready for pickup?');
$message = __(
'Are you sure you want to notify the customer that order is ready for pickup and create shipment?'
);
$this->addButton(
'ready_for_pickup',
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,18 @@

namespace Magento\InventoryInStorePickupAdminUi\Controller\Adminhtml\Order;

class NotifyPickup extends \Magento\Sales\Controller\Adminhtml\Order
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\InventoryInStorePickupApi\Api\NotifyOrderIsReadyAndShipInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Psr\Log\LoggerInterface;

/**
* Class NotifyPickup
*
* @package Magento\InventoryInStorePickupAdminUi\Controller\Adminhtml\Order
*/
class NotifyPickup extends \Magento\Backend\App\Action
{
/**
* Authorization level of a basic admin session
Expand All @@ -16,23 +27,59 @@ class NotifyPickup extends \Magento\Sales\Controller\Adminhtml\Order
*/
const ADMIN_RESOURCE = 'Magento_Sales::emails';

/**
* @var \Magento\InventoryInStorePickupApi\Api\NotifyOrderIsReadyAndShipInterface
*/
private $notifyOrderIsReadyAndShip;

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

/**
* @var \Psr\Log\LoggerInterface
*/
private $logger;

/**
* NotifyPickup constructor.
*
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\InventoryInStorePickupApi\Api\NotifyOrderIsReadyAndShipInterface $notifyOrderIsReadyAndShip
* @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
* @param \Psr\Log\LoggerInterface $logger
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
NotifyOrderIsReadyAndShipInterface $notifyOrderIsReadyAndShip,
OrderRepositoryInterface $orderRepository,
LoggerInterface $logger
) {
$this->notifyOrderIsReadyAndShip = $notifyOrderIsReadyAndShip;
$this->orderRepository = $orderRepository;
$this->logger = $logger;

parent::__construct($context);
}

/**
* Notify user
*
* @return \Magento\Framework\Controller\ResultInterface
*/
public function execute():\Magento\Framework\Controller\ResultInterface
{
/*TODO*/
/*$order = $this->_initOrder();
$order = $this->initOrder();

if ($order) {
try {
$this->orderManagement->notify($order->getEntityId());
$this->messageManager->addSuccessMessage(__('You sent the order email.'));
$this->notifyOrderIsReadyAndShip->execute((int)$order->getEntityId());
$this->messageManager->addSuccessMessage(__('The customer have been notified and shipment created.'));
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
} catch (\Exception $e) {
$this->messageManager->addErrorMessage(__('We can\'t send the email order right now.'));
$this->messageManager->addErrorMessage(__('We can\'t notify the customer right now.'));
$this->logger->critical($e);
}

Expand All @@ -42,8 +89,35 @@ public function execute():\Magento\Framework\Controller\ResultInterface
'order_id' => $order->getEntityId()
]
);
}*/
}

return $this->resultRedirectFactory->create()->setPath('sales/*/');
}

/**
* Initialize order model instance
*
* @see \Magento\Sales\Controller\Adminhtml\Order::_initOrder
*
* @return \Magento\Sales\Api\Data\OrderInterface|false
*/
private function initOrder()
{
$id = $this->getRequest()->getParam('order_id');
try {
$order = $this->orderRepository->get($id);
} catch (NoSuchEntityException $e) {
$this->messageManager->addErrorMessage(__('This order no longer exists.'));
$this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);

return false;
} catch (InputException $e) {
$this->messageManager->addErrorMessage(__('This order no longer exists.'));
$this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);

return false;
}

return $order;
}
}
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\InventoryInStorePickupApi\Api;

interface NotifyOrderIsReadyAndShipInterface
{
/**
* @param int $orderId
*
* @return int Id of created Shipment
*
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws \Magento\InventoryInStorePickupApi\Exception\OrderIsNotReadyForPickupException
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function execute(int $orderId):int;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryInStorePickupApi\Exception;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Phrase;

/**
* Class OrderIsNotReadyForPickupException
*
* @package Magento\InventoryInStorePickupApi\Exception
* @api
*/
class OrderIsNotReadyForPickupException extends LocalizedException
{
/**
* @param \Magento\Framework\Phrase $phrase
* @param \Exception $cause
* @param int $code
*/
public function __construct(Phrase $phrase = null, \Exception $cause = null, $code = 0)
{
if ($phrase === null) {
$phrase = new Phrase('The order is not ready for pickup');
}

parent::__construct($phrase, $cause, $code);
}
}
2 changes: 0 additions & 2 deletions app/code/Magento/InventoryInStorePickupApi/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,4 @@
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\InventoryInStorePickupApi\Api\GetNearbySourcesByPostcodeInterface"
type="Magento\InventoryInStorePickupApi\Model\GetNearbySourcesByPostcode"/>
<preference for="\Magento\InventoryInStorePickupApi\Api\IsOrderReadyForPickupInterface"
type="\Magento\InventoryInStorePickup\Model\IsOrderReadyForPickup"/>
</config>

0 comments on commit 166410d

Please sign in to comment.