Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Collection.php
Go to the documentation of this file.
1 <?php
7 
13 class Collection extends \Magento\Sales\Model\ResourceModel\Report\Collection\AbstractCollection
14 {
20  protected $_periodFormat;
21 
27  protected $_aggregationTable = 'salesrule_coupon_aggregated';
28 
34  protected $_selectedColumns = [];
35 
41  protected $_rulesIdsFilter;
42 
46  protected $_ruleFactory;
47 
57  public function __construct(
58  \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
59  \Psr\Log\LoggerInterface $logger,
60  \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
61  \Magento\Framework\Event\ManagerInterface $eventManager,
62  \Magento\Sales\Model\ResourceModel\Report $resource,
63  \Magento\SalesRule\Model\ResourceModel\Report\RuleFactory $ruleFactory,
64  \Magento\Framework\DB\Adapter\AdapterInterface $connection = null
65  ) {
66  $this->_ruleFactory = $ruleFactory;
67  $resource->init($this->_aggregationTable);
68  parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $resource, $connection);
69  }
70 
76  protected function _getSelectedColumns()
77  {
78  $connection = $this->getConnection();
79  if ('month' == $this->_period) {
80  $this->_periodFormat = $connection->getDateFormatSql('period', '%Y-%m');
81  } elseif ('year' == $this->_period) {
82  $this->_periodFormat = $connection->getDateExtractSql(
83  'period',
84  \Magento\Framework\DB\Adapter\AdapterInterface::INTERVAL_YEAR
85  );
86  } else {
87  $this->_periodFormat = $connection->getDateFormatSql('period', '%Y-%m-%d');
88  }
89 
90  if (!$this->isTotals() && !$this->isSubTotals()) {
91  $this->_selectedColumns = [
92  'period' => $this->_periodFormat,
93  'coupon_code',
94  'rule_name',
95  'coupon_uses' => 'SUM(coupon_uses)',
96  'subtotal_amount' => 'SUM(subtotal_amount)',
97  'discount_amount' => 'SUM(discount_amount)',
98  'total_amount' => 'SUM(total_amount)',
99  'subtotal_amount_actual' => 'SUM(subtotal_amount_actual)',
100  'discount_amount_actual' => 'SUM(discount_amount_actual)',
101  'total_amount_actual' => 'SUM(total_amount_actual)',
102  ];
103  }
104 
105  if ($this->isTotals()) {
106  $this->_selectedColumns = $this->getAggregatedColumns();
107  }
108 
109  if ($this->isSubTotals()) {
110  $this->_selectedColumns = $this->getAggregatedColumns() + ['period' => $this->_periodFormat];
111  }
112 
114  }
115 
121  protected function _applyAggregatedTable()
122  {
123  $this->getSelect()->from($this->getResource()->getMainTable(), $this->_getSelectedColumns());
124  if ($this->isSubTotals()) {
125  $this->getSelect()->group($this->_periodFormat);
126  } elseif (!$this->isTotals()) {
127  $this->getSelect()->group(
128  [
129  $this->_periodFormat,
130  'coupon_code',
131  ]
132  );
133  }
134 
135  return parent::_applyAggregatedTable();
136  }
137 
144  public function addRuleFilter(array $rulesList)
145  {
146  $this->_rulesIdsFilter = $rulesList;
147  return $this;
148  }
149 
155  protected function _applyRulesFilter()
156  {
157  if (empty($this->_rulesIdsFilter) || !is_array($this->_rulesIdsFilter)) {
158  return $this;
159  }
160 
161  $rulesList = $this->_ruleFactory->create()->getUniqRulesNamesList();
162 
163  $rulesFilterSqlParts = [];
164  foreach ($this->_rulesIdsFilter as $ruleId) {
165  if (!isset($rulesList[$ruleId])) {
166  continue;
167  }
168  $ruleName = $rulesList[$ruleId];
169  $rulesFilterSqlParts[] = $this->getConnection()->quoteInto('rule_name = ?', $ruleName);
170  }
171 
172  if (!empty($rulesFilterSqlParts)) {
173  $this->getSelect()->where(implode(' OR ', $rulesFilterSqlParts));
174  }
175  return $this;
176  }
177 
183  protected function _applyCustomFilter()
184  {
185  $this->_applyRulesFilter();
186  return parent::_applyCustomFilter();
187  }
188 }
__construct(\Magento\Framework\Data\Collection\EntityFactory $entityFactory, \Psr\Log\LoggerInterface $logger, \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Sales\Model\ResourceModel\Report $resource, \Magento\SalesRule\Model\ResourceModel\Report\RuleFactory $ruleFactory, \Magento\Framework\DB\Adapter\AdapterInterface $connection=null)
Definition: Collection.php:57
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$resource
Definition: bulk.php:12
$logger
$connection
Definition: bulk.php:13