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: class Mage_Adminhtml_Block_Report_Grid_Abstract extends Mage_Adminhtml_Block_Widget_Grid
29: {
30: protected $_resourceCollectionName = '';
31: protected $_currentCurrencyCode = null;
32: protected $_storeIds = array();
33: protected $_aggregatedColumns = null;
34:
35: public function __construct()
36: {
37: parent::__construct();
38: $this->setFilterVisibility(false);
39: $this->setPagerVisibility(false);
40: $this->setUseAjax(false);
41: if (isset($this->_columnGroupBy)) {
42: $this->isColumnGrouped($this->_columnGroupBy, true);
43: }
44: $this->setEmptyCellLabel(Mage::helper('adminhtml')->__('No records found for this period.'));
45: }
46:
47: public function getResourceCollectionName()
48: {
49: return $this->_resourceCollectionName;
50: }
51:
52: public function getCollection()
53: {
54: if (is_null($this->_collection)) {
55: $this->setCollection(Mage::getModel('reports/grouped_collection'));
56: }
57: return $this->_collection;
58: }
59:
60: protected function _getAggregatedColumns()
61: {
62: if (is_null($this->_aggregatedColumns)) {
63: foreach ($this->getColumns() as $column) {
64: if (!is_array($this->_aggregatedColumns)) {
65: $this->_aggregatedColumns = array();
66: }
67: if ($column->hasTotal()) {
68: $this->_aggregatedColumns[$column->getId()] = "{$column->getTotal()}({$column->getIndex()})";
69: }
70: }
71: }
72: return $this->_aggregatedColumns;
73: }
74:
75: 76: 77: 78: 79: 80: 81: 82: 83: 84:
85: public function addColumn($columnId, $column)
86: {
87: if (is_array($column) && array_key_exists('visibility_filter', $column)) {
88: $filterData = $this->getFilterData();
89: $visibilityFilter = $column['visibility_filter'];
90: if (!is_array($visibilityFilter)) {
91: $visibilityFilter = array($visibilityFilter);
92: }
93: foreach ($visibilityFilter as $k => $v) {
94: if (is_int($k)) {
95: $filterFieldId = $v;
96: $filterFieldValue = true;
97: } else {
98: $filterFieldId = $k;
99: $filterFieldValue = $v;
100: }
101: if (
102: !$filterData->hasData($filterFieldId) ||
103: $filterData->getData($filterFieldId) != $filterFieldValue
104: ) {
105: return $this;
106: }
107: }
108: }
109: return parent::addColumn($columnId, $column);
110: }
111:
112: 113: 114: 115: 116:
117: protected function _getStoreIds()
118: {
119: $filterData = $this->getFilterData();
120: if ($filterData) {
121: $storeIds = explode(',', $filterData->getData('store_ids'));
122: } else {
123: $storeIds = array();
124: }
125:
126: $allowedStoreIds = array_keys(Mage::app()->getStores());
127:
128: $storeIds = array_intersect($allowedStoreIds, $storeIds);
129:
130: if (empty($storeIds)) {
131: $storeIds = $allowedStoreIds;
132: }
133:
134: $storeIds = array_values($storeIds);
135:
136: return $storeIds;
137: }
138:
139: protected function _prepareCollection()
140: {
141: $filterData = $this->getFilterData();
142:
143: if ($filterData->getData('from') == null || $filterData->getData('to') == null) {
144: $this->setCountTotals(false);
145: $this->setCountSubTotals(false);
146: return parent::_prepareCollection();
147: }
148:
149: $storeIds = $this->_getStoreIds();;
150:
151: $orderStatuses = $filterData->getData('order_statuses');
152: if (is_array($orderStatuses)) {
153: if (count($orderStatuses) == 1 && strpos($orderStatuses[0],',')!== false) {
154: $filterData->setData('order_statuses', explode(',',$orderStatuses[0]));
155: }
156: }
157:
158: $resourceCollection = Mage::getResourceModel($this->getResourceCollectionName())
159: ->setPeriod($filterData->getData('period_type'))
160: ->setDateRange($filterData->getData('from', null), $filterData->getData('to', null))
161: ->addStoreFilter($storeIds)
162: ->setAggregatedColumns($this->_getAggregatedColumns());
163:
164: $this->_addOrderStatusFilter($resourceCollection, $filterData);
165: $this->_addCustomFilter($resourceCollection, $filterData);
166:
167: if ($this->_isExport) {
168: $this->setCollection($resourceCollection);
169: return $this;
170: }
171:
172: if ($filterData->getData('show_empty_rows', false)) {
173: Mage::helper('reports')->prepareIntervalsCollection(
174: $this->getCollection(),
175: $filterData->getData('from', null),
176: $filterData->getData('to', null),
177: $filterData->getData('period_type')
178: );
179: }
180:
181: if ($this->getCountSubTotals()) {
182: $this->getSubTotals();
183: }
184:
185: if ($this->getCountTotals()) {
186: $totalsCollection = Mage::getResourceModel($this->getResourceCollectionName())
187: ->setPeriod($filterData->getData('period_type'))
188: ->setDateRange($filterData->getData('from', null), $filterData->getData('to', null))
189: ->addStoreFilter($storeIds)
190: ->setAggregatedColumns($this->_getAggregatedColumns())
191: ->isTotals(true);
192:
193: $this->_addOrderStatusFilter($totalsCollection, $filterData);
194: $this->_addCustomFilter($totalsCollection, $filterData);
195:
196: foreach ($totalsCollection as $item) {
197: $this->setTotals($item);
198: break;
199: }
200: }
201:
202: $this->getCollection()->setColumnGroupBy($this->_columnGroupBy);
203: $this->getCollection()->setResourceCollection($resourceCollection);
204:
205: return parent::_prepareCollection();
206: }
207:
208: public function getCountTotals()
209: {
210: if (!$this->getTotals()) {
211: $filterData = $this->getFilterData();
212: $totalsCollection = Mage::getResourceModel($this->getResourceCollectionName())
213: ->setPeriod($filterData->getData('period_type'))
214: ->setDateRange($filterData->getData('from', null), $filterData->getData('to', null))
215: ->addStoreFilter($this->_getStoreIds())
216: ->setAggregatedColumns($this->_getAggregatedColumns())
217: ->isTotals(true);
218:
219: $this->_addOrderStatusFilter($totalsCollection, $filterData);
220:
221: if (count($totalsCollection->getItems()) < 1 || !$filterData->getData('from')) {
222: $this->setTotals(new Varien_Object());
223: } else {
224: foreach ($totalsCollection->getItems() as $item) {
225: $this->setTotals($item);
226: break;
227: }
228: }
229: }
230: return parent::getCountTotals();
231: }
232:
233: public function getSubTotals()
234: {
235: $filterData = $this->getFilterData();
236: $subTotalsCollection = Mage::getResourceModel($this->getResourceCollectionName())
237: ->setPeriod($filterData->getData('period_type'))
238: ->setDateRange($filterData->getData('from', null), $filterData->getData('to', null))
239: ->addStoreFilter($this->_getStoreIds())
240: ->setAggregatedColumns($this->_getAggregatedColumns())
241: ->isSubTotals(true);
242:
243: $this->_addOrderStatusFilter($subTotalsCollection, $filterData);
244: $this->_addCustomFilter($subTotalsCollection, $filterData);
245:
246: $this->setSubTotals($subTotalsCollection->getItems());
247: return parent::getSubTotals();
248: }
249:
250: public function setStoreIds($storeIds)
251: {
252: $this->_storeIds = $storeIds;
253: return $this;
254: }
255:
256: public function getCurrentCurrencyCode()
257: {
258: if (is_null($this->_currentCurrencyCode)) {
259: $this->_currentCurrencyCode = (count($this->_storeIds) > 0)
260: ? Mage::app()->getStore(array_shift($this->_storeIds))->getBaseCurrencyCode()
261: : Mage::app()->getStore()->getBaseCurrencyCode();
262: }
263: return $this->_currentCurrencyCode;
264: }
265:
266: 267: 268: 269: 270: 271:
272: public function getRate($toCurrency)
273: {
274: return Mage::app()->getStore()->getBaseCurrency()->getRate($toCurrency);
275: }
276:
277: 278: 279: 280: 281: 282: 283:
284: protected function _addOrderStatusFilter($collection, $filterData)
285: {
286: $collection->addOrderStatusFilter($filterData->getData('order_statuses'));
287: return $this;
288: }
289:
290: 291: 292: 293: 294: 295: 296: 297:
298: protected function _addCustomFilter($collection, $filterData)
299: {
300: return $this;
301: }
302: }
303: