Proyectos de Subversion LeadersLinked - Backend

Rev

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