Collection.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SalesRule\Model\ResourceModel\Report;
  7. /**
  8. * Sales report coupons collection
  9. *
  10. * @author Magento Core Team <core@magentocommerce.com>
  11. */
  12. class Collection extends \Magento\Sales\Model\ResourceModel\Report\Collection\AbstractCollection
  13. {
  14. /**
  15. * Period format for report (day, month, year)
  16. *
  17. * @var string
  18. */
  19. protected $_periodFormat;
  20. /**
  21. * Aggregated Data Table
  22. *
  23. * @var string
  24. */
  25. protected $_aggregationTable = 'salesrule_coupon_aggregated';
  26. /**
  27. * Array of columns that should be aggregated
  28. *
  29. * @var array
  30. */
  31. protected $_selectedColumns = [];
  32. /**
  33. * Array where rules ids stored
  34. *
  35. * @var array
  36. */
  37. protected $_rulesIdsFilter;
  38. /**
  39. * @var \Magento\SalesRule\Model\ResourceModel\Report\RuleFactory $ruleFactory
  40. */
  41. protected $_ruleFactory;
  42. /**
  43. * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
  44. * @param \Psr\Log\LoggerInterface $logger
  45. * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  46. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  47. * @param \Magento\SalesRule\Model\ResourceModel\Report\RuleFactory $ruleFactory
  48. * @param \Magento\Sales\Model\ResourceModel\Report $resource
  49. * @param mixed $connection
  50. */
  51. public function __construct(
  52. \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
  53. \Psr\Log\LoggerInterface $logger,
  54. \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
  55. \Magento\Framework\Event\ManagerInterface $eventManager,
  56. \Magento\Sales\Model\ResourceModel\Report $resource,
  57. \Magento\SalesRule\Model\ResourceModel\Report\RuleFactory $ruleFactory,
  58. \Magento\Framework\DB\Adapter\AdapterInterface $connection = null
  59. ) {
  60. $this->_ruleFactory = $ruleFactory;
  61. $resource->init($this->_aggregationTable);
  62. parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $resource, $connection);
  63. }
  64. /**
  65. * Collect columns for collection
  66. *
  67. * @return array
  68. */
  69. protected function _getSelectedColumns()
  70. {
  71. $connection = $this->getConnection();
  72. if ('month' == $this->_period) {
  73. $this->_periodFormat = $connection->getDateFormatSql('period', '%Y-%m');
  74. } elseif ('year' == $this->_period) {
  75. $this->_periodFormat = $connection->getDateExtractSql(
  76. 'period',
  77. \Magento\Framework\DB\Adapter\AdapterInterface::INTERVAL_YEAR
  78. );
  79. } else {
  80. $this->_periodFormat = $connection->getDateFormatSql('period', '%Y-%m-%d');
  81. }
  82. if (!$this->isTotals() && !$this->isSubTotals()) {
  83. $this->_selectedColumns = [
  84. 'period' => $this->_periodFormat,
  85. 'coupon_code',
  86. 'rule_name',
  87. 'coupon_uses' => 'SUM(coupon_uses)',
  88. 'subtotal_amount' => 'SUM(subtotal_amount)',
  89. 'discount_amount' => 'SUM(discount_amount)',
  90. 'total_amount' => 'SUM(total_amount)',
  91. 'subtotal_amount_actual' => 'SUM(subtotal_amount_actual)',
  92. 'discount_amount_actual' => 'SUM(discount_amount_actual)',
  93. 'total_amount_actual' => 'SUM(total_amount_actual)',
  94. ];
  95. }
  96. if ($this->isTotals()) {
  97. $this->_selectedColumns = $this->getAggregatedColumns();
  98. }
  99. if ($this->isSubTotals()) {
  100. $this->_selectedColumns = $this->getAggregatedColumns() + ['period' => $this->_periodFormat];
  101. }
  102. return $this->_selectedColumns;
  103. }
  104. /**
  105. * Add selected data
  106. *
  107. * @return Collection
  108. */
  109. protected function _applyAggregatedTable()
  110. {
  111. $this->getSelect()->from($this->getResource()->getMainTable(), $this->_getSelectedColumns());
  112. if ($this->isSubTotals()) {
  113. $this->getSelect()->group($this->_periodFormat);
  114. } elseif (!$this->isTotals()) {
  115. $this->getSelect()->group(
  116. [
  117. $this->_periodFormat,
  118. 'coupon_code',
  119. ]
  120. );
  121. }
  122. return parent::_applyAggregatedTable();
  123. }
  124. /**
  125. * Add filtering by rules ids
  126. *
  127. * @param array $rulesList
  128. * @return Collection
  129. */
  130. public function addRuleFilter(array $rulesList)
  131. {
  132. $this->_rulesIdsFilter = $rulesList;
  133. return $this;
  134. }
  135. /**
  136. * Apply filtering by rules ids
  137. *
  138. * @return $this
  139. */
  140. protected function _applyRulesFilter()
  141. {
  142. if (empty($this->_rulesIdsFilter) || !is_array($this->_rulesIdsFilter)) {
  143. return $this;
  144. }
  145. $rulesList = $this->_ruleFactory->create()->getUniqRulesNamesList();
  146. $rulesFilterSqlParts = [];
  147. foreach ($this->_rulesIdsFilter as $ruleId) {
  148. if (!isset($rulesList[$ruleId])) {
  149. continue;
  150. }
  151. $ruleName = $rulesList[$ruleId];
  152. $rulesFilterSqlParts[] = $this->getConnection()->quoteInto('rule_name = ?', $ruleName);
  153. }
  154. if (!empty($rulesFilterSqlParts)) {
  155. $this->getSelect()->where(implode(' OR ', $rulesFilterSqlParts));
  156. }
  157. return $this;
  158. }
  159. /**
  160. * Apply collection custom filter
  161. *
  162. * @return \Magento\Sales\Model\ResourceModel\Report\Collection\AbstractCollection
  163. */
  164. protected function _applyCustomFilter()
  165. {
  166. $this->_applyRulesFilter();
  167. return parent::_applyCustomFilter();
  168. }
  169. }