0

This is my dataprovider class under UI\Component\Listing\DataProvider\

namespace Arb\ArbReports\Ui\Component\Listing\DataProviders\Arb\InventoryReport;

use Magento\Framework\App\Request\Http;

/**
 * Class InventoryReportsDataProvider
 */
 /**
  * @codeCoverageIgnore
  */
class InventoryReport extends \Magento\Ui\DataProvider\AbstractDataProvider
{

    /**
     * @var \Magento\Ui\DataProvider\AddFieldToCollectionInterface[]
     */
    protected $addFieldStrategies;

    /**
     * @var \Magento\Ui\DataProvider\AddFilterToCollectionInterface[]
     */
    protected $addFilterStrategies;

    /**
     * @var \Magento\Framework\App\Request\Http
     */
    protected $request;

    /**
     * @var DateTime
     */
    protected $_dateTime;

    /**
     * @var \Arb\ArbCardsListing\Helper\Data
     */
    private $helper;
    /**
     * @var \Magento\Framework\Api\Search\SearchCriteriaBuilder
     */
    private $searchCriteriaBuilder;

    public function __construct(
        \Magento\Framework\Api\Search\SearchCriteriaBuilder $searchCriteriaBuilder,
        $name,
        $primaryFieldName,
        $requestFieldName,
        Http $request,
        array $meta = [],
        array $data = [],
        \Arb\ArbReports\Helper\InventoryData $helper
    ) {
        $this->request = $request;
        $this->helper = $helper;
        $this->name = $name;
        $this->primaryFieldName = $primaryFieldName;
        $this->requestFieldName = $requestFieldName;
        $this->meta = $meta;
        parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
    }

    /**
     * Get data
     *
     * @return array
     */
    public function getData()
    {

        // Preparing filters data//

       // getting data from helper class // Needs to apply filters on this data 

$product_id   = isset($this->request->getParam('filters')['product_id']) ? $this->request->getParam('filters')['product_id'] : "";
        
$product_name   = isset($this->request->getParam('filters')['product_name']) ? $this->request->getParam('filters')['product_name'] : '';
        $sku   = isset($this->request->getParam('filters')['sku']) ? $this->request->getParam('filters')['sku'] : '';
        $qty   = isset($this->request->getParam('filters')['qty']) ? $this->request->getParam('filters')['qty'] : '';
        

$salesData = $this->helper->getStockData($product_id, $product_name, $sku, $qty);
        $pagesize = intval($this->request->getParam('paging')['pageSize']);
        $pageCurrent = intval($this->request->getParam('paging')['current']);
        $pageoffset = ($pageCurrent - 1)*$pagesize;

        return [
                'totalRecords' => count($salesData),
                'items'=>array_slice($salesData, $pageoffset, $pageoffset+$pagesize),
        ];
    }

    /**
     * get sales data from Marketplace
     * @return array $salesData
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     * @SuppressWarnings(PHPMD.NPathComplexity)
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */

    // ###########################################
    public function setCurrentPage($offset, $size)
    {
    }
    public function setPageSize($offset, $size)
    {
    }

    public function setLimit($offset, $size)
    {
    }
    public function addOrder($field, $direction)
    {
    }

    public function addFilter(\Magento\Framework\Api\Filter $filter)
    {
     }
}

