-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathdashgoals.php
More file actions
executable file
·556 lines (474 loc) · 26.8 KB
/
Copy pathdashgoals.php
File metadata and controls
executable file
·556 lines (474 loc) · 26.8 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/
if (!defined('_PS_VERSION_')) {
exit;
}
class dashgoals extends Module
{
protected static $month_labels = [];
protected static $types = ['traffic', 'conversion', 'avg_cart_value'];
protected static $real_color = ['#9E5BA1', '#00A89C', '#3AC4ED', '#F99031'];
protected static $more_color = ['#803E84', '#008E7E', '#20B2E7', '#F66E1B'];
protected static $less_color = ['#BC77BE', '#00C2BB', '#51D6F2', '#FBB244'];
public function __construct()
{
$this->name = 'dashgoals';
$this->tab = 'administration';
$this->version = '2.0.5';
$this->author = 'PrestaShop';
parent::__construct();
$this->displayName = $this->trans('Dashboard Goals', [], 'Modules.Dashgoals.Admin');
$this->description = $this->trans('Enrich your stats: add a block with your store’s forecast to always be one step ahead!', [], 'Modules.Dashgoals.Admin');
$this->ps_versions_compliancy = ['min' => '1.7.1.0', 'max' => _PS_VERSION_];
self::$month_labels = [
'01' => $this->trans('January', [], 'Modules.Dashgoals.Admin'),
'02' => $this->trans('February', [], 'Modules.Dashgoals.Admin'),
'03' => $this->trans('March', [], 'Modules.Dashgoals.Admin'),
'04' => $this->trans('April', [], 'Modules.Dashgoals.Admin'),
'05' => $this->trans('May', [], 'Modules.Dashgoals.Admin'),
'06' => $this->trans('June', [], 'Modules.Dashgoals.Admin'),
'07' => $this->trans('July', [], 'Modules.Dashgoals.Admin'),
'08' => $this->trans('August', [], 'Modules.Dashgoals.Admin'),
'09' => $this->trans('September', [], 'Modules.Dashgoals.Admin'),
'10' => $this->trans('October', [], 'Modules.Dashgoals.Admin'),
'11' => $this->trans('November', [], 'Modules.Dashgoals.Admin'),
'12' => $this->trans('December', [], 'Modules.Dashgoals.Admin'),
];
}
public function install()
{
Configuration::updateValue('PS_DASHGOALS_CURRENT_YEAR', date('Y'));
for ($month = '01'; $month <= 12; $month = sprintf('%02d', (int) $month + 1)) {
$key = Tools::strtoupper('dashgoals_traffic_' . $month . '_' . date('Y'));
if (!ConfigurationKPI::get($key)) {
ConfigurationKPI::updateValue($key, 600);
}
$key = Tools::strtoupper('dashgoals_conversion_' . $month . '_' . date('Y'));
if (!ConfigurationKPI::get($key)) {
ConfigurationKPI::updateValue($key, 2);
}
$key = Tools::strtoupper('dashgoals_avg_cart_value_' . $month . '_' . date('Y'));
if (!ConfigurationKPI::get($key)) {
ConfigurationKPI::updateValue($key, 80);
}
}
// Prepare tab
$tab = new Tab();
$tab->active = true;
$tab->class_name = 'AdminDashgoals';
$tab->name = [];
foreach (Language::getLanguages(true) as $lang) {
$tab->name[$lang['id_lang']] = 'Dashgoals';
}
$tab->id_parent = -1;
$tab->module = $this->name;
return
$tab->add()
&& parent::install()
&& $this->registerHook('dashboardZoneTwo')
&& $this->registerHook('dashboardData')
&& $this->registerHook('actionAdminControllerSetMedia');
}
public function uninstall()
{
$id_tab = (int) Tab::getIdFromClassName('AdminDashgoals');
if ($id_tab) {
$tab = new Tab($id_tab);
$tab->delete();
}
return parent::uninstall();
}
public function hookActionAdminControllerSetMedia()
{
if (get_class($this->context->controller) == 'AdminDashboardController') {
$this->context->controller->addJs($this->_path . 'views/js/' . $this->name . '.js');
}
}
public function setMonths($year)
{
$months = [];
for ($i = '01'; $i <= 12; $i = sprintf('%02d', (int) $i + 1)) {
$months[$i . '_' . $year] = ['label' => self::$month_labels[$i], 'values' => []];
}
foreach (self::$types as $type) {
foreach ($months as $month => &$month_row) {
$key = 'dashgoals_' . $type . '_' . $month;
if (Tools::isSubmit('submitDashGoals')) {
ConfigurationKPI::updateValue(Tools::strtoupper($key), (float) Tools::getValue($key));
}
$month_row['values'][$type] = ConfigurationKPI::get(Tools::strtoupper($key));
}
}
return $months;
}
public function hookDashboardZoneTwo($params)
{
$year = Configuration::get('PS_DASHGOALS_CURRENT_YEAR');
$months = $this->setMonths($year);
$this->context->smarty->assign(
[
'colors' => self::$real_color,
'currency' => $this->context->currency,
'goals_year' => $year,
'goals_months' => $months,
'dashgoals_ajax_link' => $this->context->link->getAdminLink('AdminDashgoals'),
]
);
return $this->display(__FILE__, 'dashboard_zone_two.tpl');
}
public function hookDashboardData($params)
{
$year = ((isset($params['extra']) && $params['extra'] > 1970 && $params['extra'] < 2999) ? $params['extra'] : Configuration::get('PS_DASHGOALS_CURRENT_YEAR'));
return ['data_chart' => ['dash_goals_chart1' => $this->getChartData($year)]];
}
/**
* @return float
*/
protected function fakeConfigurationKPI_get(string $key)
{
$start = [
'TRAFFIC' => 3000,
'CONVERSION' => 2,
'AVG_CART_VALUE' => 90,
];
if (preg_match('/^DASHGOALS_([A-Z_]+)_([0-9]{2})/', $key, $matches)) {
if ($matches[1] == 'TRAFFIC') {
return $start[$matches[1]] * (1 + ((float) $matches[2] - 1) / 10);
} else {
return $start[$matches[1]];
}
}
return 0;
}
public function getChartData($year)
{
// There are stream types (different charts) and for each types there are 3 available zones (one color for the goal, one if you over perform and one if you under perfom)
$stream_types = [
['type' => 'traffic', 'title' => $this->trans('Traffic', [], 'Modules.Dashgoals.Admin'), 'unit_text' => $this->trans('Visits', [], 'Admin.Shopparameters.Feature')],
['type' => 'conversion', 'title' => $this->trans('Conversion', [], 'Modules.Dashgoals.Admin'), 'unit_text' => ''],
['type' => 'avg_cart_value', 'title' => $this->trans('Average cart value', [], 'Modules.Dashgoals.Admin'), 'unit_text' => ''],
['type' => 'sales', 'title' => $this->trans('Sales', [], 'Admin.Global'), 'unit_text' => ''],
];
$stream_zones = [
['zone' => 'real', 'text' => ''],
['zone' => 'more', 'text' => $this->trans('Goal exceeded', [], 'Modules.Dashgoals.Admin')],
['zone' => 'less', 'text' => $this->trans('Goal not reached', [], 'Modules.Dashgoals.Admin')],
];
// We initialize all the streams types for all the zones
$streams = [];
$average_goals = [];
foreach ($stream_types as $key => $stream_type) {
$streams[$stream_type['type']] = [];
foreach ($stream_zones as $stream_zone) {
$streams[$stream_type['type']][$stream_zone['zone']] = [
'key' => $stream_type['type'] . '_' . $stream_zone['zone'],
'title' => $stream_type['title'],
'unit_text' => $stream_type['unit_text'],
'zone_text' => $stream_zone['text'],
'color' => ($stream_zone['zone'] == 'more' ? self::$more_color[$key] : ($stream_zone['zone'] == 'less' ? self::$less_color[$key] : self::$real_color[$key])),
'values' => [],
'disabled' => ($stream_type['type'] == 'sales') ? false : true,
];
}
$average_goals[$stream_type['type']] = 0;
}
if (Configuration::get('PS_DASHBOARD_SIMULATION')) {
$visits = $orders = $sales = [];
$from = strtotime(date('Y-01-01 00:00:00'));
$to = strtotime(date('Y-12-31 00:00:00'));
for ($date = $from; $date <= $to; $date = strtotime('+1 day', $date)) {
$visits[$date] = round(rand(2000, 5000));
$orders[$date] = round(rand(40, 100));
$sales[$date] = round(rand(3000, 9000), 2);
}
// We need to calculate the average value of each goals for the year, this will be the base rate for "100%"
for ($i = '01'; $i <= 12; $i = sprintf('%02d', (int) $i + 1)) {
$average_goals['traffic'] += $this->fakeConfigurationKPI_get('DASHGOALS_TRAFFIC_' . $i . '_' . $year);
$average_goals['conversion'] += $this->fakeConfigurationKPI_get('DASHGOALS_CONVERSION_' . $i . '_' . $year);
$average_goals['avg_cart_value'] += $this->fakeConfigurationKPI_get('DASHGOALS_AVG_CART_VALUE_' . $i . '_' . $year);
}
foreach ($average_goals as &$average_goal) {
$average_goal /= 12;
}
$average_goals['sales'] = $average_goals['traffic'] * $average_goals['conversion'] / 100 * $average_goals['avg_cart_value'];
// Now we can calculate the value for every months
for ($i = '01'; $i <= 12; $i = sprintf('%02d', (int) $i + 1)) {
$timestamp = strtotime($year . '-' . $i . '-01');
$month_goal = $this->fakeConfigurationKPI_get('DASHGOALS_TRAFFIC_' . $i . '_' . $year);
$value = (isset($visits[$timestamp]) ? $visits[$timestamp] : 0);
$stream_values = $this->getValuesFromGoals($average_goals['traffic'], $month_goal, $value, self::$month_labels[$i]);
$goal_diff = $value - $month_goal;
$stream_values['real']['traffic'] = $value;
$stream_values['real']['goal'] = $month_goal;
if ($value > 0) {
$stream_values['real']['goal_diff'] = round(($goal_diff * 100) / ($month_goal > 0 ? $month_goal : 1), 2);
}
$stream_values['less']['traffic'] = $value;
$stream_values['more']['traffic'] = $value;
if ($value > 0 && $value < $month_goal) {
$stream_values['less']['goal_diff'] = $goal_diff;
} elseif ($value > 0) {
$stream_values['more']['goal_diff'] = $goal_diff;
}
if ($value == 0) {
$streams['traffic']['less']['zone_text'] = $this->trans('Goal set:', [], 'Modules.Dashgoals.Admin');
$stream_values['less']['goal'] = $month_goal;
}
foreach ($stream_zones as $stream_zone) {
$streams['traffic'][$stream_zone['zone']]['values'][] = $stream_values[$stream_zone['zone']];
}
$month_goal = $this->fakeConfigurationKPI_get('DASHGOALS_CONVERSION_' . $i . '_' . $year);
$value = 100 * ((isset($visits[$timestamp]) && $visits[$timestamp] && isset($orders[$timestamp]) && $orders[$timestamp]) ? ($orders[$timestamp] / $visits[$timestamp]) : 0);
$stream_values = $this->getValuesFromGoals($average_goals['conversion'], $month_goal, $value, self::$month_labels[$i]);
$goal_diff = $value - $month_goal;
$stream_values['real']['conversion'] = round($value, 2);
$stream_values['real']['goal'] = round($month_goal, 2);
if ($value > 0) {
$stream_values['real']['goal_diff'] = round(($goal_diff * 100) / ($month_goal > 0 ? $month_goal : 1), 2);
}
$stream_values['less']['conversion'] = $value;
$stream_values['more']['conversion'] = $value;
if ($value > 0 && $value < $month_goal) {
$stream_values['less']['goal_diff'] = round(($goal_diff * 100) / ($month_goal > 0 ? $month_goal : 1), 2);
} elseif ($value > 0) {
$stream_values['more']['goal_diff'] = round(($goal_diff * 100) / ($month_goal > 0 ? $month_goal : 1), 2);
}
if ($value == 0) {
$streams['conversion']['less']['zone_text'] = $this->trans('Goal set:', [], 'Modules.Dashgoals.Admin');
$stream_values['less']['goal'] = $month_goal;
}
foreach ($stream_zones as $stream_zone) {
$streams['conversion'][$stream_zone['zone']]['values'][] = $stream_values[$stream_zone['zone']];
}
$month_goal = $this->fakeConfigurationKPI_get('DASHGOALS_AVG_CART_VALUE_' . $i . '_' . $year);
$value = ((isset($orders[$timestamp]) && $orders[$timestamp] && isset($sales[$timestamp]) && $sales[$timestamp]) ? ($sales[$timestamp] / $orders[$timestamp]) : 0);
$stream_values = $this->getValuesFromGoals($average_goals['avg_cart_value'], $month_goal, $value, self::$month_labels[$i]);
$goal_diff = $value - $month_goal;
$stream_values['real']['avg_cart_value'] = $value;
$stream_values['real']['goal'] = $month_goal;
if ($value > 0) {
$stream_values['real']['goal_diff'] = round(($goal_diff * 100) / ($month_goal > 0 ? $month_goal : 1), 2);
}
$stream_values['less']['avg_cart_value'] = $value;
$stream_values['more']['avg_cart_value'] = $value;
if ($value > 0 && $value < $month_goal) {
$stream_values['less']['goal_diff'] = $goal_diff;
} elseif ($value > 0) {
$stream_values['more']['goal_diff'] = $goal_diff;
}
if ($value == 0) {
$streams['avg_cart_value']['less']['zone_text'] = $this->trans('Goal set:', [], 'Modules.Dashgoals.Admin');
$stream_values['less']['goal'] = $month_goal;
}
foreach ($stream_zones as $stream_zone) {
$streams['avg_cart_value'][$stream_zone['zone']]['values'][] = $stream_values[$stream_zone['zone']];
}
$month_goal = $this->fakeConfigurationKPI_get('DASHGOALS_TRAFFIC_' . $i . '_' . $year)
* $this->fakeConfigurationKPI_get('DASHGOALS_CONVERSION_' . $i . '_' . $year)
/ 100
* $this->fakeConfigurationKPI_get('DASHGOALS_AVG_CART_VALUE_' . $i . '_' . $year);
$value = (isset($sales[$timestamp]) ? $sales[$timestamp] : 0);
$stream_values = $this->getValuesFromGoals($average_goals['sales'], $month_goal, $value, self::$month_labels[$i]);
$goal_diff = $value - $month_goal;
$stream_values['real']['sales'] = $value;
$stream_values['real']['goal'] = $month_goal;
if ($value > 0) {
$stream_values['real']['goal_diff'] = round(($goal_diff * 100) / ($month_goal > 0 ? $month_goal : 1), 2);
}
$stream_values['less']['sales'] = $value;
$stream_values['more']['sales'] = $value;
if ($value > 0 && $value < $month_goal) {
$stream_values['less']['goal_diff'] = $goal_diff;
} elseif ($value > 0) {
$stream_values['more']['goal_diff'] = $goal_diff;
}
if ($value == 0) {
$streams['sales']['less']['zone_text'] = $this->trans('Goal set:', [], 'Modules.Dashgoals.Admin');
$stream_values['less']['goal'] = $month_goal;
}
foreach ($stream_zones as $stream_zone) {
$streams['sales'][$stream_zone['zone']]['values'][] = $stream_values[$stream_zone['zone']];
}
}
} else {
// Retrieve gross data from AdminStatsController
$visits = AdminStatsController::getVisits(false, $year . date('-01-01'), $year . date('-12-31'), 'month');
$orders = AdminStatsController::getOrders($year . date('-01-01'), $year . date('-12-31'), 'month');
$sales = AdminStatsController::getTotalSales($year . date('-01-01'), $year . date('-12-31'), 'month');
// We need to calculate the average value of each goals for the year, this will be the base rate for "100%"
for ($i = '01'; $i <= 12; $i = sprintf('%02d', (int) $i + 1)) {
$average_goals['traffic'] += (float) ConfigurationKPI::get('DASHGOALS_TRAFFIC_' . $i . '_' . $year);
$average_goals['conversion'] += (float) ConfigurationKPI::get('DASHGOALS_CONVERSION_' . $i . '_' . $year) / 100;
$average_goals['avg_cart_value'] += (float) ConfigurationKPI::get('DASHGOALS_AVG_CART_VALUE_' . $i . '_' . $year);
}
foreach ($average_goals as &$average_goal) {
$average_goal /= 12;
}
$average_goals['sales'] = $average_goals['traffic'] * $average_goals['conversion'] * $average_goals['avg_cart_value'];
// Now we can calculate the value for every months
for ($i = '01'; $i <= 12; $i = sprintf('%02d', (int) $i + 1)) {
$timestamp = strtotime($year . '-' . $i . '-01');
$month_goal = ConfigurationKPI::get('DASHGOALS_TRAFFIC_' . $i . '_' . $year);
$value = (isset($visits[$timestamp]) ? $visits[$timestamp] : 0);
$stream_values = $this->getValuesFromGoals($average_goals['traffic'], $month_goal, $value, self::$month_labels[$i]);
$goal_diff = $value - $month_goal;
$stream_values['real']['traffic'] = $value;
$stream_values['real']['goal'] = $month_goal;
if ($value > 0) {
$stream_values['real']['goal_diff'] = round(($goal_diff * 100) / ($month_goal > 0 ? $month_goal : 1), 2);
}
$stream_values['less']['traffic'] = $value;
$stream_values['more']['traffic'] = $value;
if ($value > 0 && $value < $month_goal) {
$stream_values['less']['goal_diff'] = $goal_diff;
} elseif ($value > 0) {
$stream_values['more']['goal_diff'] = $goal_diff;
}
if ($value == 0) {
$streams['traffic']['less']['zone_text'] = $this->trans('Goal set:', [], 'Modules.Dashgoals.Admin');
$stream_values['less']['goal'] = $month_goal;
}
foreach ($stream_zones as $stream_zone) {
$streams['traffic'][$stream_zone['zone']]['values'][] = $stream_values[$stream_zone['zone']];
}
$month_goal = (float) ConfigurationKPI::get('DASHGOALS_CONVERSION_' . $i . '_' . $year);
$value = 100 * ((isset($visits[$timestamp]) && $visits[$timestamp] && isset($orders[$timestamp]) && $orders[$timestamp]) ? ($orders[$timestamp] / $visits[$timestamp]) : 0);
$stream_values = $this->getValuesFromGoals($average_goals['conversion'] * 100, $month_goal, $value, self::$month_labels[$i]);
$goal_diff = $value - (int) $month_goal;
$stream_values['real']['conversion'] = round($value, 2);
$stream_values['real']['goal'] = round($month_goal, 2);
if ($value > 0) {
$stream_values['real']['goal_diff'] = round(($goal_diff * 100) / ($month_goal > 0 ? $month_goal : 1), 2);
}
$stream_values['less']['conversion'] = $value;
$stream_values['more']['conversion'] = $value;
if ($value > 0 && $value < $month_goal) {
$stream_values['less']['goal_diff'] = round(($goal_diff * 100) / ($month_goal > 0 ? $month_goal : 1), 2);
} elseif ($value > 0) {
$stream_values['more']['goal_diff'] = round(($goal_diff * 100) / ($month_goal > 0 ? $month_goal : 1), 2);
}
if ($value == 0) {
$streams['conversion']['less']['zone_text'] = $this->trans('Goal set:', [], 'Modules.Dashgoals.Admin');
$stream_values['less']['goal'] = $month_goal;
}
foreach ($stream_zones as $stream_zone) {
$streams['conversion'][$stream_zone['zone']]['values'][] = $stream_values[$stream_zone['zone']];
}
$month_goal = (int) ConfigurationKPI::get('DASHGOALS_AVG_CART_VALUE_' . $i . '_' . $year);
$value = ((isset($orders[$timestamp]) && $orders[$timestamp] && isset($sales[$timestamp]) && $sales[$timestamp]) ? ($sales[$timestamp] / $orders[$timestamp]) : 0);
$stream_values = $this->getValuesFromGoals($average_goals['avg_cart_value'], $month_goal, $value, self::$month_labels[$i]);
$goal_diff = $value - $month_goal;
$stream_values['real']['avg_cart_value'] = $value;
$stream_values['real']['goal'] = $month_goal;
if ($value > 0) {
$stream_values['real']['goal_diff'] = round(($goal_diff * 100) / ($month_goal > 0 ? $month_goal : 1), 2);
}
$stream_values['less']['avg_cart_value'] = $value;
$stream_values['more']['avg_cart_value'] = $value;
if ($value > 0 && $value < $month_goal) {
$stream_values['less']['goal_diff'] = $goal_diff;
} elseif ($value > 0) {
$stream_values['more']['goal_diff'] = $goal_diff;
}
if ($value == 0) {
$streams['avg_cart_value']['less']['zone_text'] = $this->trans('Goal set:', [], 'Modules.Dashgoals.Admin');
$stream_values['less']['goal'] = $month_goal;
}
foreach ($stream_zones as $stream_zone) {
$streams['avg_cart_value'][$stream_zone['zone']]['values'][] = $stream_values[$stream_zone['zone']];
}
$month_goal = (int) ConfigurationKPI::get('DASHGOALS_TRAFFIC_' . $i . '_' . $year)
* (float) ConfigurationKPI::get('DASHGOALS_CONVERSION_' . $i . '_' . $year)
/ 100
* (int) ConfigurationKPI::get('DASHGOALS_AVG_CART_VALUE_' . $i . '_' . $year);
$value = (isset($sales[$timestamp]) && $sales[$timestamp]) ? $sales[$timestamp] : 0;
$stream_values = $this->getValuesFromGoals($average_goals['sales'], $month_goal, isset($sales[$timestamp]) ? $sales[$timestamp] : 0, self::$month_labels[$i]);
$goal_diff = $value - $month_goal;
$stream_values['real']['sales'] = $value;
$stream_values['real']['goal'] = $month_goal;
if ($value > 0) {
$stream_values['real']['goal_diff'] = round(($goal_diff * 100) / ($month_goal > 0 ? $month_goal : 1), 2);
}
$stream_values['less']['sales'] = $value;
$stream_values['more']['sales'] = $value;
if ($value > 0 && $value < $month_goal) {
$stream_values['less']['goal_diff'] = $goal_diff;
} elseif ($value > 0) {
$stream_values['more']['goal_diff'] = $goal_diff;
}
if ($value == 0) {
$streams['sales']['less']['zone_text'] = $this->trans('Goal set:', [], 'Modules.Dashgoals.Admin');
$stream_values['less']['goal'] = $month_goal;
}
foreach ($stream_zones as $stream_zone) {
$streams['sales'][$stream_zone['zone']]['values'][] = $stream_values[$stream_zone['zone']];
}
}
}
// Merge all the streams before sending
$all_streams = [];
foreach ($stream_types as $stream_type) {
foreach ($stream_zones as $stream_zone) {
$all_streams[] = $streams[$stream_type['type']][$stream_zone['zone']];
}
}
return ['chart_type' => 'bar_chart_goals', 'data' => $all_streams];
}
protected function getValuesFromGoals($average_goal, $month_goal, $value, $label)
{
// Initialize value for each zone
$stream_values = [
'real' => ['x' => $label, 'y' => 0],
'less' => ['x' => $label, 'y' => 0],
'more' => ['x' => $label, 'y' => 0],
];
// Calculate the percentage of fullfilment of the goal
$fullfilment = 0;
if ($value && $month_goal) {
$fullfilment = round($value / $month_goal, 2);
}
// Base rate is essential here : it determines the value of the goal compared to the "100%" of the chart legend
$base_rate = 0;
if ($average_goal && $month_goal) {
$base_rate = $month_goal / $average_goal;
}
// Fullfilment of 1 means that we performed exactly anticipated
if ($fullfilment == 1) {
$stream_values['real'] = ['x' => $label, 'y' => round($base_rate, 2)];
}
// Fullfilment lower than 1 means that we UNDER performed
elseif ($fullfilment < 1) {
$stream_values['real'] = ['x' => $label, 'y' => round($fullfilment * $base_rate, 2)];
$stream_values['less'] = ['x' => $label, 'y' => round($base_rate - ($fullfilment * $base_rate), 2)];
}
// Fullfilment greater than 1 means that we OVER performed
elseif ($fullfilment > 1) {
$stream_values['real'] = ['x' => $label, 'y' => round($base_rate, 2)];
$stream_values['more'] = ['x' => $label, 'y' => round(($fullfilment * $base_rate) - $base_rate, 2)];
}
return $stream_values;
}
}