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_Rule_Createdat extends Mage_Reports_Model_Resource_Report_Abstract
36: {
37: 38: 39: 40:
41: protected function _construct()
42: {
43: $this->_init('salesrule/coupon_aggregated', 'id');
44: }
45:
46: 47: 48: 49: 50: 51: 52:
53: public function aggregate($from = null, $to = null)
54: {
55: return $this->_aggregateByOrder('created_at', $from, $to);
56: }
57:
58: 59: 60: 61: 62: 63: 64: 65: 66:
67: protected function _aggregateByOrder($aggregationField, $from, $to)
68: {
69: $from = $this->_dateToUtc($from);
70: $to = $this->_dateToUtc($to);
71:
72: $this->_checkDates($from, $to);
73:
74: $table = $this->getMainTable();
75: $sourceTable = $this->getTable('sales/order');
76: $adapter = $this->_getWriteAdapter();
77: $adapter->beginTransaction();
78:
79: try {
80: if ($from !== null || $to !== null) {
81: $subSelect = $this->_getTableDateRangeSelect($sourceTable, 'created_at', 'updated_at', $from, $to);
82: } else {
83: $subSelect = null;
84: }
85:
86: $this->_clearTableByDateRange($table, $from, $to, $subSelect);
87:
88:
89: $periodExpr = $adapter->getDatePartSql(
90: $this->getStoreTZOffsetQuery($sourceTable, $aggregationField, $from, $to)
91: );
92:
93: $columns = array(
94: 'period' => $periodExpr,
95: 'store_id' => 'store_id',
96: 'order_status' => 'status',
97: 'coupon_code' => 'coupon_code',
98: 'rule_name' => 'coupon_rule_name',
99: 'coupon_uses' => 'COUNT(entity_id)',
100:
101: 'subtotal_amount' =>
102: $adapter->getIfNullSql('SUM((base_subtotal - ' .
103: $adapter->getIfNullSql('base_subtotal_canceled', 0).') * base_to_global_rate)', 0),
104:
105: 'discount_amount' =>
106: $adapter->getIfNullSql('SUM((ABS(base_discount_amount) - ' .
107: $adapter->getIfNullSql('base_discount_canceled', 0).') * base_to_global_rate)', 0),
108:
109: 'total_amount' =>
110: $adapter->getIfNullSql('SUM((base_subtotal - ' .
111: $adapter->getIfNullSql('base_subtotal_canceled', 0) . ' - '.
112: $adapter->getIfNullSql('ABS(base_discount_amount) - ' .
113: $adapter->getIfNullSql('base_discount_canceled', 0), 0). ')
114: * base_to_global_rate)', 0),
115:
116: 'subtotal_amount_actual' =>
117: $adapter->getIfNullSql('SUM((base_subtotal_invoiced - ' .
118: $adapter->getIfNullSql('base_subtotal_refunded', 0). ') * base_to_global_rate)', 0),
119:
120: 'discount_amount_actual' =>
121: $adapter->getIfNullSql('SUM((base_discount_invoiced - ' .
122: $adapter->getIfNullSql('base_discount_refunded', 0) . ')
123: * base_to_global_rate)', 0),
124:
125: 'total_amount_actual' =>
126: $adapter->getIfNullSql('SUM((base_subtotal_invoiced - ' .
127: $adapter->getIfNullSql('base_subtotal_refunded', 0) . ' - ' .
128: $adapter->getIfNullSql('base_discount_invoiced - ' .
129: $adapter->getIfNullSql('base_discount_refunded', 0), 0) .
130: ') * base_to_global_rate)', 0),
131: );
132:
133: $select = $adapter->select();
134: $select->from(array('source_table' => $sourceTable), $columns)
135: ->where('coupon_code IS NOT NULL');
136:
137: if ($subSelect !== null) {
138: $select->having($this->_makeConditionFromDateRangeSelect($subSelect, 'period'));
139: }
140:
141: $select->group(array(
142: $periodExpr,
143: 'store_id',
144: 'status',
145: 'coupon_code'
146: ));
147:
148: $select->having('COUNT(entity_id) > 0');
149: $select->insertFromSelect($table, array_keys($columns));
150:
151: $adapter->query($select->insertFromSelect($table, array_keys($columns)));
152:
153: $select->reset();
154:
155: $columns = array(
156: 'period' => 'period',
157: 'store_id' => new Zend_Db_Expr('0'),
158: 'order_status' => 'order_status',
159: 'coupon_code' => 'coupon_code',
160: 'rule_name' => 'rule_name',
161: 'coupon_uses' => 'SUM(coupon_uses)',
162: 'subtotal_amount' => 'SUM(subtotal_amount)',
163: 'discount_amount' => 'SUM(discount_amount)',
164: 'total_amount' => 'SUM(total_amount)',
165: 'subtotal_amount_actual' => 'SUM(subtotal_amount_actual)',
166: 'discount_amount_actual' => 'SUM(discount_amount_actual)',
167: 'total_amount_actual' => 'SUM(total_amount_actual)',
168: );
169:
170: $select
171: ->from($table, $columns)
172: ->where('store_id <> 0');
173:
174: if ($subSelect !== null) {
175: $select->where($this->_makeConditionFromDateRangeSelect($subSelect, 'period'));
176: }
177:
178: $select->group(array(
179: 'period',
180: 'order_status',
181: 'coupon_code'
182: ));
183:
184: $adapter->query($select->insertFromSelect($table, array_keys($columns)));
185: $adapter->commit();
186: } catch (Exception $e) {
187: $adapter->rollBack();
188: throw $e;
189: }
190:
191: return $this;
192: }
193: }
194: