1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Reports
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27:
28: /**
29: * Report collection abstract model
30: *
31: * @category Mage
32: * @package Mage_Reports
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Reports_Model_Resource_Report_Collection_Abstract extends Mage_Core_Model_Resource_Db_Collection_Abstract
36: {
37: /**
38: * From date
39: *
40: * @var string
41: */
42: protected $_from = null;
43:
44: /**
45: * To date
46: *
47: * @var string
48: */
49: protected $_to = null;
50:
51: /**
52: * Period
53: *
54: * @var string
55: */
56: protected $_period = null;
57:
58: /**
59: * Store ids
60: *
61: * @var int|array
62: */
63: protected $_storesIds = 0;
64:
65: /**
66: * Does filters should be applied
67: *
68: * @var bool
69: */
70: protected $_applyFilters = true;
71:
72: /**
73: * Is totals
74: *
75: * @var bool
76: */
77: protected $_isTotals = false;
78:
79: /**
80: * Is subtotals
81: *
82: * @var bool
83: */
84: protected $_isSubTotals = false;
85:
86: /**
87: * Aggregated columns
88: *
89: * @var array
90: */
91: protected $_aggregatedColumns = array();
92:
93: /**
94: * Set array of columns that should be aggregated
95: *
96: * @param array $columns
97: * @return Mage_Sales_Model_Resource_Report_Collection_Abstract
98: */
99: public function setAggregatedColumns(array $columns)
100: {
101: $this->_aggregatedColumns = $columns;
102: return $this;
103: }
104:
105: /**
106: * Retrieve array of columns that should be aggregated
107: *
108: * @return array
109: */
110: public function getAggregatedColumns()
111: {
112: return $this->_aggregatedColumns;
113: }
114:
115: /**
116: * Set date range
117: *
118: * @param mixed $from
119: * @param mixed $to
120: * @return Mage_Sales_Model_Resource_Report_Collection_Abstract
121: */
122: public function setDateRange($from = null, $to = null)
123: {
124: $this->_from = $from;
125: $this->_to = $to;
126: return $this;
127: }
128:
129: /**
130: * Set period
131: *
132: * @param string $period
133: * @return Mage_Sales_Model_Resource_Report_Collection_Abstract
134: */
135: public function setPeriod($period)
136: {
137: $this->_period = $period;
138: return $this;
139: }
140:
141: /**
142: * Apply date range filter
143: *
144: * @return Mage_Sales_Model_Resource_Report_Collection_Abstract
145: */
146: protected function _applyDateRangeFilter()
147: {
148: // Remember that field PERIOD is a DATE(YYYY-MM-DD) in all databases including Oracle
149: if ($this->_from !== null) {
150: $this->getSelect()->where('period >= ?', $this->_from);
151: }
152: if ($this->_to !== null) {
153: $this->getSelect()->where('period <= ?', $this->_to);
154: }
155:
156: return $this;
157: }
158:
159: /**
160: * Set store ids
161: *
162: * @param mixed $storeIds (null, int|string, array, array may contain null)
163: * @return Mage_Sales_Model_Resource_Report_Collection_Abstract
164: */
165: public function addStoreFilter($storeIds)
166: {
167: $this->_storesIds = $storeIds;
168: return $this;
169: }
170:
171: /**
172: * Apply stores filter to select object
173: *
174: * @param Zend_Db_Select $select
175: * @return Mage_Sales_Model_Resource_Report_Collection_Abstract
176: */
177: protected function _applyStoresFilterToSelect(Zend_Db_Select $select)
178: {
179: $nullCheck = false;
180: $storeIds = $this->_storesIds;
181:
182: if (!is_array($storeIds)) {
183: $storeIds = array($storeIds);
184: }
185:
186: $storeIds = array_unique($storeIds);
187:
188: if ($index = array_search(null, $storeIds)) {
189: unset($storeIds[$index]);
190: $nullCheck = true;
191: }
192:
193: $storeIds[0] = ($storeIds[0] == '') ? 0 : $storeIds[0];
194:
195: if ($nullCheck) {
196: $select->where('store_id IN(?) OR store_id IS NULL', $storeIds);
197: } else {
198: $select->where('store_id IN(?)', $storeIds);
199: }
200:
201: return $this;
202: }
203:
204: /**
205: * Apply stores filter
206: *
207: * @return Mage_Sales_Model_Resource_Report_Collection_Abstract
208: */
209: protected function _applyStoresFilter()
210: {
211: return $this->_applyStoresFilterToSelect($this->getSelect());
212: }
213:
214: /**
215: * Set apply filters flag
216: *
217: * @param boolean $flag
218: * @return Mage_Sales_Model_Resource_Report_Collection_Abstract
219: */
220: public function setApplyFilters($flag)
221: {
222: $this->_applyFilters = $flag;
223: return $this;
224: }
225:
226: /**
227: * Getter/Setter for isTotals
228: *
229: * @param null|boolean $flag
230: * @return Mage_Sales_Model_Resource_Report_Collection_Abstract
231: */
232: public function isTotals($flag = null)
233: {
234: if (is_null($flag)) {
235: return $this->_isTotals;
236: }
237: $this->_isTotals = $flag;
238: return $this;
239: }
240:
241: /**
242: * Getter/Setter for isSubTotals
243: *
244: * @param null|boolean $flag
245: * @return Mage_Sales_Model_Resource_Report_Collection_Abstract
246: */
247: public function isSubTotals($flag = null)
248: {
249: if (is_null($flag)) {
250: return $this->_isSubTotals;
251: }
252: $this->_isSubTotals = $flag;
253: return $this;
254: }
255:
256: /**
257: * Custom filters application ability
258: *
259: * @return Mage_Reports_Model_Resource_Report_Collection_Abstract
260: */
261: protected function _applyCustomFilter()
262: {
263: return $this;
264: }
265:
266: /**
267: * Load data
268: * Redeclare parent load method just for adding method _beforeLoad
269: *
270: * @param bool $printQuery
271: * @param bool $logQuery
272: * @return Mage_Sales_Model_Resource_Report_Collection_Abstract
273: */
274: public function load($printQuery = false, $logQuery = false)
275: {
276: if ($this->isLoaded()) {
277: return $this;
278: }
279: $this->_initSelect();
280: if ($this->_applyFilters) {
281: $this->_applyDateRangeFilter();
282: $this->_applyStoresFilter();
283: $this->_applyCustomFilter();
284: }
285: return parent::load($printQuery, $logQuery);
286: }
287: }
288: