AddShipmentViewToolbarButtonPlugin.php 2.91 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
<?php
/**
 * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
 */

namespace Temando\Shipping\Plugin\Shipping;

use Magento\Backend\Block\Widget\Container;
use Magento\Framework\UrlInterface;
use Magento\Framework\View\LayoutInterface;
use Magento\Shipping\Block\Adminhtml\View as ShipmentView;
use Temando\Shipping\Model\Config\ModuleConfigInterface;
use Temando\Shipping\Model\Shipment\ShipmentProviderInterface;

/**
 * AddShipmentViewToolbarButtonPlugin
 *
 * @package Temando\Shipping\Plugin
 * @author  Christoph Aßmann <christoph.assmann@netresearch.de>
 * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
 * @link    https://www.temando.com/
 */
class AddShipmentViewToolbarButtonPlugin
{
    /**
     * @var ModuleConfigInterface
     */
    private $config;

    /**
     * @var ShipmentProviderInterface
     */
    private $shipmentProvider;

    /**
     * @var UrlInterface
     */
    private $urlBuilder;

    /**
     * AddShipmentViewToolbarButtonPlugin constructor.
     *
     * @param ModuleConfigInterface $config
     * @param ShipmentProviderInterface $shipmentProvider
     * @param UrlInterface $urlBuilder
     */
    public function __construct(
        ModuleConfigInterface $config,
        ShipmentProviderInterface $shipmentProvider,
        UrlInterface $urlBuilder
    ) {
        $this->config = $config;
        $this->shipmentProvider = $shipmentProvider;
        $this->urlBuilder = $urlBuilder;
    }

    /**
     * @param ShipmentView|Container $block
     * @param LayoutInterface $layout
     *
     * @return null
     */
    public function beforeSetLayout(ShipmentView $block, LayoutInterface $layout)
    {
        // only display button if an API shipment was registered
        $shipment = $this->shipmentProvider->getShipment();
        if (!$shipment) {
            return null;
        }

        // only display button if rma is enabled for temando shipping
        if (!$this->config->isRmaEnabled()) {
            return null;
        }

        $salesShipment = $this->shipmentProvider->getSalesShipment();
        $shipmentId = $salesShipment->getEntityId();
        $extReturnShipmentId = $salesShipment->getExtensionAttributes()->getExtReturnShipmentId();

        // only display button if forward-fulfillment return shipment was booked
        if (!$extReturnShipmentId) {
            return null;
        }

        $viewUrl = $this->urlBuilder->getUrl(
            'temando/rma_shipment/view',
            ['shipment_id' => $shipmentId, 'ext_shipment_id' => $extReturnShipmentId]
        );

        $block->addButton(
            'view_return_shipment',
            [
                'label' => __('View Return Shipment'),
                'onclick' => sprintf("setLocation('%s')", $viewUrl)
            ],
            0,
            20
        );

        // original method's argument does not get changed.
        return null;
    }
}