<?php
/**
 * Mirasvit
 *
 * This source file is subject to the Mirasvit Software License, which is available at https://mirasvit.com/license/.
 * Do not edit or add to this file if you wish to upgrade the to newer versions in the future.
 * If you wish to customize this module for your needs.
 * Please refer to http://www.magentocommerce.com for more information.
 *
 * @category  Mirasvit
 * @package   mirasvit/module-rma
 * @version   2.1.0
 * @copyright Copyright (C) 2019 Mirasvit (https://mirasvit.com/)
 */


namespace Mirasvit\Rma\Service\Report\Type;

class ReasonCnt
{
    private $itemCollectionFactory;
    private $offlineitemCollectionFactory;

    public function __construct(
        \Mirasvit\Rma\Model\ResourceModel\Item\CollectionFactory $itemCollectionFactory,
        \Mirasvit\Rma\Model\ResourceModel\OfflineItem\CollectionFactory $offlineitemCollectionFactory
    ) {
        $this->itemCollectionFactory        = $itemCollectionFactory;
        $this->offlineitemCollectionFactory = $offlineitemCollectionFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function get($reasonId)
    {
        $itemCollection = $this->itemCollectionFactory->create();
        $itemCollection->getSelect()->
            where('main_table.reason_id = '.$reasonId)
            ->columns('count(item_id) as reason_cnt')
        ;
        $itemsCnt = $itemCollection->getFirstItem()->getData('reason_cnt');

        $offlineitemCollection = $this->offlineitemCollectionFactory->create();
        $offlineitemCollection->getSelect()->
            where('main_table.reason_id = '.$reasonId)
            ->columns('count(offline_item_id) as reason_cnt')
        ;
        $itemsCnt += $offlineitemCollection->getFirstItem()->getData('reason_cnt');

        return $itemsCnt;
    }
}