1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_SalesRule_Model_Resource_Report_Collection extends Mage_Sales_Model_Resource_Report_Collection_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_periodFormat;
43:
44: 45: 46: 47: 48:
49: protected $_aggregationTable = 'salesrule/coupon_aggregated';
50:
51: 52: 53: 54: 55:
56: protected $_selectedColumns = array();
57:
58: 59: 60: 61: 62:
63: protected $_rulesIdsFilter;
64:
65: 66: 67: 68:
69: public function __construct()
70: {
71: parent::_construct();
72: $this->setModel('adminhtml/report_item');
73: $this->_resource = Mage::getResourceModel('sales/report')->init($this->_aggregationTable);
74: $this->setConnection($this->getResource()->getReadConnection());
75: }
76:
77: 78: 79: 80: 81:
82: protected function _getSelectedColumns()
83: {
84: $adapter = $this->getConnection();
85: if ('month' == $this->_period) {
86: $this->_periodFormat = $adapter->getDateFormatSql('period', '%Y-%m');
87: } elseif ('year' == $this->_period) {
88: $this->_periodFormat =
89: $adapter->getDateExtractSql('period', Varien_Db_Adapter_Interface::INTERVAL_YEAR);
90: } else {
91: $this->_periodFormat = $adapter->getDateFormatSql('period', '%Y-%m-%d');
92: }
93:
94: if (!$this->isTotals() && !$this->isSubTotals()) {
95: $this->_selectedColumns = array(
96: 'period' => $this->_periodFormat,
97: 'coupon_code',
98: 'rule_name',
99: 'coupon_uses' => 'SUM(coupon_uses)',
100: 'subtotal_amount' => 'SUM(subtotal_amount)',
101: 'discount_amount' => 'SUM(discount_amount)',
102: 'total_amount' => 'SUM(total_amount)',
103: 'subtotal_amount_actual' => 'SUM(subtotal_amount_actual)',
104: 'discount_amount_actual' => 'SUM(discount_amount_actual)',
105: 'total_amount_actual' => 'SUM(total_amount_actual)',
106: );
107: }
108:
109: if ($this->isTotals()) {
110: $this->_selectedColumns = $this->getAggregatedColumns();
111: }
112:
113: if ($this->isSubTotals()) {
114: $this->_selectedColumns =
115: $this->getAggregatedColumns() +
116: array('period' => $this->_periodFormat);
117: }
118:
119: return $this->_selectedColumns;
120: }
121:
122: 123: 124: 125: 126:
127: protected function _initSelect()
128: {
129: $this->getSelect()->from($this->getResource()->getMainTable(), $this->_getSelectedColumns());
130: if ($this->isSubTotals()) {
131: $this->getSelect()->group($this->_periodFormat);
132: } else if (!$this->isTotals()) {
133: $this->getSelect()->group(array(
134: $this->_periodFormat,
135: 'coupon_code'
136: ));
137: }
138:
139: return $this;
140: }
141:
142: 143: 144: 145: 146: 147:
148: public function addRuleFilter($rulesList)
149: {
150: $this->_rulesIdsFilter = $rulesList;
151: return $this;
152: }
153:
154: 155: 156: 157: 158:
159: protected function _applyRulesFilter()
160: {
161: if (empty($this->_rulesIdsFilter) || !is_array($this->_rulesIdsFilter)) {
162: return $this;
163: }
164:
165: $rulesList = Mage::getResourceModel('salesrule/report_rule')->getUniqRulesNamesList();
166:
167: $rulesFilterSqlParts = array();
168:
169: foreach ($this->_rulesIdsFilter as $ruleId) {
170: if (!isset($rulesList[$ruleId])) {
171: continue;
172: }
173: $ruleName = $rulesList[$ruleId];
174: $rulesFilterSqlParts[] = $this->getConnection()->quoteInto('rule_name = ?', $ruleName);
175: }
176:
177: if (!empty($rulesFilterSqlParts)) {
178: $this->getSelect()->where(implode($rulesFilterSqlParts, ' OR '));
179: }
180: }
181:
182: 183: 184: 185: 186:
187: protected function _applyCustomFilter()
188: {
189: $this->_applyRulesFilter();
190: return parent::_applyCustomFilter();
191: }
192: }
193: