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_Reports_Model_Resource_Coupons_Collection extends Mage_Sales_Model_Entity_Order_Collection
36: {
37: 38: 39: 40: 41:
42: protected $_from = '';
43:
44: 45: 46: 47: 48:
49: protected $_to = '';
50:
51: 52: 53: 54: 55: 56: 57:
58: public function setDateRange($from, $to)
59: {
60: $this->_from = $from;
61: $this->_to = $to;
62: $this->_reset();
63: return $this;
64: }
65:
66: 67: 68: 69: 70: 71:
72: public function setStoreIds($storeIds)
73: {
74: $this->joinFields($this->_from, $this->_to, $storeIds);
75: return $this;
76: }
77:
78: 79: 80: 81: 82: 83: 84:
85: public function joinFields($from, $to, $storeIds = array())
86: {
87: $this->groupByAttribute('coupon_code')
88: ->addAttributeToFilter('created_at', array('from' => $from, 'to' => $to, 'datetime' => true))
89: ->addAttributeToFilter('coupon_code', array('neq' => ''))
90: ->getSelect()->columns(array('uses' => 'COUNT(e.entity_id)'))
91: ->having('uses > ?', 0)
92: ->order('uses ' . self::SORT_ORDER_DESC);
93:
94: $storeIds = array_values($storeIds);
95: if (count($storeIds) >= 1 && $storeIds[0] != '') {
96: $this->addAttributeToFilter('store_id', array('in' => $storeIds));
97: $this->addExpressionAttributeToSelect(
98: 'subtotal',
99: 'SUM({{base_subtotal}})',
100: array('base_subtotal'))
101: ->addExpressionAttributeToSelect(
102: 'discount',
103: 'SUM({{base_discount_amount}})',
104: array('base_discount_amount'))
105: ->addExpressionAttributeToSelect(
106: 'total',
107: 'SUM({{base_subtotal}}-{{base_discount_amount}})',
108: array('base_subtotal', 'base_discount_amount'));
109: } else {
110: $this->addExpressionAttributeToSelect(
111: 'subtotal',
112: 'SUM({{base_subtotal}}*{{base_to_global_rate}})',
113: array('base_subtotal', 'base_to_global_rate'))
114: ->addExpressionAttributeToSelect(
115: 'discount',
116: 'SUM({{base_discount_amount}}*{{base_to_global_rate}})',
117: array('base_discount_amount', 'base_to_global_rate'))
118: ->addExpressionAttributeToSelect(
119: 'total',
120: 'SUM(({{base_subtotal}}-{{base_discount_amount}})*{{base_to_global_rate}})',
121: array('base_subtotal', 'base_discount_amount', 'base_to_global_rate'));
122: }
123:
124: }
125:
126: 127: 128: 129: 130:
131: public function getSelectCountSql()
132: {
133: $countSelect = clone $this->getSelect();
134: $countSelect->reset(Zend_Db_Select::ORDER);
135: $countSelect->reset(Zend_Db_Select::LIMIT_COUNT);
136: $countSelect->reset(Zend_Db_Select::LIMIT_OFFSET);
137: $countSelect->reset(Zend_Db_Select::COLUMNS);
138: $countSelect->reset(Zend_Db_Select::GROUP);
139: $countSelect->reset(Zend_Db_Select::HAVING);
140: $countSelect->columns('COUNT(DISTINCT main_table.rule_id)');
141:
142: return $countSelect;
143: }
144: }
145: