1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OroCRM\Bundle\MagentoBundle\Dashboard; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
|
7
|
|
|
use Oro\Bundle\ChartBundle\Model\ConfigProvider; |
8
|
|
|
|
9
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
10
|
|
|
|
11
|
|
|
use Oro\Bundle\ChartBundle\Model\ChartView; |
12
|
|
|
use Oro\Bundle\ChartBundle\Model\ChartViewBuilder; |
13
|
|
|
use Oro\Bundle\DashboardBundle\Helper\DateHelper; |
14
|
|
|
use Oro\Bundle\SecurityBundle\ORM\Walker\AclHelper; |
15
|
|
|
use Oro\Bundle\LocaleBundle\Formatter\DateTimeFormatter; |
16
|
|
|
|
17
|
|
|
use OroCRM\Bundle\MagentoBundle\Entity\Repository\OrderRepository; |
18
|
|
|
|
19
|
|
|
class OrderDataProvider |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var ManagerRegistry |
23
|
|
|
*/ |
24
|
|
|
protected $registry; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var AclHelper |
28
|
|
|
*/ |
29
|
|
|
protected $aclHelper; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ConfigProvider |
33
|
|
|
*/ |
34
|
|
|
protected $configProvider; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var DateTimeFormatter |
38
|
|
|
*/ |
39
|
|
|
protected $dateTimeFormatter; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var DateHelper |
43
|
|
|
*/ |
44
|
|
|
protected $dateHelper; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param ManagerRegistry $registry |
48
|
|
|
* @param AclHelper $aclHelper |
49
|
|
|
* @param ConfigProvider $configProvider |
50
|
|
|
* @param DateTimeFormatter $dateTimeFormatter |
51
|
|
|
* @param DateHelper $dateHelper |
52
|
|
|
*/ |
53
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
54
|
|
|
ManagerRegistry $registry, |
55
|
|
|
AclHelper $aclHelper, |
56
|
|
|
ConfigProvider $configProvider, |
57
|
|
|
DateTimeFormatter $dateTimeFormatter, |
58
|
|
|
DateHelper $dateHelper |
59
|
|
|
) { |
60
|
|
|
$this->registry = $registry; |
61
|
|
|
$this->aclHelper = $aclHelper; |
62
|
|
|
$this->configProvider = $configProvider; |
63
|
|
|
$this->dateTimeFormatter = $dateTimeFormatter; |
64
|
|
|
$this->dateHelper = $dateHelper; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param ChartViewBuilder $viewBuilder |
69
|
|
|
* @param array $dateRange |
70
|
|
|
* @param DateHelper $dateHelper |
71
|
|
|
* |
72
|
|
|
* @return ChartView |
73
|
|
|
*/ |
74
|
|
|
public function getAverageOrderAmountChartView(ChartViewBuilder $viewBuilder, $dateRange, DateHelper $dateHelper) |
75
|
|
|
{ |
76
|
|
|
list($start, $end) = $dateHelper->getPeriod($dateRange, 'OroCRMMagentoBundle:Customer', 'createdAt'); |
77
|
|
View Code Duplication |
if ($start === null && $end === null) { |
|
|
|
|
78
|
|
|
$start = new \DateTime(DateHelper::MIN_DATE, new \DateTimeZone('UTC')); |
79
|
|
|
$end = new \DateTime('now', new \DateTimeZone('UTC')); |
80
|
|
|
} |
81
|
|
|
/** @var OrderRepository $orderRepository */ |
82
|
|
|
$orderRepository = $this->registry->getRepository('OroCRMMagentoBundle:Order'); |
83
|
|
|
$result = $orderRepository->getAverageOrderAmount($this->aclHelper, $start, $end, $dateHelper); |
84
|
|
|
|
85
|
|
|
$chartOptions = array_merge_recursive( |
86
|
|
|
['name' => 'multiline_chart'], |
87
|
|
|
$this->configProvider->getChartConfig('average_order_amount') |
88
|
|
|
); |
89
|
|
|
$chartType = $dateHelper->getFormatStrings($start, $end)['viewType']; |
90
|
|
|
$chartOptions['data_schema']['label']['type'] = $chartType; |
91
|
|
|
$chartOptions['data_schema']['label']['label'] = |
92
|
|
|
sprintf( |
93
|
|
|
'oro.dashboard.chart.%s.label', |
94
|
|
|
$chartType |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
return $viewBuilder->setOptions($chartOptions) |
98
|
|
|
->setArrayData($result) |
99
|
|
|
->getView(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param ChartViewBuilder $viewBuilder |
104
|
|
|
* @param array $dateRange |
105
|
|
|
* |
106
|
|
|
* @return ChartView |
107
|
|
|
*/ |
108
|
|
|
public function getOrdersOverTimeChartView(ChartViewBuilder $viewBuilder, array $dateRange) |
109
|
|
|
{ |
110
|
|
|
/* @var $from DateTime */ |
111
|
|
|
/* @var $to DateTime */ |
112
|
|
|
list($from, $to) = $this->dateHelper->getPeriod($dateRange, 'OroCRMMagentoBundle:Order', 'createdAt'); |
113
|
|
View Code Duplication |
if ($from === null && $to === null) { |
|
|
|
|
114
|
|
|
$from = new \DateTime(DateHelper::MIN_DATE, new \DateTimeZone('UTC')); |
115
|
|
|
$to = new \DateTime('now', new \DateTimeZone('UTC')); |
116
|
|
|
} |
117
|
|
|
$result = $this->getOrderRepository()->getOrdersOverTime($this->aclHelper, $this->dateHelper, $from, $to); |
118
|
|
|
$items = $this->dateHelper->convertToCurrentPeriod($from, $to, $result, 'cnt', 'count'); |
119
|
|
|
|
120
|
|
|
$previousFrom = $this->createPreviousFrom($from, $to); |
121
|
|
|
$previousResult = $this->getOrderRepository()->getOrdersOverTime( |
122
|
|
|
$this->aclHelper, |
123
|
|
|
$this->dateHelper, |
124
|
|
|
$previousFrom, |
125
|
|
|
$from |
126
|
|
|
); |
127
|
|
|
$previousItems = $this->dateHelper->combinePreviousDataWithCurrentPeriod( |
128
|
|
|
$previousFrom, |
129
|
|
|
$from, |
130
|
|
|
$previousResult, |
131
|
|
|
'cnt', |
132
|
|
|
'count' |
133
|
|
|
); |
134
|
|
|
|
135
|
|
|
$chartType = $this->dateHelper->getFormatStrings($from, $to)['viewType']; |
136
|
|
|
$data = [ |
137
|
|
|
$this->createPeriodLabel($previousFrom, $from) => $previousItems, |
138
|
|
|
$this->createPeriodLabel($from, $to) => $items, |
139
|
|
|
]; |
140
|
|
|
|
141
|
|
|
return $this->createPeriodChartView($viewBuilder, 'orders_over_time_chart', $chartType, $data); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param ChartViewBuilder $viewBuilder |
146
|
|
|
* @param array $dateRange |
147
|
|
|
* |
148
|
|
|
* @return ChartView |
149
|
|
|
*/ |
150
|
|
|
public function getRevenueOverTimeChartView(ChartViewBuilder $viewBuilder, array $dateRange) |
151
|
|
|
{ |
152
|
|
|
/* @var $from DateTime */ |
153
|
|
|
/* @var $to DateTime */ |
154
|
|
|
list($from, $to) = $this->dateHelper->getPeriod($dateRange, 'OroCRMMagentoBundle:Order', 'createdAt'); |
155
|
|
View Code Duplication |
if ($from === null && $to === null) { |
|
|
|
|
156
|
|
|
$from = new \DateTime(DateHelper::MIN_DATE, new \DateTimeZone('UTC')); |
157
|
|
|
$to = new \DateTime('now', new \DateTimeZone('UTC')); |
158
|
|
|
} |
159
|
|
|
$orderRepository = $this->getOrderRepository(); |
160
|
|
|
|
161
|
|
|
$result = $orderRepository->getRevenueOverTime($this->aclHelper, $this->dateHelper, $from, $to); |
162
|
|
|
$items = $this->dateHelper->convertToCurrentPeriod($from, $to, $result, 'amount', 'amount'); |
163
|
|
|
|
164
|
|
|
$previousFrom = $this->createPreviousFrom($from, $to); |
165
|
|
|
$previousResult = $orderRepository->getRevenueOverTime( |
166
|
|
|
$this->aclHelper, |
167
|
|
|
$this->dateHelper, |
168
|
|
|
$previousFrom, |
169
|
|
|
$from |
170
|
|
|
); |
171
|
|
|
$previousItems = $this->dateHelper->combinePreviousDataWithCurrentPeriod( |
172
|
|
|
$previousFrom, |
173
|
|
|
$from, |
174
|
|
|
$previousResult, |
175
|
|
|
'amount', |
176
|
|
|
'amount' |
177
|
|
|
); |
178
|
|
|
|
179
|
|
|
$chartType = $this->dateHelper->getFormatStrings($from, $to)['viewType']; |
180
|
|
|
$data = [ |
181
|
|
|
$this->createPeriodLabel($previousFrom, $from) => $previousItems, |
182
|
|
|
$this->createPeriodLabel($from, $to) => $items, |
183
|
|
|
]; |
184
|
|
|
|
185
|
|
|
return $this->createPeriodChartView($viewBuilder, 'revenue_over_time_chart', $chartType, $data); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param ChartViewBuilder $viewBuilder |
190
|
|
|
* @param string $chart |
191
|
|
|
* @param string $type |
192
|
|
|
* @param array $data |
193
|
|
|
* |
194
|
|
|
* @return ChartView |
195
|
|
|
*/ |
196
|
|
|
protected function createPeriodChartView(ChartViewBuilder $viewBuilder, $chart, $type, array $data) |
197
|
|
|
{ |
198
|
|
|
$chartOptions = array_merge_recursive( |
199
|
|
|
['name' => 'multiline_chart'], |
200
|
|
|
$this->configProvider->getChartConfig($chart) |
201
|
|
|
); |
202
|
|
|
$chartOptions['data_schema']['label']['type'] = $type; |
203
|
|
|
$chartOptions['data_schema']['label']['label'] = |
204
|
|
|
sprintf( |
205
|
|
|
'oro.dashboard.chart.%s.label', |
206
|
|
|
$type |
207
|
|
|
); |
208
|
|
|
|
209
|
|
|
return $viewBuilder->setOptions($chartOptions) |
210
|
|
|
->setArrayData($data) |
211
|
|
|
->getView(); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param DateTime $from |
216
|
|
|
* @param DateTime $to |
217
|
|
|
* |
218
|
|
|
* @return DateTime |
219
|
|
|
*/ |
220
|
|
|
protected function createPreviousFrom(DateTime $from, DateTime $to) |
221
|
|
|
{ |
222
|
|
|
$diff = $to->getTimestamp() - $from->getTimestamp(); |
223
|
|
|
$previousFrom = clone $from; |
224
|
|
|
$previousFrom->setTimestamp($previousFrom->getTimestamp() - $diff); |
225
|
|
|
|
226
|
|
|
return $previousFrom; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @param DateTime $from |
231
|
|
|
* @param DateTime $to |
232
|
|
|
* |
233
|
|
|
* @return string |
234
|
|
|
*/ |
235
|
|
|
protected function createPeriodLabel(DateTime $from, DateTime $to) |
236
|
|
|
{ |
237
|
|
|
return sprintf( |
238
|
|
|
'%s - %s', |
239
|
|
|
$this->dateTimeFormatter->formatDate($from), |
240
|
|
|
$this->dateTimeFormatter->formatDate($to) |
241
|
|
|
); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @return OrderRepository |
246
|
|
|
*/ |
247
|
|
|
protected function getOrderRepository() |
248
|
|
|
{ |
249
|
|
|
return $this->registry->getRepository('OroCRMMagentoBundle:Order'); |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.