Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 15945 | Rev 16130 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
15540 efrain 1
<?php
15808 anderson 2
 
15540 efrain 3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Controller;
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
8
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
9
use Laminas\Mvc\Controller\AbstractActionController;
10
use Laminas\Log\LoggerInterface;
11
use Laminas\View\Model\ViewModel;
12
use Laminas\View\Model\JsonModel;
13
use LeadersLinked\Library\Functions;
14
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
15
use LeadersLinked\Mapper\CompanyMapper;
16
use LeadersLinked\Mapper\DailyPulseEmojiMapper;
17
use LeadersLinked\Form\DailyPulse\DailyPulseAddEmojiForm;
18
use LeadersLinked\Form\DailyPulse\DailyPulseEditEmojiForm;
19
use LeadersLinked\Model\DailyPulseEmoji;
20
use LeadersLinked\Library\Image;
21
use LeadersLinked\Mapper\DailyPulseRecordMapper;
22
use LeadersLinked\Model\DailyPulseRecord;
23
use PhpOffice\PhpSpreadsheet\IOFactory;
24
use PhpOffice\PhpSpreadsheet\Spreadsheet;
25
 
26
class DailyPulseReportsController extends AbstractActionController
27
{
28
    /**
29
     *
30
     * @var AdapterInterface
31
     */
32
    private $adapter;
15808 anderson 33
 
34
 
15540 efrain 35
    /**
36
     *
37
     * @var AbstractAdapter
38
     */
39
    private $cache;
15808 anderson 40
 
15540 efrain 41
    /**
42
     *
43
     * @var  LoggerInterface
44
     */
45
    private $logger;
15808 anderson 46
 
47
 
15540 efrain 48
    /**
49
     *
50
     * @var array
51
     */
52
    private $config;
15808 anderson 53
 
15540 efrain 54
    /**
55
     *
56
     * @param AdapterInterface $adapter
57
     * @param AbstractAdapter $cache
58
     * @param LoggerInterface $logger
59
     * @param array $config
60
     */
15808 anderson 61
    public function __construct($adapter, $cache, $logger, $config)
15540 efrain 62
    {
63
        $this->adapter      = $adapter;
64
        $this->cache        = $cache;
65
        $this->logger       = $logger;
66
        $this->config       = $config;
67
    }
15808 anderson 68
 
15540 efrain 69
    public function indexAction()
70
    {
71
        return new JsonModel([
72
            'success' => false,
73
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
74
        ]);
75
    }
15808 anderson 76
 
15540 efrain 77
    public function overviewAction()
78
    {
79
        $currentUserPlugin = $this->plugin('currentUserPlugin');
80
        $currentUser = $currentUserPlugin->getUser();
15808 anderson 81
 
15540 efrain 82
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
83
        $currentNetwork = $currentNetworkPlugin->getNetwork();
15808 anderson 84
 
15540 efrain 85
        $companyMapper = CompanyMapper::getInstance($this->adapter);
86
        $company = $companyMapper->fetchDefaultForNetworkByNetworkId($currentNetwork->id);
15808 anderson 87
 
15540 efrain 88
        $request = $this->getRequest();
16129 anderson 89
        //if ($request->isGet()) {
15808 anderson 90
 
16129 anderson 91
        $headers  = $request->getHeaders();
15808 anderson 92
 
16129 anderson 93
        $isJson = false;
94
        if ($headers->has('Accept')) {
95
            $accept = $headers->get('Accept');
15808 anderson 96
 
16129 anderson 97
            $prioritized = $accept->getPrioritized();
15808 anderson 98
 
16129 anderson 99
            foreach ($prioritized as $key => $value) {
100
                $raw = trim($value->getRaw());
15808 anderson 101
 
16129 anderson 102
                if (!$isJson) {
103
                    $isJson = strpos($raw, 'json');
15540 efrain 104
                }
105
            }
16129 anderson 106
        }
15546 efrain 107
 
15808 anderson 108
 
16129 anderson 109
        //if ($isJson) {
15808 anderson 110
 
16129 anderson 111
        $startDate = $this->params()->fromQuery('startDate');
112
        if (empty($startDate)) {
113
            $startDate = date('Y-m-d');
114
        }
15808 anderson 115
 
116
 
16129 anderson 117
        $endDate = $this->params()->fromQuery('endDate');
118
        if (empty($endDate)) {
119
            $endDate = date('Y-m-d');
120
        }
15808 anderson 121
 
16129 anderson 122
        $startDate = '2023-03-01';
123
        $endDate = '2023-03-19';
15808 anderson 124
 
16129 anderson 125
        $dtStartDate = \DateTime::createFromFormat('Y-n-d', $startDate);
126
        $dtEndDate = \DateTime::createFromFormat('Y-n-d', $endDate);
15808 anderson 127
 
16129 anderson 128
        if (!$dtStartDate || !$dtEndDate) {
129
            $startDate = date('Y-m-d');
130
            $endDate = date('Y-m-d');
131
        } else {
15945 anderson 132
 
16129 anderson 133
            if ($dtStartDate->getTimestamp() > $dtEndDate->getTimestamp()) {
134
                $startDate = date('Y-m-d');
135
                $endDate = date('Y-m-d');
136
            }
137
        }
15808 anderson 138
 
16129 anderson 139
        $dailyPulseRecordMapper = DailyPulseRecordMapper::getInstance($this->adapter);
140
        $how_are_you_feel = $dailyPulseRecordMapper->fetchAllDataChartOrExcelByCompanyIdAndTypeAndDateRange($company->id, DailyPulseRecord::TYPE_HOW_ARE_YOU_FEEL, $startDate, $endDate);
15808 anderson 141
 
16129 anderson 142
        $climate_on_your_organization = $dailyPulseRecordMapper->fetchAllDataChartOrExcelByCompanyIdAndTypeAndDateRange($company->id, DailyPulseRecord::TYPE_CLIMATE_ON_YOUR_ORGANIZATION, $startDate, $endDate);
15540 efrain 143
 
15808 anderson 144
 
145
 
16129 anderson 146
        $data = [
147
            'labels'             => [],
148
            'users' => [
149
                'how_are_you_feel' => [],
150
                'climate_on_your_organization' => [],
151
            ],
152
            'points' => [
153
                'how_are_you_feel' => [],
154
                'climate_on_your_organization' => [],
155
            ],
156
            'average' => [
157
                'how_are_you_feel' => [],
158
                'climate_on_your_organization' => [],
159
            ],
160
            'how_are_you_feel' => [
161
                'average_points'     => 0,
162
                'average_users'      => 0,
163
            ],
164
            'climate_on_your_organization' => [
165
                'average_points'    => 0,
166
                'average_users'     => 0,
167
            ]
15808 anderson 168
 
169
 
16129 anderson 170
        ];
15544 efrain 171
 
15808 anderson 172
 
173
 
16129 anderson 174
        $how_are_you_feel_points = 0;
175
        $how_are_you_feel_users = 0;
15808 anderson 176
 
16129 anderson 177
        $climate_on_your_organization_points = 0;
178
        $climate_on_your_organization_users = 0;
15808 anderson 179
 
16129 anderson 180
        $count = 0;
15540 efrain 181
 
182
 
16129 anderson 183
        $dt = \DateTime::createFromFormat('Y-m-d', $startDate);
184
        do {
185
            $count++;
186
            $date = $dt->format('Y-m-d');
187
            $label = $dt->format('d/m/Y');
15544 efrain 188
 
16129 anderson 189
            array_push($data['labels'], $label);
15808 anderson 190
 
191
 
16129 anderson 192
            $points = 0;
193
            $users = 0;
194
            foreach ($how_are_you_feel as $record) {
195
                if ($date == $record['date']) {
196
                    $points = $record['points'];
197
                    $users = $record['users'];
198
                }
199
            }
15808 anderson 200
 
201
 
202
 
16129 anderson 203
            array_push($data['points']['how_are_you_feel'], $points);
204
            array_push($data['users']['how_are_you_feel'], $users);
205
            array_push($data['average']['how_are_you_feel'], $users ?  $points / $users : 0);
15808 anderson 206
 
207
 
16129 anderson 208
            $how_are_you_feel_points += $points;
209
            $how_are_you_feel_users += $users;
15540 efrain 210
 
16129 anderson 211
            $points = 0;
212
            $users = 0;
213
            foreach ($climate_on_your_organization as $record) {
214
                if ($date == $record['date']) {
215
                    $points = $record['points'];
216
                    $users = $record['users'];
217
                }
218
            }
15808 anderson 219
 
220
 
16129 anderson 221
            array_push($data['points']['climate_on_your_organization'], $points);
222
            array_push($data['users']['climate_on_your_organization'], $users);
223
            array_push($data['average']['climate_on_your_organization'], $users ?  $points / $users : 0);
15808 anderson 224
 
225
 
16129 anderson 226
            $climate_on_your_organization_points += $points;
227
            $climate_on_your_organization_users += $users;
15808 anderson 228
 
229
 
16129 anderson 230
            $dt->add(new \DateInterval('P1D'));
231
        } while ($date < $endDate);
15808 anderson 232
 
233
 
234
 
235
 
16129 anderson 236
        $data['how_are_you_feel']['average_points'] =  number_format($how_are_you_feel_points / $count, 2);
237
        $data['how_are_you_feel']['average_users'] = number_format($how_are_you_feel_users / $count, 2);
15808 anderson 238
 
16129 anderson 239
        $data['climate_on_your_organization']['average_points'] = number_format($climate_on_your_organization_points / $count, 2);
240
        $data['climate_on_your_organization']['average_users'] = number_format($climate_on_your_organization_users / $count, 2);
15540 efrain 241
 
15546 efrain 242
 
15808 anderson 243
 
244
 
245
 
16129 anderson 246
        return new JsonModel([
247
            'success' => true,
248
            'data' => $data,
249
        ]);
250
        // } else {
251
        //     $this->layout()->setTemplate('layout/layout-backend');
252
        //     $viewModel = new ViewModel();
253
        //     $viewModel->setTemplate('leaders-linked/daily-pulse-reports/overview.phtml');
254
        //     return $viewModel;
255
        // }
256
        // } else {
15945 anderson 257
 
258
 
16129 anderson 259
        //     return new JsonModel([
260
        //         'success' => false,
261
        //         'data' => 'ERROR_METHOD_NOT_ALLOWED'
262
        //     ]);
263
        // }
15540 efrain 264
    }
15808 anderson 265
 
15540 efrain 266
    public function overviewDownloadAction()
267
    {
268
        $currentUserPlugin = $this->plugin('currentUserPlugin');
269
        $currentUser = $currentUserPlugin->getUser();
15808 anderson 270
 
15540 efrain 271
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
272
        $currentNetwork = $currentNetworkPlugin->getNetwork();
15808 anderson 273
 
15540 efrain 274
        $companyMapper = CompanyMapper::getInstance($this->adapter);
275
        $company = $companyMapper->fetchDefaultForNetworkByNetworkId($currentNetwork->id);
15808 anderson 276
 
15540 efrain 277
        $request = $this->getRequest();
15808 anderson 278
 
279
        if ($request->isGet()) {
280
 
15540 efrain 281
            $startDate = $this->params()->fromQuery('startDate');
15808 anderson 282
            if (empty($startDate)) {
15540 efrain 283
                $startDate = date('Y-m-d');
284
            }
15808 anderson 285
 
286
 
15540 efrain 287
            $endDate = $this->params()->fromQuery('endDate');
15808 anderson 288
            if (empty($startDate)) {
15540 efrain 289
                $endDate = date('Y-m-d');
290
            }
15808 anderson 291
 
15541 efrain 292
            //$startDate = '2023-03-01';
293
            //$endDate = '2023-03-19';
15808 anderson 294
 
15540 efrain 295
            $dtStartDate = \DateTime::createFromFormat('Y-n-d', $startDate);
296
            $dtEndDate = \DateTime::createFromFormat('Y-n-d', $endDate);
15808 anderson 297
 
298
            if (!$dtStartDate || !$dtEndDate) {
15540 efrain 299
                $startDate = date('Y-m-d');
300
                $endDate = date('Y-m-d');
301
            } else {
15808 anderson 302
 
303
                if ($dtStartDate->getTimestamp() > $dtEndDate->getTimestamp()) {
15540 efrain 304
                    $startDate = date('Y-m-d');
305
                    $endDate = date('Y-m-d');
306
                }
307
            }
15808 anderson 308
 
15540 efrain 309
            $dailyPulseRecordMapper = DailyPulseRecordMapper::getInstance($this->adapter);
310
            $how_are_you_feel = $dailyPulseRecordMapper->fetchAllDataChartOrExcelByCompanyIdAndTypeAndDateRange($company->id, DailyPulseRecord::TYPE_HOW_ARE_YOU_FEEL, $startDate, $endDate);
15808 anderson 311
 
15540 efrain 312
            $climate_on_your_organization = $dailyPulseRecordMapper->fetchAllDataChartOrExcelByCompanyIdAndTypeAndDateRange($company->id, DailyPulseRecord::TYPE_CLIMATE_ON_YOUR_ORGANIZATION, $startDate, $endDate);
15808 anderson 313
 
314
 
315
 
15540 efrain 316
            $data = [
15544 efrain 317
                'labels'             => [],
15545 efrain 318
                'average' => [
319
                    'how_are_you_feel' => [],
320
                    'climate_on_your_organization' => [],
321
                ],
15544 efrain 322
                'users' => [
323
                    'how_are_you_feel' => [],
324
                    'climate_on_your_organization' => [],
325
                ],
326
                'points' => [
327
                    'how_are_you_feel' => [],
328
                    'climate_on_your_organization' => [],
329
                ],
15540 efrain 330
                'how_are_you_feel' => [
15544 efrain 331
                    'average_points'     => 0,
332
                    'average_users'      => 0,
15540 efrain 333
                ],
334
                'climate_on_your_organization' => [
335
                    'average_points'    => 0,
336
                    'average_users'     => 0,
337
                ]
15808 anderson 338
 
339
 
15540 efrain 340
            ];
15808 anderson 341
 
342
 
343
 
15540 efrain 344
            $how_are_you_feel_points = 0;
345
            $how_are_you_feel_users = 0;
15808 anderson 346
 
15540 efrain 347
            $climate_on_your_organization_points = 0;
348
            $climate_on_your_organization_users = 0;
15808 anderson 349
 
15540 efrain 350
            $count = 0;
15808 anderson 351
 
352
 
15540 efrain 353
            $dt = \DateTime::createFromFormat('Y-m-d', $startDate);
354
            do {
355
                $count++;
356
                $date = $dt->format('Y-m-d');
357
                $label = $dt->format('d/m/Y');
15808 anderson 358
 
15544 efrain 359
                array_push($data['labels'], $label);
360
 
15808 anderson 361
 
15544 efrain 362
                $points = 0;
363
                $users = 0;
15808 anderson 364
                foreach ($how_are_you_feel as $record) {
365
                    if ($date == $record['date']) {
15544 efrain 366
                        $points = $record['points'];
367
                        $users = $record['users'];
15540 efrain 368
                    }
369
                }
15808 anderson 370
 
371
 
372
 
15544 efrain 373
                array_push($data['points']['how_are_you_feel'], $points);
374
                array_push($data['users']['how_are_you_feel'], $users);
15545 efrain 375
                array_push($data['average']['how_are_you_feel'], $users ?  $points / $users : 0);
15808 anderson 376
 
377
 
15544 efrain 378
                $how_are_you_feel_points += $points;
379
                $how_are_you_feel_users += $users;
15808 anderson 380
 
15544 efrain 381
                $points = 0;
382
                $users = 0;
15808 anderson 383
                foreach ($climate_on_your_organization as $record) {
384
                    if ($date == $record['date']) {
15544 efrain 385
                        $points = $record['points'];
386
                        $users = $record['users'];
15540 efrain 387
                    }
388
                }
15808 anderson 389
 
390
 
15544 efrain 391
                array_push($data['points']['climate_on_your_organization'], $points);
392
                array_push($data['users']['climate_on_your_organization'], $users);
15545 efrain 393
                array_push($data['average']['climate_on_your_organization'], $users ?  $points / $users : 0);
15808 anderson 394
 
395
 
15544 efrain 396
                $climate_on_your_organization_points += $points;
397
                $climate_on_your_organization_users += $users;
15808 anderson 398
 
399
 
15540 efrain 400
                $dt->add(new \DateInterval('P1D'));
15808 anderson 401
            } while ($date < $endDate);
402
 
15540 efrain 403
            $data['how_are_you_feel']['average_points'] =  number_format($how_are_you_feel_points / $count, 2);
404
            $data['how_are_you_feel']['average_users'] = number_format($how_are_you_feel_users / $count, 2);
15808 anderson 405
 
15540 efrain 406
            $data['climate_on_your_organization']['average_points'] = number_format($climate_on_your_organization_points / $count, 2);
407
            $data['climate_on_your_organization']['average_users'] = number_format($climate_on_your_organization_users / $count, 2);
15808 anderson 408
 
409
 
410
 
411
 
412
 
15540 efrain 413
            $spreadsheet = new Spreadsheet();
414
            $spreadsheet->getProperties()->setTitle('Pulso Diario');
15808 anderson 415
 
416
 
417
 
15540 efrain 418
            $spreadsheet->setActiveSheetIndex(0);
419
            $dt = \DateTime::createFromFormat('Y-m-d', $startDate);
15808 anderson 420
 
15540 efrain 421
            $spreadsheet->getActiveSheet()->SetCellValue('A1', 'Desde:');
422
            $spreadsheet->getActiveSheet()->SetCellValue('B1', $dt->format('d/m/Y'));
15808 anderson 423
 
424
 
15540 efrain 425
            $dt = \DateTime::createFromFormat('Y-m-d', $startDate);
426
            $spreadsheet->getActiveSheet()->SetCellValue('C1', 'Hasta:');
427
            $spreadsheet->getActiveSheet()->SetCellValue('D1', $dt->format('d/m/Y'));
15808 anderson 428
 
429
 
15540 efrain 430
            $spreadsheet->getActiveSheet()->SetCellValue('A3', 'Como te sientes hoy');
15544 efrain 431
            $spreadsheet->getActiveSheet()->setMergeCells(['A3', 'B3', 'C3', 'D3']);
15540 efrain 432
            $spreadsheet->getActiveSheet()->SetCellValue('A4', 'Puntos promedios');
433
            $spreadsheet->getActiveSheet()->SetCellValue('B4',  $data['how_are_you_feel']['average_points']);
434
            $spreadsheet->getActiveSheet()->SetCellValue('C4', 'Usuarios promedios');
435
            $spreadsheet->getActiveSheet()->SetCellValue('D4',  $data['how_are_you_feel']['average_users']);
15808 anderson 436
 
437
 
15540 efrain 438
            $spreadsheet->getActiveSheet()->SetCellValue('F3', 'Clima en su organización');
15544 efrain 439
            $spreadsheet->getActiveSheet()->setMergeCells(['F3', 'G3', 'H3', 'I3']);
15540 efrain 440
            $spreadsheet->getActiveSheet()->SetCellValue('F4', 'Puntos promedios');
441
            $spreadsheet->getActiveSheet()->SetCellValue('G4',  $data['climate_on_your_organization']['average_points']);
442
            $spreadsheet->getActiveSheet()->SetCellValue('H4', 'Usuarios promedios');
443
            $spreadsheet->getActiveSheet()->SetCellValue('I4',  $data['climate_on_your_organization']['average_users']);
15808 anderson 444
 
445
 
15544 efrain 446
            $spreadsheet->getActiveSheet()->SetCellValue('A6', 'Usuarios');
447
            $spreadsheet->getActiveSheet()->setMergeCells(['A6', 'B6', 'C6']);
448
            $spreadsheet->getActiveSheet()->SetCellValue('A7', 'Fecha');
449
            $spreadsheet->getActiveSheet()->SetCellValue('B7', 'Puntos promedios');
450
            $spreadsheet->getActiveSheet()->SetCellValue('C7', 'Cantidad de usuarios');
15808 anderson 451
 
15544 efrain 452
            $row = 8;
453
            $max = count($data['labels']);
15808 anderson 454
 
455
            for ($i = 0; $i < $max; $i++) {
15544 efrain 456
                $spreadsheet->getActiveSheet()->SetCellValue('A' . $row, $data['labels'][$i]);
457
                $spreadsheet->getActiveSheet()->SetCellValue('B' . $row, $data['users']['how_are_you_feel'][$i]);
458
                $spreadsheet->getActiveSheet()->SetCellValue('C' . $row, $data['users']['climate_on_your_organization'][$i]);
459
                $row++;
15540 efrain 460
            }
15544 efrain 461
 
15808 anderson 462
 
15544 efrain 463
            $spreadsheet->getActiveSheet()->SetCellValue('F6', 'Puntos');
464
            $spreadsheet->getActiveSheet()->setMergeCells(['F6', 'G6', 'H6']);
465
            $spreadsheet->getActiveSheet()->SetCellValue('F7', 'Fecha');
466
            $spreadsheet->getActiveSheet()->SetCellValue('G7', 'Puntos promedios');
467
            $spreadsheet->getActiveSheet()->SetCellValue('H7', 'Cantidad de usuarios');
468
 
15808 anderson 469
 
470
 
15544 efrain 471
            $row = 8;
472
            $max = count($data['labels']);
15808 anderson 473
 
474
            for ($i = 0; $i < $max; $i++) {
15544 efrain 475
                $spreadsheet->getActiveSheet()->SetCellValue('F' . $row, $data['labels'][$i]);
476
                $spreadsheet->getActiveSheet()->SetCellValue('G' . $row, $data['points']['how_are_you_feel'][$i]);
477
                $spreadsheet->getActiveSheet()->SetCellValue('H' . $row, $data['points']['climate_on_your_organization'][$i]);
478
                $row++;
15540 efrain 479
            }
15808 anderson 480
 
15545 efrain 481
            $spreadsheet->getActiveSheet()->SetCellValue('J6', 'Puntos promedios ( puntos / usuarios ) ');
482
            $spreadsheet->getActiveSheet()->setMergeCells(['J6', 'K6', 'L6']);
483
            $spreadsheet->getActiveSheet()->SetCellValue('J7', 'Fecha');
484
            $spreadsheet->getActiveSheet()->SetCellValue('K7', 'Puntos promedios');
485
            $spreadsheet->getActiveSheet()->SetCellValue('L7', 'Cantidad de usuarios');
15808 anderson 486
 
487
 
488
 
15545 efrain 489
            $row = 8;
490
            $max = count($data['labels']);
15808 anderson 491
 
492
            for ($i = 0; $i < $max; $i++) {
15545 efrain 493
                $spreadsheet->getActiveSheet()->SetCellValue('J' . $row, $data['labels'][$i]);
494
                $spreadsheet->getActiveSheet()->SetCellValue('K' . $row, number_format($data['average']['how_are_you_feel'][$i], 2));
495
                $spreadsheet->getActiveSheet()->SetCellValue('L' . $row, number_format($data['average']['climate_on_your_organization'][$i], 2));
496
                $row++;
497
            }
15808 anderson 498
 
499
 
15540 efrain 500
            $fileName = 'reporte_pulso_diario_'  . date('d-m-Y-h-i-a', time()) . '.xls';
501
            $tempFilename = tempnam(sys_get_temp_dir(), 'reporte_pulso_diario_' . time());
15808 anderson 502
 
15540 efrain 503
            $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
504
            $writer->save($tempFilename);
15808 anderson 505
 
15540 efrain 506
            $content = file_get_contents($tempFilename);
507
            @unlink($tempFilename);
15808 anderson 508
 
15540 efrain 509
            return new JsonModel([
510
                'success' => true,
511
                'data' => [
512
                    'content' => base64_encode($content),
513
                    'basename' => $fileName
15808 anderson 514
 
15540 efrain 515
                ]
516
            ]);
517
        } else {
518
            return new JsonModel([
519
                'success' => false,
520
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
521
            ]);
522
        }
523
    }
524
}