123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Sales\Block\Adminhtml\Order\Create\Search;
- /**
- * Adminhtml sales order create search products block
- *
- * @api
- * @author Magento Core Team <core@magentocommerce.com>
- * @since 100.0.2
- */
- class Grid extends \Magento\Backend\Block\Widget\Grid\Extended
- {
- /**
- * Sales config
- *
- * @var \Magento\Sales\Model\Config
- */
- protected $_salesConfig;
- /**
- * Session quote
- *
- * @var \Magento\Backend\Model\Session\Quote
- */
- protected $_sessionQuote;
- /**
- * Catalog config
- *
- * @var \Magento\Catalog\Model\Config
- */
- protected $_catalogConfig;
- /**
- * Product factory
- *
- * @var \Magento\Catalog\Model\ProductFactory
- */
- protected $_productFactory;
- /**
- * @param \Magento\Backend\Block\Template\Context $context
- * @param \Magento\Backend\Helper\Data $backendHelper
- * @param \Magento\Catalog\Model\ProductFactory $productFactory
- * @param \Magento\Catalog\Model\Config $catalogConfig
- * @param \Magento\Backend\Model\Session\Quote $sessionQuote
- * @param \Magento\Sales\Model\Config $salesConfig
- * @param array $data
- */
- public function __construct(
- \Magento\Backend\Block\Template\Context $context,
- \Magento\Backend\Helper\Data $backendHelper,
- \Magento\Catalog\Model\ProductFactory $productFactory,
- \Magento\Catalog\Model\Config $catalogConfig,
- \Magento\Backend\Model\Session\Quote $sessionQuote,
- \Magento\Sales\Model\Config $salesConfig,
- array $data = []
- ) {
- $this->_productFactory = $productFactory;
- $this->_catalogConfig = $catalogConfig;
- $this->_sessionQuote = $sessionQuote;
- $this->_salesConfig = $salesConfig;
- parent::__construct($context, $backendHelper, $data);
- }
- /**
- * Constructor
- *
- * @return void
- */
- protected function _construct()
- {
- parent::_construct();
- $this->setId('sales_order_create_search_grid');
- $this->setRowClickCallback('order.productGridRowClick.bind(order)');
- $this->setCheckboxCheckCallback('order.productGridCheckboxCheck.bind(order)');
- $this->setRowInitCallback('order.productGridRowInit.bind(order)');
- $this->setDefaultSort('entity_id');
- $this->setUseAjax(true);
- if ($this->getRequest()->getParam('collapse')) {
- $this->setIsCollapsed(true);
- }
- }
- /**
- * Retrieve quote store object
- *
- * @return \Magento\Store\Model\Store
- */
- public function getStore()
- {
- return $this->_sessionQuote->getStore();
- }
- /**
- * Retrieve quote object
- *
- * @return \Magento\Quote\Model\Quote
- */
- public function getQuote()
- {
- return $this->_sessionQuote->getQuote();
- }
- /**
- * Add column filter to collection
- *
- * @param \Magento\Backend\Block\Widget\Grid\Column $column
- * @return $this
- */
- protected function _addColumnFilterToCollection($column)
- {
- // Set custom filter for in product flag
- if ($column->getId() == 'in_products') {
- $productIds = $this->_getSelectedProducts();
- if (empty($productIds)) {
- $productIds = 0;
- }
- if ($column->getFilter()->getValue()) {
- $this->getCollection()->addFieldToFilter('entity_id', ['in' => $productIds]);
- } else {
- if ($productIds) {
- $this->getCollection()->addFieldToFilter('entity_id', ['nin' => $productIds]);
- }
- }
- } else {
- parent::_addColumnFilterToCollection($column);
- }
- return $this;
- }
- /**
- * Prepare collection to be displayed in the grid
- *
- * @return $this
- */
- protected function _prepareCollection()
- {
- $attributes = $this->_catalogConfig->getProductAttributes();
- /* @var $collection \Magento\Catalog\Model\ResourceModel\Product\Collection */
- $collection = $this->_productFactory->create()->getCollection();
- $collection->setStore(
- $this->getStore()
- )->addAttributeToSelect(
- $attributes
- )->addAttributeToSelect(
- 'sku'
- )->addStoreFilter()->addAttributeToFilter(
- 'type_id',
- $this->_salesConfig->getAvailableProductTypes()
- )->addAttributeToSelect(
- 'gift_message_available'
- );
- $this->setCollection($collection);
- return parent::_prepareCollection();
- }
- /**
- * Prepare columns
- *
- * @return $this
- */
- protected function _prepareColumns()
- {
- $this->addColumn(
- 'entity_id',
- [
- 'header' => __('ID'),
- 'sortable' => true,
- 'header_css_class' => 'col-id',
- 'column_css_class' => 'col-id',
- 'index' => 'entity_id'
- ]
- );
- $this->addColumn(
- 'name',
- [
- 'header' => __('Product'),
- 'renderer' => \Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid\Renderer\Product::class,
- 'index' => 'name'
- ]
- );
- $this->addColumn('sku', ['header' => __('SKU'), 'index' => 'sku']);
- $this->addColumn(
- 'price',
- [
- 'header' => __('Price'),
- 'column_css_class' => 'price',
- 'type' => 'currency',
- 'currency_code' => $this->getStore()->getCurrentCurrencyCode(),
- 'rate' => $this->getStore()->getBaseCurrency()->getRate($this->getStore()->getCurrentCurrencyCode()),
- 'index' => 'price',
- 'renderer' => \Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid\Renderer\Price::class
- ]
- );
- $this->addColumn(
- 'in_products',
- [
- 'header' => __('Select'),
- 'type' => 'checkbox',
- 'name' => 'in_products',
- 'values' => $this->_getSelectedProducts(),
- 'index' => 'entity_id',
- 'sortable' => false,
- 'header_css_class' => 'col-select',
- 'column_css_class' => 'col-select'
- ]
- );
- $this->addColumn(
- 'qty',
- [
- 'filter' => false,
- 'sortable' => false,
- 'header' => __('Quantity'),
- 'renderer' => \Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid\Renderer\Qty::class,
- 'name' => 'qty',
- 'inline_css' => 'qty',
- 'type' => 'input',
- 'validate_class' => 'validate-number',
- 'index' => 'qty'
- ]
- );
- return parent::_prepareColumns();
- }
- /**
- * Get grid url
- *
- * @return string
- */
- public function getGridUrl()
- {
- return $this->getUrl(
- 'sales/*/loadBlock',
- ['block' => 'search_grid', '_current' => true, 'collapse' => null]
- );
- }
- /**
- * Get selected products
- *
- * @return mixed
- */
- protected function _getSelectedProducts()
- {
- $products = $this->getRequest()->getPost('products', []);
- return $products;
- }
- /**
- * Add custom options to product collection
- *
- * @return $this
- */
- protected function _afterLoadCollection()
- {
- $this->getCollection()->addOptionsToResult();
- return parent::_afterLoadCollection();
- }
- }
|