This is my ui_component file

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <argument name="data" xsi:type="array">
        <item name="js_config" xsi:type="array">
            <item name="provider" xsi:type="string">arb_reports_inventoryreport.arb_reports_inventoryreport_data_source</item>
        </item>
    </argument>

    <settings>
        <spinner>arb_reports_inventoryreport_columns</spinner>
        <deps>
            <dep>arb_reports_inventoryreport.arb_reports_inventoryreport_data_source</dep>
        </deps>
    </settings>

    <dataSource name="arb_reports_inventoryreport_data_source">

        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string">Arb\ArbReports\Ui\Component\Listing\DataProviders\Arb\InventoryReport\InventoryReport</argument>
            <argument name="name" xsi:type="string">arb_reports_inventoryreport_data_source</argument>
            <argument name="primaryFieldName" xsi:type="string">order_id</argument>
            <argument name="requestFieldName" xsi:type="string">id</argument>
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/provider</item>
                    <item name="update_url" xsi:type="url" path="mui/index/render"/>
                    <item name="storageConfig" xsi:type="array">
                        <item name="indexField" xsi:type="string">product_id</item>
                    </item>
                </item>
            </argument>
        </argument>


        <argument name="data" xsi:type="array">
            <item name="js_config" xsi:type="array">
                <item name="component" xsi:type="string">Magento_Ui/js/grid/provider</item>
            </item>
        </argument>
    </dataSource>


    <container name="listing_top">


        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="template" xsi:type="string">ui/grid/toolbar</item>
            </item>
        </argument>
        <!--<bookmark name="bookmarks" />-->
        <columnsControls name="columns_controls"  />
        <filters name="listing_filters" displayArea="dataGridFilters">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="params" xsi:type="array">
                        <item name="filters_modifier" xsi:type="array"/>
                    </item>
                    <item name="observers" xsi:type="array">
                        <item name="filters" xsi:type="object">Magento\Catalog\Ui\Component\Listing\Filters</item>
                    </item>
                </item>
            </argument>
            <settings>
                <statefull>
                    <property name="applied" xsi:type="boolean">false</property>
                </statefull>
            </settings>
            <filterSelect name="status" provider="${ $.parentName }">
                <settings>
                    <options class="Magento\Catalog\Model\Product\Attribute\Source\Status"/>
                    <caption translate="true">Select...</caption>
                    <label translate="true">Status</label>
                    <dataScope>status</dataScope>
                    <imports>
                        <link name="visible">componentType = column, index = ${ $.index }:visible</link>
                    </imports>
                </settings>
            </filterSelect>
            <filterInput name="product_id" provider="${ $.parentName }">
            <settings>
            <label translate="true">Product Id</label>
            <dataScope>product_id</dataScope>
                <imports>
                    <link name="visible">componentType = column, index = ${ $.index }:visible</link>
                </imports>
            </settings>
            </filterInput>
        </filters>
   <paging name="listing_paging">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="selectProvider" xsi:type="string">arb_reports_inventoryreport.arb_reports_inventoryreport.arb_reports_inventoryreport_columns.ids</item>
                    <item name="storageConfig" xsi:type="array">
                        <item name="provider" xsi:type="string">arb_reports_inventoryreport.arb_reports_inventoryreport.listing_top.bookmarks</item>
                        <item name="namespace" xsi:type="string">current.paging</item>
                    </item>
                </item>
            </argument>
        </paging>
 <exportButton name="export_button" class="Arb\ArbReports\Component\ExportButton">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="options_custom" xsi:type="array">
                        <item name="cvs" xsi:type="array">
                            <item name="value" xsi:type="string">csv</item>
                            <item name="label" xsi:type="string" translate="true">CSV</item>
                            <!--<item name="url" xsi:type="string">vendor_module/product/gridtocsv</item>-->
                        </item>
                    </item>
                </item>
            </argument>
        </exportButton>
 </container>
    <columns name="arb_reports_inventoryreport_columns">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="storageConfig" xsi:type="array">
                    <item name="provider" xsi:type="string">arb_reports_inventoryreport.arb_reports_inventoryreport.listing_top.bookmarks</item>
                    <item name="namespace" xsi:type="string">current</item>
                </item>
                <item name="childDefaults" xsi:type="array">
                    <item name="fieldAction" xsi:type="array">
                        <item name="provider" xsi:type="string">arb_reports_inventoryreport.arb_reports_inventoryreport.arb_reports_inventoryreport_columns.actions</item>
                        <item name="target" xsi:type="string">applyAction</item>
                        <item name="params" xsi:type="array">
                            <item name="0" xsi:type="string">edit</item>
                            <item name="1" xsi:type="string">${ $.$data.rowIndex }</item>
                        </item>
                    </item>
                    <item name="controlVisibility" xsi:type="boolean">true</item>
                    <item name="appendTo" xsi:type="string">arb_reports_inventoryreport.arb_reports_inventoryreport.listing_top.columns_controls</item>
                    <item name="storageConfig" xsi:type="array">
                        <item name="provider" xsi:type="string">arb_reports_inventoryreport.arb_reports_inventoryreport.listing_top.bookmarks</item>
                        <item name="root" xsi:type="string">columns.${ $.index }</item>
                        <item name="namespace" xsi:type="string">current.${ $.storageConfig.root}</item>
                    </item>
                </item>
            </item>
        </argument>
        <selectionsColumn name="ids">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="indexField" xsi:type="string">order_id</item>
                    <item name="sortOrder" xsi:type="number">0</item>
                </item>
            </argument>
        </selectionsColumn>
