Collection.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Reports\Model\ResourceModel\Quote;
  7. use Magento\Store\Model\Store;
  8. /**
  9. * Collection of abandoned quotes with reports join.
  10. *
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Collection extends \Magento\Quote\Model\ResourceModel\Quote\Collection
  15. {
  16. /**
  17. * @var \Magento\Customer\Model\ResourceModel\Customer
  18. */
  19. protected $customerResource;
  20. /**
  21. * @param \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory
  22. * @param \Psr\Log\LoggerInterface $logger
  23. * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  24. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  25. * @param \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot $entitySnapshot
  26. * @param \Magento\Customer\Model\ResourceModel\Customer $customerResource
  27. * @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
  28. * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
  29. */
  30. public function __construct(
  31. \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
  32. \Psr\Log\LoggerInterface $logger,
  33. \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
  34. \Magento\Framework\Event\ManagerInterface $eventManager,
  35. \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot $entitySnapshot,
  36. \Magento\Customer\Model\ResourceModel\Customer $customerResource,
  37. \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
  38. \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
  39. ) {
  40. parent::__construct(
  41. $entityFactory,
  42. $logger,
  43. $fetchStrategy,
  44. $eventManager,
  45. $entitySnapshot,
  46. $connection,
  47. $resource
  48. );
  49. $this->customerResource = $customerResource;
  50. }
  51. /**
  52. * Filter collections by stores.
  53. *
  54. * @param array $storeIds
  55. * @param bool $withAdmin
  56. * @return $this
  57. * @since 100.3.1
  58. */
  59. public function addStoreFilter(array $storeIds, $withAdmin = true)
  60. {
  61. if ($withAdmin) {
  62. $storeIds[] = Store::DEFAULT_STORE_ID;
  63. }
  64. $this->addFieldToFilter('store_id', ['in' => $storeIds]);
  65. return $this;
  66. }
  67. /**
  68. * Prepare for abandoned report
  69. *
  70. * @param array $storeIds
  71. * @param string $filter
  72. * @return $this
  73. */
  74. public function prepareForAbandonedReport($storeIds, $filter = null)
  75. {
  76. $this->addFieldToFilter(
  77. 'items_count',
  78. ['neq' => '0']
  79. )->addFieldToFilter(
  80. 'main_table.is_active',
  81. '1'
  82. )->addFieldToFilter(
  83. 'main_table.customer_id',
  84. ['neq' => null]
  85. )->addSubtotal(
  86. $storeIds,
  87. $filter
  88. )->setOrder(
  89. 'updated_at'
  90. );
  91. if (isset($filter['email']) || isset($filter['customer_name'])) {
  92. $this->addCustomerData($filter);
  93. }
  94. if (is_array($storeIds) && !empty($storeIds)) {
  95. $this->addFieldToFilter('store_id', ['in' => $storeIds]);
  96. }
  97. return $this;
  98. }
  99. /**
  100. * Add customer data
  101. *
  102. * @param array|null $filter
  103. * @return $this
  104. */
  105. public function addCustomerData($filter = null)
  106. {
  107. $customersSelect = $this->customerResource->getConnection()->select();
  108. $customersSelect->from(
  109. ['customer' => $this->customerResource->getTable('customer_entity')],
  110. 'entity_id'
  111. );
  112. if (isset($filter['customer_name'])) {
  113. $customerName = $this->customerResource->getConnection()
  114. ->getConcatSql(['customer.firstname', 'customer.lastname'], ' ');
  115. $customersSelect->where($customerName . ' LIKE ?', '%' . $filter['customer_name'] . '%');
  116. }
  117. if (isset($filter['email'])) {
  118. $customersSelect->where('customer.email LIKE ?', '%' . $filter['email'] . '%');
  119. }
  120. $filteredCustomers = $this->customerResource->getConnection()->fetchCol($customersSelect);
  121. $this->getSelect()->where('main_table.customer_id IN (?)', $filteredCustomers);
  122. return $this;
  123. }
  124. /**
  125. * Add subtotals
  126. *
  127. * @param array $storeIds
  128. * @param null|array $filter
  129. * @return $this
  130. */
  131. public function addSubtotal($storeIds = '', $filter = null)
  132. {
  133. if (is_array($storeIds)) {
  134. $this->getSelect()->columns(
  135. ['subtotal' => '(main_table.base_subtotal_with_discount*main_table.base_to_global_rate)']
  136. );
  137. $this->_joinedFields['subtotal'] =
  138. '(main_table.base_subtotal_with_discount*main_table.base_to_global_rate)';
  139. } else {
  140. $this->getSelect()->columns(['subtotal' => 'main_table.base_subtotal_with_discount']);
  141. $this->_joinedFields['subtotal'] = 'main_table.base_subtotal_with_discount';
  142. }
  143. if ($filter && is_array($filter) && isset($filter['subtotal'])) {
  144. if (isset($filter['subtotal']['from'])) {
  145. $this->getSelect()->where(
  146. $this->_joinedFields['subtotal'] . ' >= ?',
  147. $filter['subtotal']['from'],
  148. \Zend_Db::FLOAT_TYPE
  149. );
  150. }
  151. if (isset($filter['subtotal']['to'])) {
  152. $this->getSelect()->where(
  153. $this->_joinedFields['subtotal'] . ' <= ?',
  154. $filter['subtotal']['to'],
  155. \Zend_Db::FLOAT_TYPE
  156. );
  157. }
  158. }
  159. return $this;
  160. }
  161. /**
  162. * Resolve customers data based on ids quote table.
  163. *
  164. * @return void
  165. */
  166. public function resolveCustomerNames()
  167. {
  168. $select = $this->customerResource->getConnection()->select();
  169. $customerName = $this->customerResource->getConnection()->getConcatSql(['firstname', 'lastname'], ' ');
  170. $select->from(
  171. ['customer' => $this->customerResource->getTable('customer_entity')],
  172. ['entity_id', 'email']
  173. );
  174. $select->columns(
  175. ['customer_name' => $customerName]
  176. );
  177. $select->where(
  178. 'customer.entity_id IN (?)',
  179. array_column(
  180. $this->getData(),
  181. 'customer_id'
  182. )
  183. );
  184. $customersData = $this->customerResource->getConnection()->fetchAll($select);
  185. foreach ($this->getItems() as $item) {
  186. foreach ($customersData as $customerItemData) {
  187. if ($item['customer_id'] == $customerItemData['entity_id']) {
  188. $item->setData(array_merge($item->getData(), $customerItemData));
  189. }
  190. }
  191. }
  192. }
  193. }