Display custom column in Invoice grid using join
-
14-04-2021 - |
Question
I have custom moudule with custom table and tried to join it with invoice grid table to show it in invoice grid.But it is not working as it should.Here is what i have tried.
adminhtml/di.xml
<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
<arguments>
<argument name="collections" xsi:type="array">
<item name="sales_order_invoice_grid_data_source" xsi:type="string">Vendor\Module\Model\ResourceModel\Order\Grid\Collection</item>
</argument>
</arguments>
</type>
collection.php
<?php
namespace Vendor\Module\Model\ResourceModel\Order\Grid;
use Magento\Sales\Model\ResourceModel\Order\Grid\Collection as OriginalCollection;
/**
* Order grid extended collection
*/
class Collection extends OriginalCollection
{
protected function _renderFiltersBefore()
{
$myInvoiceTable = $this->getTable('mytable');
$this->getSelect()->joinLeft(
["myinv" => $myInvoiceTable],
'main_table.entity_id = my.invoice_id',
['increment_id_with_prefix']
)
;
parent::_renderFiltersBefore();
}
}
sales_order_invoice_grid.xml
<?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">
<columns name="sales_order_invoice_columns">
<column name="increment_id_with_prefix">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">text</item>
<item name="label" xsi:type="string" translate="true">Invoice Increment</item>
</item>
</argument>
</column>
</columns>
</listing>
Mytable Structure
Output invoice grid
It show some column missing data also not showing mytable column data.Also there is many invoice but it show only these 4 row only.
Solution
Here it is working fine.Don't know why was not working with adding my custom argument class Vendor\Module\Model\ResourceModel\Order\Grid\Collection
adminhtml/di.xml
<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
<plugin name="sales_invoice_additional_columns_for_gst" type="Vendor\Module\Plugin\SalesInvoiceGridCollection" sortOrder="10" disabled="false" />
</type>
SalesInvoiceGridCollection.php
<?php
namespace Vendor\Module\Plugin;
use Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory;
use Magento\Sales\Model\ResourceModel\Order\Invoice\Grid\Collection as SalesOrderInvoiceGridCollection;
class SalesInvoiceGridCollection
{
const SALES_ORDER_INVOICE_GRID_DATA_SOURCE = 'sales_order_invoice_grid_data_source';
private $collection;
public function __construct(SalesOrderInvoiceGridCollection $collection)
{
$this->collection = $collection;
}
public function aroundGetReport(CollectionFactory $subject, \Closure $proceed, $requestName)
{
$result = $proceed($requestName);
if (self::SALES_ORDER_INVOICE_GRID_DATA_SOURCE == $requestName) {
if ($result instanceof $this->collection) {
$select = $this->collection->getSelect();
$select->joinLeft(
["myinv" => 'mytable'],
'main_table.entity_id = myinv.invoice_id',
['increment_id_with_prefix']
);
return $this->collection;
}
}
return $result;
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange