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_Report_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: protected $_period;
57:
58: 59: 60: 61: 62:
63: protected $_model;
64:
65: 66: 67: 68: 69:
70: protected $_intervals;
71:
72: 73: 74: 75: 76:
77: protected $_pageSize;
78:
79: 80: 81: 82: 83:
84: protected $_storeIds;
85:
86: 87: 88: 89:
90: protected function _construct()
91: {
92:
93: }
94:
95: 96: 97: 98: 99: 100:
101: public function setPeriod($period)
102: {
103: $this->_period = $period;
104: return $this;
105: }
106:
107: 108: 109: 110: 111: 112: 113:
114: public function setInterval($from, $to)
115: {
116: $this->_from = $from;
117: $this->_to = $to;
118:
119: return $this;
120: }
121:
122: 123: 124: 125: 126:
127: public function getIntervals()
128: {
129: if (!$this->_intervals) {
130: $this->_intervals = array();
131: if (!$this->_from && !$this->_to) {
132: return $this->_intervals;
133: }
134: $dateStart = new Zend_Date($this->_from);
135: $dateEnd = new Zend_Date($this->_to);
136:
137:
138: $t = array();
139: $firstInterval = true;
140: while ($dateStart->compare($dateEnd) <= 0) {
141:
142: switch ($this->_period) {
143: case 'day':
144: $t['title'] = $dateStart->toString(Mage::app()->getLocale()->getDateFormat());
145: $t['start'] = $dateStart->toString('yyyy-MM-dd HH:mm:ss');
146: $t['end'] = $dateStart->toString('yyyy-MM-dd 23:59:59');
147: $dateStart->addDay(1);
148: break;
149: case 'month':
150: $t['title'] = $dateStart->toString('MM/yyyy');
151: $t['start'] = ($firstInterval) ? $dateStart->toString('yyyy-MM-dd 00:00:00')
152: : $dateStart->toString('yyyy-MM-01 00:00:00');
153:
154: $lastInterval = ($dateStart->compareMonth($dateEnd->getMonth()) == 0);
155:
156: $t['end'] = ($lastInterval) ? $dateStart->setDay($dateEnd->getDay())
157: ->toString('yyyy-MM-dd 23:59:59')
158: : $dateStart->toString('yyyy-MM-'.date('t', $dateStart->getTimestamp()).' 23:59:59');
159:
160: $dateStart->addMonth(1);
161:
162: if ($dateStart->compareMonth($dateEnd->getMonth()) == 0) {
163: $dateStart->setDay(1);
164: }
165:
166: $firstInterval = false;
167: break;
168: case 'year':
169: $t['title'] = $dateStart->toString('yyyy');
170: $t['start'] = ($firstInterval) ? $dateStart->toString('yyyy-MM-dd 00:00:00')
171: : $dateStart->toString('yyyy-01-01 00:00:00');
172:
173: $lastInterval = ($dateStart->compareYear($dateEnd->getYear()) == 0);
174:
175: $t['end'] = ($lastInterval) ? $dateStart->setMonth($dateEnd->getMonth())
176: ->setDay($dateEnd->getDay())->toString('yyyy-MM-dd 23:59:59')
177: : $dateStart->toString('yyyy-12-31 23:59:59');
178: $dateStart->addYear(1);
179:
180: if ($dateStart->compareYear($dateEnd->getYear()) == 0) {
181: $dateStart->setMonth(1)->setDay(1);
182: }
183:
184: $firstInterval = false;
185: break;
186: }
187: $this->_intervals[$t['title']] = $t;
188: }
189: }
190: return $this->_intervals;
191: }
192:
193: 194: 195: 196: 197:
198: public function getPeriods()
199: {
200: return array(
201: 'day' => Mage::helper('reports')->__('Day'),
202: 'month' => Mage::helper('reports')->__('Month'),
203: 'year' => Mage::helper('reports')->__('Year')
204: );
205: }
206:
207: 208: 209: 210: 211: 212:
213: public function setStoreIds($storeIds)
214: {
215: $this->_storeIds = $storeIds;
216: return $this;
217: }
218:
219: 220: 221: 222: 223:
224: public function getStoreIds()
225: {
226: return $this->_storeIds;
227: }
228:
229: 230: 231: 232: 233:
234: public function getSize()
235: {
236: return count($this->getIntervals());
237: }
238:
239: 240: 241: 242: 243: 244:
245: public function setPageSize($size)
246: {
247: $this->_pageSize = $size;
248: return $this;
249: }
250:
251: 252: 253: 254: 255:
256: public function getPageSize()
257: {
258: return $this->_pageSize;
259: }
260:
261: 262: 263: 264: 265: 266:
267: public function initReport($modelClass)
268: {
269: $this->_model = Mage::getModel('reports/report')
270: ->setPageSize($this->getPageSize())
271: ->setStoreIds($this->getStoreIds())
272: ->initCollection($modelClass);
273:
274: return $this;
275: }
276:
277: 278: 279: 280: 281: 282: 283:
284: public function getReportFull($from, $to)
285: {
286: return $this->_model->getReportFull($this->timeShift($from), $this->timeShift($to));
287: }
288:
289: 290: 291: 292: 293: 294: 295:
296: public function getReport($from, $to)
297: {
298: return $this->_model->getReport($this->timeShift($from), $this->timeShift($to));
299: }
300:
301: 302: 303: 304: 305: 306:
307: public function timeShift($datetime)
308: {
309: return Mage::app()->getLocale()
310: ->utcDate(null, $datetime, true, Varien_Date::DATETIME_INTERNAL_FORMAT)
311: ->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
312: }
313: }
314: