Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev Autor Línea Nro. Línea
15670 anderson 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Controller;
6
 
16758 efrain 7
 
8
use Laminas\Db\Adapter\AdapterInterface;
16766 efrain 9
use LeadersLinked\Cache\CacheInterface;
16758 efrain 10
use Laminas\Mvc\Controller\AbstractActionController;
11
use Laminas\Log\LoggerInterface;
12
use Laminas\Mvc\I18n\Translator;
13
 
15682 anderson 14
use Laminas\View\Model\JsonModel;
15
use Laminas\View\Model\ViewModel;
15718 anderson 16
use LeadersLinked\Mapper\UserMapper;
17
use LeadersLinked\Mapper\QueryMapper;
18
use LeadersLinked\Mapper\DiscoveryContactMapper;
16758 efrain 19
use Laminas\Db\Sql\Expression;
20
use LeadersLinked\Mapper\DiscoveryContactInteractionMapper;
21
use LeadersLinked\Mapper\DiscoveryContactInteractionTypeMapper;
22
use PhpOffice\PhpSpreadsheet\IOFactory;
23
use PhpOffice\PhpSpreadsheet\Spreadsheet;
15670 anderson 24
 
16758 efrain 25
 
15670 anderson 26
// Create an action controller.
27
class DiscoveryContactProgressController extends AbstractActionController
28
{
15682 anderson 29
    /**
30
     *
31
     * @var AdapterInterface
32
     */
33
    private $adapter;
16758 efrain 34
 
35
 
15682 anderson 36
    /**
37
     *
16766 efrain 38
     * @var CacheInterface
15682 anderson 39
     */
40
    private $cache;
16758 efrain 41
 
15682 anderson 42
    /**
43
     *
44
     * @var  LoggerInterface
45
     */
46
    private $logger;
16758 efrain 47
 
15682 anderson 48
    /**
49
     *
50
     * @var array
51
     */
52
    private $config;
16758 efrain 53
 
15682 anderson 54
    /**
55
     *
16758 efrain 56
     * @var Translator
57
     */
58
    private $translator;
59
 
60
    /**
61
     *
15682 anderson 62
     * @param AdapterInterface $adapter
16766 efrain 63
     * @param CacheInterface $cache
15682 anderson 64
     * @param LoggerInterface $logger
65
     * @param array $config
16758 efrain 66
     * @param Translator $translator;
15682 anderson 67
     */
16758 efrain 68
    public function __construct($adapter, $cache, $logger, $config, $translator)
15682 anderson 69
    {
70
        $this->adapter      = $adapter;
71
        $this->cache        = $cache;
72
        $this->logger       = $logger;
73
        $this->config       = $config;
16758 efrain 74
        $this->translator   = $translator;
15682 anderson 75
    }
76
 
15670 anderson 77
 
15678 anderson 78
 
15679 anderson 79
 
15788 anderson 80
    public function indexAction()
81
    {
15792 anderson 82
        $currentUserPlugin = $this->plugin('currentUserPlugin');
16758 efrain 83
        $currentCompany = $currentUserPlugin->getCompany();
84
 
15796 anderson 85
        $request = $this->getRequest();
15793 anderson 86
 
16305 anderson 87
        if ($request->isGet()) {
88
            $headers  = $request->getHeaders();
89
            $isJson = false;
15795 anderson 90
 
16305 anderson 91
            if ($headers->has('Accept')) {
92
                $accept = $headers->get('Accept');
15796 anderson 93
 
16305 anderson 94
                $prioritized = $accept->getPrioritized();
15796 anderson 95
 
16305 anderson 96
                foreach ($prioritized as $key => $value) {
97
                    $raw = trim($value->getRaw());
15796 anderson 98
 
16305 anderson 99
                    if (!$isJson) {
100
                        $isJson = strpos($raw, 'json');
101
                    }
15796 anderson 102
                }
103
            }
16758 efrain 104
 
105
 
106
            //$isJson =  true;
16305 anderson 107
            if ($isJson) {
16241 anderson 108
 
16305 anderson 109
                $startDate = $this->params()->fromQuery('startDate');
110
                if (empty($startDate)) {
111
                    $startDate = date('Y-m-d');
112
                }
15798 anderson 113
 
16305 anderson 114
                $endDate = $this->params()->fromQuery('endDate');
115
                if (empty($endDate)) {
116
                    $endDate = date('Y-m-d');
117
                }
16758 efrain 118
 
16305 anderson 119
                $dtStartDate = \DateTime::createFromFormat('Y-n-d', $startDate);
120
                $dtEndDate = \DateTime::createFromFormat('Y-n-d', $endDate);
16758 efrain 121
 
15800 anderson 122
 
16758 efrain 123
 
16305 anderson 124
                if (!$dtStartDate || !$dtEndDate) {
125
                    $startDate = date('Y-m-d');
126
                    $endDate = date('Y-m-d');
127
                } else {
16125 anderson 128
 
16305 anderson 129
                    if ($dtStartDate->getTimestamp() > $dtEndDate->getTimestamp()) {
130
                        $startDate = date('Y-m-d');
131
                        $endDate = date('Y-m-d');
132
                    }
16758 efrain 133
 
16305 anderson 134
                }
16758 efrain 135
 
136
                //echo ' $startDate  = ' .  $startDate  . ' $endDate = ' . $endDate; exit;
137
 
138
                $graph = [];
139
 
15806 anderson 140
 
16758 efrain 141
                $dt = \DateTime::createFromFormat('Y-m-d', $startDate);
142
 
143
                do {
144
                    $tempDate = $dt->format('Y-m-d');
145
                    $graph[ $tempDate ] = ['label' => $dt->format('d/m/Y'), 'value' => 0];
146
                    $dt->add(new \DateInterval('P1D'));
147
 
148
 
149
                } while($endDate > $tempDate);
150
 
151
 
152
                $queryMapper = QueryMapper::getInstance($this->adapter);
153
 
154
                /*
155
                select count(*) as cant, date(added_on) as date from tbl_discovery_contacts
156
                where date(added_on) BETWEEN '2023-05-05' AND '2023-05-31'
157
                group by date(added_on)
158
                order by date ASC
159
                 */
160
 
161
                $select = $queryMapper->getSql()->select(DiscoveryContactMapper::_TABLE);
162
                $select->columns([
163
                    'total' => new Expression('COUNT(*)'),
164
                    'date' => new Expression('DATE(added_on)'),
165
                ]);
166
                $select->where->between( new Expression('DATE(added_on)') , $startDate,  $endDate);
167
                $select->group(new Expression('DATE(added_on)'));
168
                $select->order('date ASC');
169
 
170
                $records = $queryMapper->fetchAll($select);
171
                foreach($records as $record)
172
                {
16558 anderson 173
 
16758 efrain 174
                    $graph[ $record['date']  ][ 'value' ] = $record['total'];
16552 anderson 175
                }
16758 efrain 176
 
177
                $discoveryContactInteractionTypeMapper = DiscoveryContactInteractionTypeMapper::getInstance($this->adapter);
178
                $discoveryContactInteractionType =  $discoveryContactInteractionTypeMapper->fetchOneDefaultByCompanyId($currentCompany->id);
179
                if(!$discoveryContactInteractionType) {
180
                    $discoveryContactInteractionType = $discoveryContactInteractionTypeMapper->fetchOneFirstActiveByCompanyId($currentCompany->id);
181
                }
182
 
183
 
184
                /*
185
                  select count(*), user_id, date(added_on) as date from tbl_discovery_contact_interactions
186
                where interaction_type_id = 1 and date(added_on) BETWEEN '2023-05-05' AND '2023-05-31'
187
                group by date(added_on), user_id
188
                order by date
189
                */
190
 
191
                $table = [];
192
                $select = $queryMapper->getSql()->select(DiscoveryContactInteractionMapper::_TABLE);
193
                $select->columns([
194
                    'user_id',
195
                    'total' => new Expression('COUNT(*)'),
196
                    'date' => new Expression('DATE(added_on)'),
197
                ]);
198
                $select->where->equalTo('interaction_type_id', $discoveryContactInteractionType->id);
199
                $select->where->between( new Expression('DATE(added_on)') , $startDate,  $endDate);
200
                $select->group([ new Expression('DATE(added_on)'), 'user_id']);
201
                $select->order('date ASC');
202
 
203
                $records = $queryMapper->fetchAll($select);
204
                foreach($records as $record)
205
                {
206
                    $user_id =  $record['user_id'];
207
 
208
                    if(!isset( $table[ $user_id  ])) {
209
 
210
                        $items = [];
211
 
212
                        $dt = \DateTime::createFromFormat('Y-m-d', $startDate);
213
 
214
                        do {
215
                            $tempDate = $dt->format('Y-m-d');
216
                            $items[ $tempDate ] = ['label' => $dt->format('d/m/Y'), 'value' => 0];
217
                            $dt->add(new \DateInterval('P1D'));
218
 
219
 
220
                        } while($endDate > $tempDate);
221
 
222
                        $table[ $user_id  ] = $items;
223
 
224
 
225
                    }
226
 
227
 
228
                    $table[ $user_id ][ $record['date'] ]['value'] = $record['total'];
229
                }
230
 
231
                $records = array_values($graph);
232
 
233
                $labels = [];
234
                $values = [];
235
 
236
 
237
 
238
                foreach($records as $record)
239
                {
240
                    array_push($labels, $record['label']);
241
                    array_push($values, $record['value']);
242
                }
243
 
244
                $graph = [
245
                    'labels' => $labels,
246
                    'values' => $values
16538 anderson 247
                ];
16758 efrain 248
 
249
                $userMapper = UserMapper::getInstance($this->adapter);
250
 
251
 
252
                $first = true;
253
                $labels = [];
254
                $users = [];
255
 
256
                $records = $table;
257
                foreach($records as  $user_id => $items)
258
                {
259
                    $user = $userMapper->fetchOne($user_id);
260
                    if(!$user) {
261
                        continue;
262
                    }
263
 
264
 
265
                    $values = [];
266
                    foreach($items as $item)
267
                    {
268
                        if($first) {
269
                            array_push($labels,['date' =>  $item['label'] ]);
270
                        }
271
                        array_push($values,['value' =>  $item['value'] ]);
272
                    }
273
 
274
                    array_push($users, [
275
                       'first_name' => $user->first_name,
276
                       'last_name' => $user->last_name,
277
                       'email' => $user->email,
278
                       'values' => $values,
279
                    ]);
280
 
281
                    $first = false;
282
 
16305 anderson 283
                }
16758 efrain 284
 
285
 
286
                usort($users, function($a, $b) {
287
 
288
                    $result = $a['first_name'] <=> $b['last_name'];
289
                    if($result === 0) {
290
                        $a['last_name'] <=> $b['last_name'];
291
                    }
292
 
293
                    return $result;
294
 
295
 
296
                });
297
 
298
                $table = [
299
                    'labels' => $labels,
300
                    'users' => $users,
301
                ];
15842 anderson 302
 
16758 efrain 303
 
16305 anderson 304
                return new JsonModel([
305
                    'success' => true,
16758 efrain 306
                    'data' => [
307
                        'graph' => $graph,
308
                        'table' => $table,
309
                    ],
310
 
16305 anderson 311
                ]);
312
            } else {
313
                $this->layout()->setTemplate('layout/layout-backend');
314
                $viewModel = new ViewModel();
315
                $viewModel->setTemplate('leaders-linked/discovery-contact-progress/index.phtml');
316
                return $viewModel;
317
            }
318
        }
16758 efrain 319
 
320
        return new JsonModel([
321
            'success' => false,
322
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
323
        ]);
15670 anderson 324
    }
16758 efrain 325
 
326
    public function downloadAction()
327
    {
328
        $currentUserPlugin = $this->plugin('currentUserPlugin');
329
        $currentCompany = $currentUserPlugin->getCompany();
330
 
331
        $request = $this->getRequest();
332
 
333
        if ($request->isGet()) {
334
            $startDate = $this->params()->fromQuery('startDate');
335
            if (empty($startDate)) {
336
                $startDate = date('Y-m-d');
337
            }
338
 
339
            $endDate = $this->params()->fromQuery('endDate');
340
            if (empty($endDate)) {
341
                $endDate = date('Y-m-d');
342
            }
343
 
344
            $dtStartDate = \DateTime::createFromFormat('Y-n-d', $startDate);
345
            $dtEndDate = \DateTime::createFromFormat('Y-n-d', $endDate);
346
 
347
 
348
 
349
            if (!$dtStartDate || !$dtEndDate) {
350
                $startDate = date('Y-m-d');
351
                $endDate = date('Y-m-d');
352
            } else {
353
 
354
                if ($dtStartDate->getTimestamp() > $dtEndDate->getTimestamp()) {
355
                    $startDate = date('Y-m-d');
356
                    $endDate = date('Y-m-d');
357
                }
358
 
359
            }
360
 
361
            //echo ' $startDate  = ' .  $startDate  . ' $endDate = ' . $endDate; exit;
362
 
363
            $graph = [];
364
 
365
 
366
            $dt = \DateTime::createFromFormat('Y-m-d', $startDate);
367
 
368
            do {
369
                $tempDate = $dt->format('Y-m-d');
370
                $graph[ $tempDate ] = ['label' => $dt->format('d/m/Y'), 'value' => 0];
371
                $dt->add(new \DateInterval('P1D'));
372
 
373
 
374
            } while($endDate > $tempDate);
375
 
376
 
377
            $queryMapper = QueryMapper::getInstance($this->adapter);
378
 
379
            /*
380
             select count(*) as cant, date(added_on) as date from tbl_discovery_contacts
381
             where date(added_on) BETWEEN '2023-05-05' AND '2023-05-31'
382
             group by date(added_on)
383
             order by date ASC
384
             */
385
 
386
            $select = $queryMapper->getSql()->select(DiscoveryContactMapper::_TABLE);
387
            $select->columns([
388
                'total' => new Expression('COUNT(*)'),
389
                'date' => new Expression('DATE(added_on)'),
390
            ]);
391
            $select->where->between( new Expression('DATE(added_on)') , $startDate,  $endDate);
392
            $select->group(new Expression('DATE(added_on)'));
393
            $select->order('date ASC');
394
 
395
            $records = $queryMapper->fetchAll($select);
396
            foreach($records as $record)
397
            {
398
 
399
                $graph[ $record['date']  ][ 'value' ] = $record['total'];
400
            }
401
 
402
            $discoveryContactInteractionTypeMapper = DiscoveryContactInteractionTypeMapper::getInstance($this->adapter);
403
            $discoveryContactInteractionType =  $discoveryContactInteractionTypeMapper->fetchOneDefaultByCompanyId($currentCompany->id);
404
            if(!$discoveryContactInteractionType) {
405
                $discoveryContactInteractionType = $discoveryContactInteractionTypeMapper->fetchOneFirstActiveByCompanyId($currentCompany->id);
406
            }
407
 
408
 
409
            /*
410
             select count(*), user_id, date(added_on) as date from tbl_discovery_contact_interactions
411
             where interaction_type_id = 1 and date(added_on) BETWEEN '2023-05-05' AND '2023-05-31'
412
             group by date(added_on), user_id
413
             order by date
414
             */
415
 
416
            $table = [];
417
            $select = $queryMapper->getSql()->select(DiscoveryContactInteractionMapper::_TABLE);
418
            $select->columns([
419
                'user_id',
420
                'total' => new Expression('COUNT(*)'),
421
                'date' => new Expression('DATE(added_on)'),
422
            ]);
423
            $select->where->equalTo('interaction_type_id', $discoveryContactInteractionType->id);
424
            $select->where->between( new Expression('DATE(added_on)') , $startDate,  $endDate);
425
            $select->group([ new Expression('DATE(added_on)'), 'user_id']);
426
            $select->order('date ASC');
427
 
428
            $records = $queryMapper->fetchAll($select);
429
            foreach($records as $record)
430
            {
431
                $user_id =  $record['user_id'];
432
 
433
                if(!isset( $table[ $user_id  ])) {
434
 
435
                    $items = [];
436
 
437
                    $dt = \DateTime::createFromFormat('Y-m-d', $startDate);
438
 
439
                    do {
440
                        $tempDate = $dt->format('Y-m-d');
441
                        $items[ $tempDate ] = ['label' => $dt->format('d/m/Y'), 'value' => 0];
442
                        $dt->add(new \DateInterval('P1D'));
443
 
444
 
445
                    } while($endDate > $tempDate);
446
 
447
                    $table[ $user_id  ] = $items;
448
 
449
 
450
                }
451
 
452
 
453
                $table[ $user_id ][ $record['date'] ]['value'] = $record['total'];
454
            }
455
 
456
            $records = array_values($graph);
457
 
458
            $spreadsheet = new Spreadsheet();
459
            $spreadsheet->getProperties()->setTitle('Relevamiento de Contacto');
460
 
461
            $spreadsheet->setActiveSheetIndex(0);
462
 
463
            $dt = \DateTime::createFromFormat('Y-m-d', $startDate);
464
            $spreadsheet->getActiveSheet()->SetCellValue('A1', 'Desde:');
465
            $spreadsheet->getActiveSheet()->SetCellValue('B1', $dt->format('d/m/Y'));
466
 
467
 
468
            $dt = \DateTime::createFromFormat('Y-m-d', $startDate);
469
            $spreadsheet->getActiveSheet()->SetCellValue('C1', 'Hasta:');
470
            $spreadsheet->getActiveSheet()->SetCellValue('D1', $dt->format('d/m/Y'));
471
 
472
 
473
 
474
 
475
            $col = 1;
476
 
477
            foreach($records as $record)
478
            {
479
                $spreadsheet->getActiveSheet()->setCellValue([$col, 3], $record['label']);
480
                $spreadsheet->getActiveSheet()->setCellValue([$col, 4], $record['value']);
481
                $col++;
482
            }
483
 
484
            $spreadsheet->getActiveSheet()->SetCellValue('A6', $this->translator->translate('LABEL_FIRST_NAME'));
485
            $spreadsheet->getActiveSheet()->SetCellValue('B6', $this->translator->translate('LABEL_LAST_NAME'));
486
            $spreadsheet->getActiveSheet()->SetCellValue('C6', $this->translator->translate('LABEL_EMAIL'));
487
 
488
 
489
            $col = 4;
490
 
491
            foreach($records as $record)
492
            {
493
                $spreadsheet->getActiveSheet()->setCellValue([$col, 6], $record['label']);
494
 
495
                $col++;
496
            }
497
 
498
 
499
            $userMapper = UserMapper::getInstance($this->adapter);
500
 
501
 
502
 
503
            $row = 7;
504
            $records = $table;
505
            foreach($records as  $user_id => $items)
506
            {
507
                $user = $userMapper->fetchOne($user_id);
508
                if(!$user) {
509
                    continue;
510
                }
511
 
512
 
513
                $spreadsheet->getActiveSheet()->SetCellValue([1, $row], $user->first_name);
514
                $spreadsheet->getActiveSheet()->SetCellValue([2, $row], $user->last_name);
515
                $spreadsheet->getActiveSheet()->SetCellValue([3, $row], $user->email);
516
 
517
 
518
                $col = 4;
519
 
520
                foreach($items as $item)
521
                {
522
                    $spreadsheet->getActiveSheet()->SetCellValue([$col, $row],  $item['value']);
523
                    $col++;
524
 
525
                }
526
 
527
                $row++;
528
 
529
            }
530
 
531
            $fileName = 'reporte_relevamiento_de_contact_progreso_por_dia_'  . date('d-m-Y-h-i-a', time()) . '.xls';
532
            $tempFilename = tempnam(sys_get_temp_dir(), 'reporte_relevamiento_de_contact_progreso_por_dia_' . time());
533
 
534
            $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
535
            $writer->save($tempFilename);
536
 
537
            $content = file_get_contents($tempFilename);
538
            @unlink($tempFilename);
539
 
540
            return new JsonModel([
541
                'success' => true,
542
                'data' => [
543
                    'content' => base64_encode($content),
544
                    'basename' => $fileName
545
 
546
                ]
547
            ]);
548
 
549
        }
550
 
551
        return new JsonModel([
552
            'success' => false,
553
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
554
        ]);
555
    }
15670 anderson 556
}