<?php
/**
 * Mageplaza
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Mageplaza.com license that is
 * available through the world-wide-web at this URL:
 * https://www.mageplaza.com/LICENSE.txt
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this extension to newer
 * version in the future.
 *
 * @category    Mageplaza
 * @package     Mageplaza_EditOrder
 * @copyright   Copyright (c) Mageplaza (https://www.mageplaza.com/)
 * @license     https://www.mageplaza.com/LICENSE.txt
 */

namespace Mageplaza\EditOrder\Observer\Adminhtml;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Session\SessionManager;
use Mageplaza\EditOrder\Helper\Data as HelperData;

/**
 * Class ConvertToQuoteItem
 * @package Mageplaza\EditOrder\Observer\Adminhtml
 */
class ConvertToQuoteItem implements ObserverInterface
{
    /**
     * @var HelperData
     */
    protected $_helperData;

    /**
     * @var SessionManager
     */
    protected $session;

    /**
     * ConvertToQuoteItem constructor.
     *
     * @param HelperData $helperData
     * @param SessionManager $session
     */
    public function __construct(HelperData $helperData, SessionManager $session)
    {
        $this->_helperData = $helperData;
        $this->session = $session;
    }

    /**
     * @param Observer $observer
     */
    public function execute(Observer $observer)
    {
        $orderItem = $observer->getData('order_item');
        $quoteItem = $observer->getData('quote_item');

        if (!$this->_helperData->isEnabled($orderItem->getStoreId())
            || !$this->_helperData->isMpEdit()
            || !in_array($orderItem->getOrder()->getStatus(), $this->_helperData->getEditableOrderStatus())) {
            return;
        }

        $quoteItem->setData('mp_custom_can_exceed_stock_quantity', $orderItem->getQtyOrdered());
        $quoteItem->setMpCustomTaxPercent($orderItem->getMpCustomTaxPercent());
        $quoteItem->setMpCustomDiscountType($orderItem->getMpCustomDiscountType());
        $quoteItem->setMpCustomDiscountValue($orderItem->getMpCustomDiscountValue());
    }
}
