Collection.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model\ResourceModel\Order;
  7. use Magento\Sales\Api\Data\OrderSearchResultInterface;
  8. use Magento\Sales\Model\ResourceModel\Collection\AbstractCollection;
  9. /**
  10. * Flat sales order collection
  11. *
  12. * @api
  13. * @author Magento Core Team <core@magentocommerce.com>
  14. * @since 100.0.2
  15. */
  16. class Collection extends AbstractCollection implements OrderSearchResultInterface
  17. {
  18. /**
  19. * @var string
  20. */
  21. protected $_idFieldName = 'entity_id';
  22. /**
  23. * Event prefix
  24. *
  25. * @var string
  26. */
  27. protected $_eventPrefix = 'sales_order_collection';
  28. /**
  29. * Event object
  30. *
  31. * @var string
  32. */
  33. protected $_eventObject = 'order_collection';
  34. /**
  35. * @var \Magento\Framework\DB\Helper
  36. */
  37. protected $_coreResourceHelper;
  38. /**
  39. * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
  40. * @param \Psr\Log\LoggerInterface $logger
  41. * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  42. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  43. * @param \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot $entitySnapshot
  44. * @param \Magento\Framework\DB\Helper $coreResourceHelper
  45. * @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
  46. * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
  47. */
  48. public function __construct(
  49. \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
  50. \Psr\Log\LoggerInterface $logger,
  51. \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
  52. \Magento\Framework\Event\ManagerInterface $eventManager,
  53. \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot $entitySnapshot,
  54. \Magento\Framework\DB\Helper $coreResourceHelper,
  55. \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
  56. \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
  57. ) {
  58. parent::__construct(
  59. $entityFactory,
  60. $logger,
  61. $fetchStrategy,
  62. $eventManager,
  63. $entitySnapshot,
  64. $connection,
  65. $resource
  66. );
  67. $this->_coreResourceHelper = $coreResourceHelper;
  68. }
  69. /**
  70. * Model initialization
  71. *
  72. * @return void
  73. */
  74. protected function _construct()
  75. {
  76. $this->_init(\Magento\Sales\Model\Order::class, \Magento\Sales\Model\ResourceModel\Order::class);
  77. $this->addFilterToMap(
  78. 'entity_id',
  79. 'main_table.entity_id'
  80. )->addFilterToMap(
  81. 'customer_id',
  82. 'main_table.customer_id'
  83. )->addFilterToMap(
  84. 'quote_address_id',
  85. 'main_table.quote_address_id'
  86. );
  87. }
  88. /**
  89. * Add items count expr to collection select, backward capability with eav structure
  90. *
  91. * @return $this
  92. */
  93. public function addItemCountExpr()
  94. {
  95. if ($this->_fieldsToSelect === null) {
  96. // If we select all fields from table, we need to add column alias
  97. $this->getSelect()->columns(['items_count' => 'total_item_count']);
  98. } else {
  99. $this->addFieldToSelect('total_item_count', 'items_count');
  100. }
  101. return $this;
  102. }
  103. /**
  104. * Minimize usual count select
  105. *
  106. * @return \Magento\Framework\DB\Select
  107. */
  108. public function getSelectCountSql()
  109. {
  110. /* @var $countSelect \Magento\Framework\DB\Select */
  111. $countSelect = parent::getSelectCountSql();
  112. $countSelect->resetJoinLeft();
  113. return $countSelect;
  114. }
  115. /**
  116. * Reset left join
  117. *
  118. * @param int $limit
  119. * @param int $offset
  120. * @return \Magento\Eav\Model\Entity\Collection\AbstractCollection
  121. */
  122. protected function _getAllIdsSelect($limit = null, $offset = null)
  123. {
  124. $idsSelect = parent::_getAllIdsSelect($limit, $offset);
  125. $idsSelect->resetJoinLeft();
  126. return $idsSelect;
  127. }
  128. /**
  129. * Join table sales_order_address to select for billing and shipping order addresses.
  130. *
  131. * Create correlation map
  132. *
  133. * @return $this
  134. */
  135. protected function _addAddressFields()
  136. {
  137. $billingAliasName = 'billing_o_a';
  138. $shippingAliasName = 'shipping_o_a';
  139. $joinTable = $this->getTable('sales_order_address');
  140. $this->addFilterToMap(
  141. 'billing_firstname',
  142. $billingAliasName . '.firstname'
  143. )->addFilterToMap(
  144. 'billing_lastname',
  145. $billingAliasName . '.lastname'
  146. )->addFilterToMap(
  147. 'billing_telephone',
  148. $billingAliasName . '.telephone'
  149. )->addFilterToMap(
  150. 'billing_postcode',
  151. $billingAliasName . '.postcode'
  152. )->addFilterToMap(
  153. 'shipping_firstname',
  154. $shippingAliasName . '.firstname'
  155. )->addFilterToMap(
  156. 'shipping_lastname',
  157. $shippingAliasName . '.lastname'
  158. )->addFilterToMap(
  159. 'shipping_telephone',
  160. $shippingAliasName . '.telephone'
  161. )->addFilterToMap(
  162. 'shipping_postcode',
  163. $shippingAliasName . '.postcode'
  164. );
  165. $this->getSelect()->joinLeft(
  166. [$billingAliasName => $joinTable],
  167. "(main_table.entity_id = {$billingAliasName}.parent_id" .
  168. " AND {$billingAliasName}.address_type = 'billing')",
  169. [
  170. $billingAliasName . '.firstname',
  171. $billingAliasName . '.lastname',
  172. $billingAliasName . '.telephone',
  173. $billingAliasName . '.postcode'
  174. ]
  175. )->joinLeft(
  176. [$shippingAliasName => $joinTable],
  177. "(main_table.entity_id = {$shippingAliasName}.parent_id" .
  178. " AND {$shippingAliasName}.address_type = 'shipping')",
  179. [
  180. $shippingAliasName . '.firstname',
  181. $shippingAliasName . '.lastname',
  182. $shippingAliasName . '.telephone',
  183. $shippingAliasName . '.postcode'
  184. ]
  185. );
  186. $this->_coreResourceHelper->prepareColumnsList($this->getSelect());
  187. return $this;
  188. }
  189. /**
  190. * Add addresses information to select
  191. *
  192. * @return \Magento\Sales\Model\ResourceModel\Collection\AbstractCollection
  193. */
  194. public function addAddressFields()
  195. {
  196. return $this->_addAddressFields();
  197. }
  198. /**
  199. * Add field search filter to collection as OR condition
  200. *
  201. * @param string $field
  202. * @param int|string|array|null $condition
  203. * @return $this
  204. *
  205. * @see self::_getConditionSql for $condition
  206. */
  207. public function addFieldToSearchFilter($field, $condition = null)
  208. {
  209. $field = $this->_getMappedField($field);
  210. $this->_select->orWhere($this->_getConditionSql($field, $condition));
  211. return $this;
  212. }
  213. /**
  214. * Specify collection select filter by attribute value
  215. *
  216. * @param array $attributes
  217. * @param array|int|string|null $condition
  218. * @return $this
  219. */
  220. public function addAttributeToSearchFilter($attributes, $condition = null)
  221. {
  222. if (is_array($attributes) && !empty($attributes)) {
  223. $this->_addAddressFields();
  224. foreach ($attributes as $attribute) {
  225. $this->addFieldToSearchFilter($this->_attributeToField($attribute['attribute']), $attribute);
  226. }
  227. } else {
  228. $this->addAttributeToFilter($attributes, $condition);
  229. }
  230. return $this;
  231. }
  232. /**
  233. * Add filter by specified billing agreements
  234. *
  235. * @param int|int[] $agreements
  236. * @return $this
  237. */
  238. public function addBillingAgreementsFilter($agreements)
  239. {
  240. $agreements = is_array($agreements) ? $agreements : [$agreements];
  241. $this->getSelect()->joinInner(
  242. ['sbao' => $this->getTable('sales_billing_agreement_order')],
  243. 'main_table.entity_id = sbao.order_id',
  244. []
  245. )->where(
  246. 'sbao.agreement_id IN(?)',
  247. $agreements
  248. );
  249. return $this;
  250. }
  251. }