Grid.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Block\Adminhtml\Order\Create\Search;
  7. /**
  8. * Adminhtml sales order create search products block
  9. *
  10. * @api
  11. * @author Magento Core Team <core@magentocommerce.com>
  12. * @since 100.0.2
  13. */
  14. class Grid extends \Magento\Backend\Block\Widget\Grid\Extended
  15. {
  16. /**
  17. * Sales config
  18. *
  19. * @var \Magento\Sales\Model\Config
  20. */
  21. protected $_salesConfig;
  22. /**
  23. * Session quote
  24. *
  25. * @var \Magento\Backend\Model\Session\Quote
  26. */
  27. protected $_sessionQuote;
  28. /**
  29. * Catalog config
  30. *
  31. * @var \Magento\Catalog\Model\Config
  32. */
  33. protected $_catalogConfig;
  34. /**
  35. * Product factory
  36. *
  37. * @var \Magento\Catalog\Model\ProductFactory
  38. */
  39. protected $_productFactory;
  40. /**
  41. * @param \Magento\Backend\Block\Template\Context $context
  42. * @param \Magento\Backend\Helper\Data $backendHelper
  43. * @param \Magento\Catalog\Model\ProductFactory $productFactory
  44. * @param \Magento\Catalog\Model\Config $catalogConfig
  45. * @param \Magento\Backend\Model\Session\Quote $sessionQuote
  46. * @param \Magento\Sales\Model\Config $salesConfig
  47. * @param array $data
  48. */
  49. public function __construct(
  50. \Magento\Backend\Block\Template\Context $context,
  51. \Magento\Backend\Helper\Data $backendHelper,
  52. \Magento\Catalog\Model\ProductFactory $productFactory,
  53. \Magento\Catalog\Model\Config $catalogConfig,
  54. \Magento\Backend\Model\Session\Quote $sessionQuote,
  55. \Magento\Sales\Model\Config $salesConfig,
  56. array $data = []
  57. ) {
  58. $this->_productFactory = $productFactory;
  59. $this->_catalogConfig = $catalogConfig;
  60. $this->_sessionQuote = $sessionQuote;
  61. $this->_salesConfig = $salesConfig;
  62. parent::__construct($context, $backendHelper, $data);
  63. }
  64. /**
  65. * Constructor
  66. *
  67. * @return void
  68. */
  69. protected function _construct()
  70. {
  71. parent::_construct();
  72. $this->setId('sales_order_create_search_grid');
  73. $this->setRowClickCallback('order.productGridRowClick.bind(order)');
  74. $this->setCheckboxCheckCallback('order.productGridCheckboxCheck.bind(order)');
  75. $this->setRowInitCallback('order.productGridRowInit.bind(order)');
  76. $this->setDefaultSort('entity_id');
  77. $this->setUseAjax(true);
  78. if ($this->getRequest()->getParam('collapse')) {
  79. $this->setIsCollapsed(true);
  80. }
  81. }
  82. /**
  83. * Retrieve quote store object
  84. *
  85. * @return \Magento\Store\Model\Store
  86. */
  87. public function getStore()
  88. {
  89. return $this->_sessionQuote->getStore();
  90. }
  91. /**
  92. * Retrieve quote object
  93. *
  94. * @return \Magento\Quote\Model\Quote
  95. */
  96. public function getQuote()
  97. {
  98. return $this->_sessionQuote->getQuote();
  99. }
  100. /**
  101. * Add column filter to collection
  102. *
  103. * @param \Magento\Backend\Block\Widget\Grid\Column $column
  104. * @return $this
  105. */
  106. protected function _addColumnFilterToCollection($column)
  107. {
  108. // Set custom filter for in product flag
  109. if ($column->getId() == 'in_products') {
  110. $productIds = $this->_getSelectedProducts();
  111. if (empty($productIds)) {
  112. $productIds = 0;
  113. }
  114. if ($column->getFilter()->getValue()) {
  115. $this->getCollection()->addFieldToFilter('entity_id', ['in' => $productIds]);
  116. } else {
  117. if ($productIds) {
  118. $this->getCollection()->addFieldToFilter('entity_id', ['nin' => $productIds]);
  119. }
  120. }
  121. } else {
  122. parent::_addColumnFilterToCollection($column);
  123. }
  124. return $this;
  125. }
  126. /**
  127. * Prepare collection to be displayed in the grid
  128. *
  129. * @return $this
  130. */
  131. protected function _prepareCollection()
  132. {
  133. $attributes = $this->_catalogConfig->getProductAttributes();
  134. /* @var $collection \Magento\Catalog\Model\ResourceModel\Product\Collection */
  135. $collection = $this->_productFactory->create()->getCollection();
  136. $collection->setStore(
  137. $this->getStore()
  138. )->addAttributeToSelect(
  139. $attributes
  140. )->addAttributeToSelect(
  141. 'sku'
  142. )->addStoreFilter()->addAttributeToFilter(
  143. 'type_id',
  144. $this->_salesConfig->getAvailableProductTypes()
  145. )->addAttributeToSelect(
  146. 'gift_message_available'
  147. );
  148. $this->setCollection($collection);
  149. return parent::_prepareCollection();
  150. }
  151. /**
  152. * Prepare columns
  153. *
  154. * @return $this
  155. */
  156. protected function _prepareColumns()
  157. {
  158. $this->addColumn(
  159. 'entity_id',
  160. [
  161. 'header' => __('ID'),
  162. 'sortable' => true,
  163. 'header_css_class' => 'col-id',
  164. 'column_css_class' => 'col-id',
  165. 'index' => 'entity_id'
  166. ]
  167. );
  168. $this->addColumn(
  169. 'name',
  170. [
  171. 'header' => __('Product'),
  172. 'renderer' => \Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid\Renderer\Product::class,
  173. 'index' => 'name'
  174. ]
  175. );
  176. $this->addColumn('sku', ['header' => __('SKU'), 'index' => 'sku']);
  177. $this->addColumn(
  178. 'price',
  179. [
  180. 'header' => __('Price'),
  181. 'column_css_class' => 'price',
  182. 'type' => 'currency',
  183. 'currency_code' => $this->getStore()->getCurrentCurrencyCode(),
  184. 'rate' => $this->getStore()->getBaseCurrency()->getRate($this->getStore()->getCurrentCurrencyCode()),
  185. 'index' => 'price',
  186. 'renderer' => \Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid\Renderer\Price::class
  187. ]
  188. );
  189. $this->addColumn(
  190. 'in_products',
  191. [
  192. 'header' => __('Select'),
  193. 'type' => 'checkbox',
  194. 'name' => 'in_products',
  195. 'values' => $this->_getSelectedProducts(),
  196. 'index' => 'entity_id',
  197. 'sortable' => false,
  198. 'header_css_class' => 'col-select',
  199. 'column_css_class' => 'col-select'
  200. ]
  201. );
  202. $this->addColumn(
  203. 'qty',
  204. [
  205. 'filter' => false,
  206. 'sortable' => false,
  207. 'header' => __('Quantity'),
  208. 'renderer' => \Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid\Renderer\Qty::class,
  209. 'name' => 'qty',
  210. 'inline_css' => 'qty',
  211. 'type' => 'input',
  212. 'validate_class' => 'validate-number',
  213. 'index' => 'qty'
  214. ]
  215. );
  216. return parent::_prepareColumns();
  217. }
  218. /**
  219. * Get grid url
  220. *
  221. * @return string
  222. */
  223. public function getGridUrl()
  224. {
  225. return $this->getUrl(
  226. 'sales/*/loadBlock',
  227. ['block' => 'search_grid', '_current' => true, 'collapse' => null]
  228. );
  229. }
  230. /**
  231. * Get selected products
  232. *
  233. * @return mixed
  234. */
  235. protected function _getSelectedProducts()
  236. {
  237. $products = $this->getRequest()->getPost('products', []);
  238. return $products;
  239. }
  240. /**
  241. * Add custom options to product collection
  242. *
  243. * @return $this
  244. */
  245. protected function _afterLoadCollection()
  246. {
  247. $this->getCollection()->addOptionsToResult();
  248. return parent::_afterLoadCollection();
  249. }
  250. }