1

i want to get all invoice collection?
Here below is my code but not working

class Grid extends \Magento\Backend\Block\Widget\Grid\Extended
{
    /**
     * @var \Magento\Framework\Module\Manager
     */
    protected $moduleManager;

    /**
     * @var \Vendor\Module\Model\shippingchargesFactory
     */
    protected $invoice_collection;

    /**
     * @var \Vendor\Module\Model\Status
     */
    protected $_status;

    /**
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Backend\Helper\Data $backendHelper
     * @param \Vendor\Module\Model\shippingchargesFactory $shippingchargesFactory
     * @param \Vendor\Module\Model\Status $status
     * @param \Magento\Framework\Module\Manager $moduleManager
     * @param array $data
     *
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
     */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Backend\Helper\Data $backendHelper,
        \Magento\Sales\Model\Order\InvoiceRepository $invoice_collection,
        \Magento\Framework\Module\Manager $moduleManager,
        array $data = []
    ) {

        error_reporting(E_ALL);
        $this->invoice_collection = $invoice_collection;
        $this->moduleManager = $moduleManager;
        parent::__construct($context, $backendHelper, $data);
    }


    /**
     * @return $this
     */
    protected function _prepareCollection()
    {
        $collection = $this->invoice_collection->create()->getList();
        $this->setCollection($collection);
        parent::_prepareCollection();
        return $this;
    }
}
1

There are two ways you can get Invoice collection.

Using Repository interface

Magento\Sales\Api\InvoiceRepositoryInterface::getList

Another way is

Magento\Sales\Model\ResourceModel\Order\Invoice\CollectionFactory
1
public function __construct(
    \Magento\Sales\Model\Order\InvoiceRepositoryFactory $invoiceRepositoryFactory,
    \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria        
) {

     $this->invoiceRepositoryFactory = $invoiceRepositoryFactory;
    $this->searchCriteria = $searchCriteria;
}


/**
 * @return $this
 */
public function execute()
{
    $this->searchCriteria->setFilterGroups();
    $invoiceRepo = $this->invoiceRepositoryFactory->create();
    $invoiceRepoCollection = $invoiceRepo->getList($this->searchCriteria);
    $items = $invoiceRepoCollection->getItems();
    print_r($invoiceRepoCollection->getSize());
    foreach ($items as $item) {
        print_r($item->getData());
    }
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.