<column name="merchant_contact_name">
            <argument name="data" xsi:type="array">
                <item name="js_config" xsi:type="array">
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/column</item>
                </item>
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="align" xsi:type="string">left</item>
                    <item name="label" xsi:type="string" translate="true">Merchant Contact Name</item>
                    <item name="sortOrder" xsi:type="number">10</item>
                </item>
            </argument>
        </column>

        <column name="product_status">
            <argument name="data" xsi:type="array">
                <item name="js_config" xsi:type="array">
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/column</item>
                </item>
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="align" xsi:type="string">left</item>
                    <item name="label" xsi:type="string" translate="true">Product Status</item>
                    <item name="sortOrder" xsi:type="number">10</item>
                </item>
            </argument>
        </column>

        <column name="product_salable_quantity">
            <argument name="data" xsi:type="array">
                <item name="js_config" xsi:type="array">
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/column</item>
                </item>
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="align" xsi:type="string">left</item>
                    <item name="label" xsi:type="string" translate="true">Salable Quantity</item>
                    <item name="sortOrder" xsi:type="number">10</item>
                </item>
            </argument>
        </column>
        <column name="product_quantity">
            <argument name="data" xsi:type="array">
                <item name="js_config" xsi:type="array">
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/column</item>
                </item>
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="align" xsi:type="string">left</item>
                    <item name="label" xsi:type="string" translate="true">Quantity</item>
                    <item name="sortOrder" xsi:type="number">10</item>
                </item>
            </argument>
        </column>
 <column name="product_price">
            <argument name="data" xsi:type="array">
                <item name="js_config" xsi:type="array">
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/column</item>
                </item>
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="align" xsi:type="string">left</item>
                    <item name="label" xsi:type="string" translate="true">Product Price</item>
                    <item name="sortOrder" xsi:type="number">10</item>
                </item>
            </argument>
        </column>
        <column name="product_name">
            <argument name="data" xsi:type="array">
                <item name="js_config" xsi:type="array">
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/column</item>
                </item>
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="align" xsi:type="string">left</item>
                    <item name="label" xsi:type="string" translate="true">Product Name</item>
                    <item name="sortOrder" xsi:type="number">10</item>
                </item>
            </argument>
        </column>
        <column name="product_sku">
            <argument name="data" xsi:type="array">
                <item name="js_config" xsi:type="array">
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/column</item>
                </item>
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="align" xsi:type="string">left</item>
                    <item name="label" xsi:type="string" translate="true">SKU</item>
                    <item name="sortOrder" xsi:type="number">10</item>
                </item>
            </argument>
        </column>

        <column name="product_id">
            <argument name="data" xsi:type="array">
                <item name="js_config" xsi:type="array">
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/column</item>
                </item>
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="align" xsi:type="string">left</item>
                    <item name="label" xsi:type="string" translate="true">Product ID</item>
                    <item name="sortOrder" xsi:type="number">10</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

This is a helper class to create data for the data provider class

  <?php
 

namespace Arb\ArbReports\Helper;

use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\App\Helper\Context;
use Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku;

/**
 * ArbStockInventoryReport InventoryData helper
 *
 * @api
 *
 * @SuppressWarnings(PHPMD.TooManyFields)
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 * @since 100.0.2
 */
/**
 * @codeCoverageIgnore
 */
class InventoryData extends \Magento\Framework\App\Helper\AbstractHelper
{

    /**
     * @var Context
     */
    protected $context;

    /**
     * @var DateTime
     */
    protected $_dateTime;

    /**
     * @var CustomerRepositoryInterface
     */
    private $_customerRepository;

    /**
     * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
     */
    private $timezone;

    /**
     * @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
     */
    private $productCollection;
    /**
     * @var \Magento\CatalogInventory\Model\Stock\StockItemRepository
     */
    private $stockItemRepository;
    /**
     * @var \Magento\CatalogInventory\Helper\Stock
     */
    private $stockFilter;
    /**
     * @var \Magento\CatalogInventory\Api\StockRegistryInterface
     */
    private $stockRegistry;
    /**
     * @var GetSalableQuantityDataBySku
     */
    private $getSalableQuantityDataBySku;

    /**
     * InventoryData constructor.
     * @param Context $context
     * @param DateTime $_dateTime
     * @param OrderFactory $_orderFactory
     * @param SalesListCollection $_salesListCollection
     * @param CustomerRepositoryInterface $_customerRepository
     * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone
     */
    public function __construct(
        Context $context,
        CustomerRepositoryInterface $_customerRepository,
        \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollection,
        \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
        GetSalableQuantityDataBySku $getSalableQuantityDataBySku
    ) {
        $this->context = $context;
        $this->_customerRepository = $_customerRepository;
        parent::__construct($context);
        $this->timezone = $timezone;
        $this->productCollection = $productCollection;
        $this->stockRegistry = $stockRegistry;
        $this->getSalableQuantityDataBySku = $getSalableQuantityDataBySku;
    }

    /**
     * return array
     */
    public function getStockData($product_id=null, $product_name=null, $sku=null, $qty=null, $status=null)
    {
        $collection = $this->productCollection->create();
        $collection->addAttributeToSelect('*');
        //$collection->addAttributeToFilter('status', \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
        // $collection ->addFieldToFilter("sku", 'FRiENDi100');
        // ADD THIS CODE IF YOU WANT IN-STOCK-PRODUCT
        //$this->stockFilter->addInStockFilterToCollection($collection);
        if (!empty($product_id)) {
            $collection->addFieldToFilter('entity_id', $product_id);
        }
        if (!empty($product_name)) {
            $collection->addAttributeToFilter('name', [
                ['like' => '%' . $product_name . '%']
            ]);
        }
        if (!empty($sku)) {
            $collection->addFieldToFilter('sku', $sku);
        }

        if (!empty($qty)) {
            $collection->joinField(
                'qty',
                'cataloginventory_stock_item',
                'qty',
                'product_id=entity_id',
                '{{table}}.stock_id=1',
                'left'
            );
            $collection->addAttributeToFilter('qty', ['lteq'=>$qty]);
        }

        if (!empty($status)) {
            $collection->addFieldToFilter('status', $status);
        }

        $productsData  =[];
        //$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

        foreach ($collection as $product) {
            $productStock = $this->stockRegistry->getStockItem($product->getId());

            // Get quantity of product.
            $productQty = $productStock->getQty();
            $salable = $this->getSalableQuantityDataBySku->execute($product->getSku());

            $family = "N/A";

            $productsData[] = [

                        "product_id"                =>  $product->getId(),
                        "product_name"              =>  $product->getName(),
                        "product_sku"               =>  $product->getSku(),
                        "product_price"             =>  $product->getPriceInfo()->getPrice('final_price')->getAmount()->getBaseAmount(),
                        "product_quantity"          =>  $productQty,
                        "product_salable_quantity"  =>  $salable[0]['qty'],
                        "product_status"            => ($product->getStatus()==1) ? "Enabled" : "Disabled",
                        "merchant_contact_name"     =>  $product->getUserid()
                     ];
        }
        return $productsData;
    }
}

currently, I am getting filter values from request()->getParams() method, but I need other solution to apply filters in my custom data provider class. Please advise me on this question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.