<?php
/**
 * Cart product add after Observer
 *
 * @category  Magetop Extensions
 * @package   Magetop_Quotesystem
 * @author    Magetop Developer
 * @copyright Copyright (c) Magetop (https://www.magetop.com)
 * @license   https://www.magetop.com/license.html
 */

namespace Magetop\Quotesystem\Model\Quote\Address\Total;

use Magento\Quote\Model\Quote\Address;
use Magento\Quote\Model\Quote\Address\Item as AddressItem;
use Magento\Quote\Model\Quote\Item;

/**
 * Address total collector model
 */
class Subtotal extends \Magento\Quote\Model\Quote\Address\Total\Subtotal
{
    /**
     * @var \Magetop\Quotesystem\Helper\Data
     */
    protected $_helper;

    /**
     * Address item initialization
     *
     * @param Address $address
     * @param AddressItem|Item $item
     * @return bool
     */
    protected function _initItem($address, $item)
    {
        if ($item instanceof AddressItem) {
            $quoteItem = $item->getAddress()->getQuote()->getItemById($item->getQuoteItemId());
        } else {
            $quoteItem = $item;
        }
        $valid = false;
        if ($quoteItem) {
            $product = $quoteItem->getProduct();
            
            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $this->_helper = $objectManager->create(\Magetop\Quotesystem\Helper\Data::class);
            $helper = $this->_helper;
            /**
             * Quote super mode flag mean what we work with quote without restriction
             */
            if ($product && ($item->getQuote()->getIsSuperMode() || $product->isVisibleInCatalog())) {
                $product->setCustomerGroupId($quoteItem->getQuote()->getCustomerGroupId());
                $quoteItem->setConvertedPrice(null);
                $originalPrice = $product->getPrice();
                if ($quoteItem->getParentItem() && $quoteItem->isChildrenCalculated()) {
                    $quoteCollection = $helper->getMtQuoteModel()->getCollection()
                    ->addFieldToFilter("item_id", $item->getItemId());
                    if ($item instanceof AddressItem && $quoteCollection->getSize()) {
                        $finalPrice = $quoteItem->getPrice();
                    } else {
                        $finalPrice = $quoteItem->getParentItem()->getProduct()->getPriceModel()->getChildFinalPrice(
                            $quoteItem->getParentItem()->getProduct(),
                            $quoteItem->getParentItem()->getQty(),
                            $product,
                            $quoteItem->getQty()
                        );
                    }
                    $this->_calculateRowTotal($item, $finalPrice, $originalPrice);
                } elseif (!$quoteItem->getParentItem()) {
                    $quoteCollection = $helper->getMtQuoteModel()->getCollection()
                    ->addFieldToFilter("item_id", $item->getItemId());
                    if ($item instanceof AddressItem && $quoteCollection->getSize()) {
                        $finalPrice = $quoteItem->getPrice();
                    } else {
                        $finalPrice = $product->getFinalPrice($quoteItem->getQty());
                    }
                    $this->_calculateRowTotal($item, $finalPrice, $originalPrice);
                    $this->_addAmount($item->getRowTotal());
                    $this->_addBaseAmount($item->getBaseRowTotal());
                    $address->setTotalQty($address->getTotalQty() + $item->getQty());
                }
                $valid = true;
            }
        }
        return $valid;
    }
}
