/
Collection.php
204 lines (172 loc) · 6.14 KB
/
Collection.php
1
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
/**
* Atlas Extensions
*
* NOTICE OF LICENSE
*
* This source file is subject to the Atlas Commercial License
* that is available through the world-wide-web at this URL:
*
* @copyright Copyright (c) 2015 Atlas Extensions
* @license Commercial
*/
class Atlas_Advancedreports_Model_Mysql4_Report_Collection
{
protected $_from;
protected $_to;
protected $_period;
protected $_timezone;
protected $_model;
protected $_intervals;
protected $_pageSize;
protected $_storeIds;
protected function _construct()
{
}
public function setPeriod($period)
{
$this->_period = $period;
}
public function setInterval($from, $to)
{
$this->_from = $from;
$this->_to = $to;
}
public function getIntervals()
{
if (!$this->_intervals)
{
$this->_intervals = array();
if (!$this->_from && !$this->_to)
{
return $this->_intervals;
}
$dateStart = new Zend_Date($this->_from);
$dateEnd = new Zend_Date($this->_to);
$t = array();
$firstInterval = true;
while ($dateStart->compare($dateEnd) <= 0) {
switch ($this->_period) {
case 'day' :
$t['title'] = $dateStart->toString(Mage::app()->getLocale()->getDateFormat());
$t['start'] = $dateStart->toString('yyyy-MM-dd HH:mm:ss');
$t['end'] = $dateStart->toString('yyyy-MM-dd 23:59:59');
$dateStart->addDay(1);
break;
case 'month':
$t['title'] = $dateStart->toString('MM/yyyy');
$t['start'] = ($firstInterval) ? $dateStart->toString('yyyy-MM-dd 00:00:00') : $dateStart->toString('yyyy-MM-01 00:00:00');
$lastInterval = ($dateStart->compareMonth($dateEnd->getMonth()) == 0);
$t['end'] = ($lastInterval) ? $dateStart->setDay($dateEnd->getDay())
->toString('yyyy-MM-dd 23:59:59') : $dateStart->toString('yyyy-MM-' . date('t', $dateStart->getTimestamp()) . ' 23:59:59');
$dateStart->addMonth(1);
if ($dateStart->compareMonth($dateEnd->getMonth()) == 0)
{
$dateStart->setDay(1);
}
$firstInterval = false;
break;
case 'year':
$t['title'] = $dateStart->toString('yyyy');
$t['start'] = ($firstInterval) ? $dateStart->toString('yyyy-MM-dd 00:00:00') : $dateStart->toString('yyyy-01-01 00:00:00');
$lastInterval = ($dateStart->compareYear($dateEnd->getYear()) == 0);
$t['end'] = ($lastInterval) ? $dateStart->setMonth($dateEnd->getMonth())
->setDay($dateEnd->getDay())->toString('yyyy-MM-dd 23:59:59') : $dateStart->toString('yyyy-12-31 23:59:59');
$dateStart->addYear(1);
if ($dateStart->compareYear($dateEnd->getYear()) == 0)
{
$dateStart->setMonth(1)->setDay(1);
}
$firstInterval = false;
break;
}
$this->_intervals[$t['title']] = $t;
}
}
return $this->_intervals;
}
/**
* Return date periods
*
* @return array
*/
public function getPeriods()
{
return array(
'day' => Mage::helper('reports')->__('Day'),
'month' => Mage::helper('reports')->__('Month'),
'year' => Mage::helper('reports')->__('Year')
);
}
public function setStoreIds($storeIds)
{
$this->_storeIds = $storeIds;
}
public function getStoreIds()
{
return $this->_storeIds;
}
public function getSize()
{
return count($this->getIntervals());
}
public function setPageSize($size)
{
$this->_pageSize = $size;
return $this;
}
public function getPageSize()
{
return $this->_pageSize;
}
public function initReport($modelClass)
{
$this->_model = Mage::getModel('reports/report')
->setPageSize($this->getPageSize())
->setStoreIds($this->getStoreIds())
->initCollection($modelClass);
}
public function getReportFull($from, $to)
{
return $this->_model->getReportFull($this->timeShift($from), $this->timeShift($to));
}
public function getReport($from, $to)
{
return $this->_model->getReport($this->timeShift($from), $this->timeShift($to));
}
public function timeShift($datetime)
{
if ($this->_timezone == 'local')
{
$dateObj = new Zend_Date($datetime, null, Mage::app()->getLocale()->getLocaleCode());
$dateObj->setTimezone('America/Los_Angeles');
$dateObj->set($datetime, Varien_Date::DATETIME_INTERNAL_FORMAT);
$dateObj->setTimezone('UTC');
return $dateObj->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
}
elseif ($this->_timezone == 'PST4PM')
{
$dateObj = new Zend_Date($datetime, null, Mage::app()->getLocale()->getLocaleCode());
$dateObj->setTimezone('EST');
$dateObj->set($datetime, Varien_Date::DATETIME_INTERNAL_FORMAT);
$dateObj->setTimezone('America/Denver');
return $dateObj->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
}
else
{//UTC
return $datetime;
}
}
public function setIsUTC($timeZone)
{
$this->_timezone = $timeZone;
}
public function getIsUTC()
{
return array(
'local' => Mage::helper('reports')->__('Local Time (PST)'),
'UTC' => Mage::helper('reports')->__('UTC'),
'PST4PM' => Mage::helper('reports')->__('PST 4PM')
);
}
}