Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
12437 nelberth 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Controller;
5
 
6
use Laminas\Db\Adapter\AdapterInterface;
16768 efrain 7
 
12437 nelberth 8
use Laminas\Mvc\Controller\AbstractActionController;
9
use Laminas\Log\LoggerInterface;
10
use Laminas\View\Model\JsonModel;
11
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
12443 nelberth 12
use LeadersLinked\Library\Functions;
12437 nelberth 13
use LeadersLinked\Mapper\UserMapper;
12443 nelberth 14
use LeadersLinked\Mapper\CompanyMapper;
12437 nelberth 15
 
12668 nelberth 16
use LeadersLinked\Mapper\HighPerformanceTeamsGroupsMapper;
17
use LeadersLinked\Mapper\HighPerformanceTeamsGroupsMembersMapper;
18
use LeadersLinked\Model\HighPerformanceTeamsGroupsMembers;
19
use LeadersLinked\Model\HighPerformanceTeamsGroups;
12437 nelberth 20
class HighPerformanceTeamsGroupsViewObjectivesController extends AbstractActionController
21
{
22
    /**
23
     *
24
     * @var AdapterInterface
25
     */
26
    private $adapter;
27
 
28
    /**
29
     *
30
     * @var  LoggerInterface
31
     */
32
    private $logger;
33
 
34
    /**
35
     *
36
     * @var array
37
     */
38
    private $config;
39
 
40
    /**
41
     *
42
     * @param AdapterInterface $adapter
43
     * @param LoggerInterface $logger
44
     * @param array $config
45
     */
16768 efrain 46
    public function __construct($adapter, $logger, $config)
12437 nelberth 47
    {
16768 efrain 48
        $this->adapter = $adapter;
49
        $this->logger = $logger;
50
        $this->config = $config;
12437 nelberth 51
    }
12443 nelberth 52
 
53
 
12437 nelberth 54
 
12443 nelberth 55
 
56
    public function indexAction()
12437 nelberth 57
    {
12443 nelberth 58
 
59
 
12437 nelberth 60
        $currentUserPlugin = $this->plugin('currentUserPlugin');
61
        $currentUser = $currentUserPlugin->getUser();
62
        $currentCompany = $currentUserPlugin->getCompany();
63
 
12443 nelberth 64
 
12437 nelberth 65
        $request = $this->getRequest();
66
        if($request->isGet()) {
12443 nelberth 67
 
68
 
12437 nelberth 69
            $headers  = $request->getHeaders();
70
 
71
            $isJson = false;
72
            if($headers->has('Accept')) {
73
                $accept = $headers->get('Accept');
74
 
75
                $prioritized = $accept->getPrioritized();
76
 
77
                foreach($prioritized as $key => $value) {
78
                    $raw = trim($value->getRaw());
79
 
80
                    if(!$isJson) {
81
                        $isJson = strpos($raw, 'json');
82
                    }
83
 
84
                }
85
            }
12443 nelberth 86
 
87
            if($isJson) {
12667 nelberth 88
                $group_uuid = $this->params()->fromRoute('group_id');
89
                if(!$group_uuid) {
90
                    $data = [
91
                        'success'   => false,
92
                        'data'   => 'ERROR_INVALID_PARAMETER'
93
                    ];
94
 
95
                    return new JsonModel($data);
96
                }
12443 nelberth 97
 
12667 nelberth 98
                $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
99
                $highPerformanceTeamsGroups = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($group_uuid);
100
 
101
                if (!$highPerformanceTeamsGroups) {
102
                    $data = [
103
                        'success' => false,
104
                        'data' => 'ERROR_RECORD_NOT_FOUND'
105
                    ];
106
 
107
                    return new JsonModel($data);
108
                }
109
                if($highPerformanceTeamsGroups->status != HighPerformanceTeamsGroups::STATUS_ACTIVE) {
110
 
111
                    return new JsonModel([
112
                        'success' => false,
113
                        'data' => 'ERROR_UNAUTHORIZED'
114
                    ]);
115
 
116
                }
117
                $highPerformanceTeamsGroupsMembersMapper = HighPerformanceTeamsGroupsMembersMapper::getInstance($this->adapter);
118
                $highPerformanceTeamsGroupsMember = $highPerformanceTeamsGroupsMembersMapper->fetchOneByGroupIdAndUserId($highPerformanceTeamsGroups->id, $currentUser->id);
119
                if(!$highPerformanceTeamsGroupsMember) {
120
                    return new JsonModel([
121
                        'success' => false,
122
                        'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
123
                    ]);
124
                }
125
                if(!($highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ACCEPTED || $highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ADDED_BY_ADMIN) ){
126
                    return new JsonModel([
127
                        'success' => false,
128
                        'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
129
                    ]);
130
                }
12443 nelberth 131
 
132
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
12669 nelberth 133
                $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'high-performance-teams/groups/view/objectives/edit');
134
                $allowDelete = $acl->isAllowed($currentUser->usertype_id, 'high-performance-teams/groups/view/objectives/delete');
135
                $allowObjective = $acl->isAllowed($currentUser->usertype_id, 'high-performance-teams/groups/view/objectives/goals');
136
                $allowObjectiveReport = $acl->isAllowed($currentUser->usertype_id, 'high-performance-teams/groups/view/objectives/report');
137
                $allowObjectiveReportAll = $acl->isAllowed($currentUser->usertype_id, 'high-performance-teams/groups/view/objectives/reportall');
12443 nelberth 138
 
139
 
12437 nelberth 140
 
12443 nelberth 141
                $search = $this->params()->fromQuery('search', []);
16766 efrain 142
                $search = empty($search['value']) ? '' :  Functions::sanitizeFilterString($search['value']);
12443 nelberth 143
 
144
 
145
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
146
                $page               = (intval($this->params()->fromQuery('start', 1), 10)/$records_x_page)+1;
147
                $order =  $this->params()->fromQuery('order', []);
148
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
16766 efrain 149
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(Functions::sanitizeFilterString($order[0]['dir']));
12443 nelberth 150
 
151
                $fields =  ['title', 'date'];
152
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'title';
153
 
154
                if(!in_array($order_direction, ['ASC', 'DESC'])) {
155
                    $order_direction = 'ASC';
156
                }
12437 nelberth 157
 
12443 nelberth 158
 
159
                $planningObjectivesAndGoalsObjectivesMapper = PlanningObjectivesAndGoalsObjectivesMapper::getInstance($this->adapter);
160
 
161
                $planningObjectivesAndGoalsGoalsMapper = PlanningObjectivesAndGoalsGoalsMapper::getInstance($this->adapter);
162
 
163
                $planningObjectivesAndGoalsTaskMapper = PlanningObjectivesAndGoalsTaskMapper::getInstance($this->adapter);
164
 
12669 nelberth 165
                $paginator = $planningObjectivesAndGoalsObjectivesMapper->fetchAllDataTableHptg($search, $page, $records_x_page, $order_field, $order_direction, $currentCompany->id,$highPerformanceTeamsGroups->id);
12680 nelberth 166
 
167
                $items = [];
168
 
169
                $records = $paginator->getCurrentItems();
12682 nelberth 170
 
12443 nelberth 171
                foreach($records as $record)
172
                {
173
 
174
 
175
 
176
                    $recordsGoals = $planningObjectivesAndGoalsGoalsMapper->fetchAll($record->id);
177
                    $costObjective=0;
178
                    $indicatorObjective=0;
179
                    $contador=0;
180
                    $countRecordsGoals = count($recordsGoals);
181
                    if($countRecordsGoals>0){
182
                        foreach($recordsGoals as $record2)
183
                        {
184
                            $recordsTask = $planningObjectivesAndGoalsTaskMapper->fetchAll($record2->id);
185
                            $costGoals=0;
186
                            $indicatorGoals=0;
187
                            $countRecordsTask = count($recordsTask);
188
                            if($countRecordsTask>0){
189
                                foreach($recordsTask as $record3){
190
                                    $indicatorGoals=$indicatorGoals+$record3->indicator;
191
                                    $costGoals=$costGoals+$record3->cost;
192
                                }
193
                                $indicatorObjective=$indicatorObjective+($indicatorGoals/$countRecordsTask);
194
                                $costObjective=$costObjective+$costGoals;
195
                                $contador++;
196
                            }
197
                        }
198
                        if($indicatorObjective==0){
199
                            $indicatorObjective=(-1);
200
                        }else{
201
                            $indicatorObjective=round($indicatorObjective/$contador,2);
202
 
203
                        }
204
                    }else{
205
                        $indicatorObjective=(-2);
206
                    }
207
 
208
                    $date='';
12683 nelberth 209
 
12443 nelberth 210
                    if($record->date!=NULL){
211
                        $dt = \DateTime::createFromFormat('Y-m-d', $record->date);
212
                        $date =  $dt->format('d/m/Y');
213
                    }
12685 nelberth 214
 
12443 nelberth 215
                    $item = [
216
                        'title' => $record->title,
217
                        'description' => $record->description,
218
                        'date'=> $date,
219
                        'cost'=>$costObjective,
220
                        'progress'=>$indicatorObjective,
221
                        'status'=> $record->status,
12690 nelberth 222
                        'link_report_all'> $allowObjectiveReportAll ? $this->url()->fromRoute('high-performance-teams/groups/view/objectives/reportall',['group_id' => $highPerformanceTeamsGroups->uuid]) : '',
12443 nelberth 223
                        'actions' => [
12689 nelberth 224
                            'link_edit' => $allowEdit ? $this->url()->fromRoute('high-performance-teams/groups/view/objectives/edit', ['group_id' => $highPerformanceTeamsGroups->uuid,'id' => $record->uuid]) : '',
225
                            'link_delete' => $allowDelete ? $this->url()->fromRoute('high-performance-teams/groups/view/objectives/delete', ['group_id' => $highPerformanceTeamsGroups->uuid,'id' => $record->uuid]) : '',
226
                            'link_objective' => $allowObjective ? $this->url()->fromRoute('high-performance-teams/groups/view/objectives/goals', ['group_id' => $highPerformanceTeamsGroups->uuid,'objective_id' => $record->uuid]) : '',
227
                            'link_objective_report' => $allowObjectiveReport ? $this->url()->fromRoute('high-performance-teams/groups/view/objectives/report', ['group_id' => $highPerformanceTeamsGroups->uuid,'id' => $record->uuid]) : '',
12443 nelberth 228
 
229
                        ]
230
 
231
                    ];
232
 
12690 nelberth 233
 
12443 nelberth 234
                    array_push($items, $item);
235
 
236
                }
12684 nelberth 237
 
12671 nelberth 238
                $recordsObjectivesModule = $planningObjectivesAndGoalsObjectivesMapper->fetchAllHptg($currentCompany->id,$highPerformanceTeamsGroups->id);
12443 nelberth 239
                $costModule=0;
240
                $indicatorModule=0;
241
                $contadorTotalModule=0;
242
                $countRecordsObjectives = count($recordsObjectivesModule);
243
                if($countRecordsObjectives>0){
244
 
245
                    foreach($recordsObjectivesModule as $record)
246
                    {
247
 
248
                        $recordsGoalsModule = $planningObjectivesAndGoalsGoalsMapper->fetchAll($record->id);
249
                        $costObjectiveModule=0;
250
                        $indicatorObjectiveModule=0;
251
                        $contadorModule=0;
252
                        $countRecordsGoalsModule = count($recordsGoalsModule);
253
                        if($countRecordsGoalsModule>0){
254
 
255
                            foreach($recordsGoalsModule as $record2)
256
                            {
257
 
258
                                $recordsTaskModule = $planningObjectivesAndGoalsTaskMapper->fetchAll($record2->id);
259
                                $costGoalsModule=0;
260
                                $indicatorGoalsModule=0;
261
                                $countRecordsTaskModule = count($recordsTaskModule);
262
                                if($countRecordsTaskModule>0){
263
                                    foreach($recordsTaskModule as $record3){
264
 
265
                                        $indicatorGoalsModule=$indicatorGoalsModule+$record3->indicator;
266
                                        $costGoalsModule=$costGoalsModule+$record3->cost;
267
                                    }
268
                                    $indicatorObjectiveModule=$indicatorObjectiveModule+($indicatorGoalsModule/$countRecordsTaskModule);
269
                                    $costModule=$costModule+$costGoalsModule;
270
                                    $contadorModule++;
271
                                }
272
                            }
273
                            if($indicatorObjectiveModule>0){
274
                                $indicatorModule=$indicatorModule+($indicatorObjectiveModule/$contadorModule);
275
                                $contadorTotalModule++;
276
                            }
277
                        }
278
 
279
                    }
280
                    if($indicatorModule==0){
281
                        $indicatorModule=(-5);
282
                    }else{
283
                        $indicatorModule=round($indicatorModule/$contadorTotalModule,2);
284
                    }
285
                }else{
286
                    $indicatorModule=(-3);
287
                }
288
 
289
 
290
                return new JsonModel([
291
                    'success' => true,
292
                    'data' => [
293
                        'items' => $items,
294
                        'total' => $paginator->getTotalItemCount(),
295
                        'module'=>[
296
                            'costModule' => '$'.$costModule,
297
                            'indicatorModule'=>$indicatorModule
298
                        ]
299
                    ]
300
                ]);
301
 
12667 nelberth 302
            }
12443 nelberth 303
        } else {
304
            return new JsonModel([
305
                'success' => false,
306
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
307
            ]);
308
        }
309
    }
310
    public function addAction()
311
    {
12676 nelberth 312
 
12443 nelberth 313
      $currentUserPlugin = $this->plugin('currentUserPlugin');
314
        $currentUser = $currentUserPlugin->getUser();
315
        $currentCompany = $currentUserPlugin->getCompany();
12670 nelberth 316
        $group_uuid = $this->params()->fromRoute('group_id');
317
        if(!$group_uuid) {
318
            $data = [
319
                'success'   => false,
320
                'data'   => 'ERROR_INVALID_PARAMETER'
321
            ];
322
 
323
            return new JsonModel($data);
324
        }
325
 
326
        $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
327
        $highPerformanceTeamsGroups = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($group_uuid);
328
 
329
        if (!$highPerformanceTeamsGroups) {
330
            $data = [
331
                'success' => false,
332
                'data' => 'ERROR_RECORD_NOT_FOUND'
333
            ];
12443 nelberth 334
 
12670 nelberth 335
            return new JsonModel($data);
336
        }
337
        if($highPerformanceTeamsGroups->status != HighPerformanceTeamsGroups::STATUS_ACTIVE) {
338
 
339
            return new JsonModel([
340
                'success' => false,
341
                'data' => 'ERROR_UNAUTHORIZED'
342
            ]);
343
 
344
        }
345
        $highPerformanceTeamsGroupsMembersMapper = HighPerformanceTeamsGroupsMembersMapper::getInstance($this->adapter);
346
        $highPerformanceTeamsGroupsMember = $highPerformanceTeamsGroupsMembersMapper->fetchOneByGroupIdAndUserId($highPerformanceTeamsGroups->id, $currentUser->id);
347
        if(!$highPerformanceTeamsGroupsMember) {
348
            return new JsonModel([
349
                'success' => false,
350
                'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
351
            ]);
352
        }
353
        if(!($highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ACCEPTED || $highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ADDED_BY_ADMIN) ){
354
            return new JsonModel([
355
                'success' => false,
356
                'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
357
            ]);
358
        }
359
 
12443 nelberth 360
        $request = $this->getRequest();
361
        if($request->isPost()) {
362
            $form = new  PlanningObjectivesAndGoalsObjectivesForm();
363
            $dataPost = $request->getPost()->toArray();
364
 
365
            $form->setData($dataPost);
366
 
367
            if($form->isValid()) {
368
                $dataPost = (array) $form->getData();
369
                $dataPost['status'] =  isset($dataPost['status']) ? $dataPost['status'] : PlanningObjectivesAndGoalsObjectives::STATUS_INACTIVE;
370
                $dataPost['company_id']=$currentCompany->id;
371
                $hydrator = new ObjectPropertyHydrator();
372
                $planningObjectivesAndGoalsObjectives = new PlanningObjectivesAndGoalsObjectives();
373
                $hydrator->hydrate($dataPost, $planningObjectivesAndGoalsObjectives);
12672 nelberth 374
                $planningObjectivesAndGoalsObjectives->high_performance_team_group_id=$highPerformanceTeamsGroups->id;
12443 nelberth 375
                $planningObjectivesAndGoalsObjectivesMapper = PlanningObjectivesAndGoalsObjectivesMapper::getInstance($this->adapter);
12673 nelberth 376
 
12678 nelberth 377
 
12443 nelberth 378
                $result = $planningObjectivesAndGoalsObjectivesMapper->insert($planningObjectivesAndGoalsObjectives);
379
 
380
                if($result) {
381
                    $this->logger->info('Se agrego el objetivo ' . $planningObjectivesAndGoalsObjectives->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
382
 
383
                    $data = [
384
                        'success'   => true,
385
                        'data'   => 'LABEL_RECORD_ADDED'
386
                    ];
387
                } else {
388
                    $data = [
389
                        'success'   => false,
390
                        'data'      => $planningObjectivesAndGoalsObjectivesMapper->getError()
391
                    ];
392
 
393
                }
394
 
395
                return new JsonModel($data);
396
 
397
            } else {
398
                $messages = [];
399
                $form_messages = (array) $form->getMessages();
400
                foreach($form_messages  as $fieldname => $field_messages)
401
                {
402
 
403
                    $messages[$fieldname] = array_values($field_messages);
404
                }
405
 
406
                return new JsonModel([
407
                    'success'   => false,
408
                    'data'   => $messages
409
                ]);
12437 nelberth 410
            }
411
 
12443 nelberth 412
        } else {
413
            $data = [
414
                'success' => false,
415
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
416
            ];
417
 
418
            return new JsonModel($data);
419
        }
420
 
421
        return new JsonModel($data);
422
 
423
 
424
    }
425
 
426
 
427
 
428
     public function editAction(){
429
 
430
        $currentUserPlugin = $this->plugin('currentUserPlugin');
431
        $currentUser = $currentUserPlugin->getUser();
432
        $currentCompany = $currentUserPlugin->getCompany();
433
        $request = $this->getRequest();
434
        $uuid = $this->params()->fromRoute('id');
12670 nelberth 435
        $group_uuid = $this->params()->fromRoute('group_id');
436
        if(!$group_uuid) {
437
            $data = [
438
                'success'   => false,
439
                'data'   => 'ERROR_INVALID_PARAMETER'
440
            ];
441
 
442
            return new JsonModel($data);
443
        }
444
 
445
        $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
446
        $highPerformanceTeamsGroups = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($group_uuid);
447
 
448
        if (!$highPerformanceTeamsGroups) {
449
            $data = [
450
                'success' => false,
451
                'data' => 'ERROR_RECORD_NOT_FOUND'
452
            ];
12443 nelberth 453
 
12670 nelberth 454
            return new JsonModel($data);
455
        }
456
        if($highPerformanceTeamsGroups->status != HighPerformanceTeamsGroups::STATUS_ACTIVE) {
457
 
458
            return new JsonModel([
459
                'success' => false,
460
                'data' => 'ERROR_UNAUTHORIZED'
461
            ]);
462
 
463
        }
464
        $highPerformanceTeamsGroupsMembersMapper = HighPerformanceTeamsGroupsMembersMapper::getInstance($this->adapter);
465
        $highPerformanceTeamsGroupsMember = $highPerformanceTeamsGroupsMembersMapper->fetchOneByGroupIdAndUserId($highPerformanceTeamsGroups->id, $currentUser->id);
466
        if(!$highPerformanceTeamsGroupsMember) {
467
            return new JsonModel([
468
                'success' => false,
469
                'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
470
            ]);
471
        }
472
        if(!($highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ACCEPTED || $highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ADDED_BY_ADMIN) ){
473
            return new JsonModel([
474
                'success' => false,
475
                'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
476
            ]);
477
        }
478
 
12443 nelberth 479
 
480
        if(!$uuid) {
481
            $data = [
482
                'success'   => false,
483
                'data'   => 'ERROR_INVALID_PARAMETER'
484
            ];
485
 
486
            return new JsonModel($data);
487
        }
488
 
489
        $planningObjectivesAndGoalsObjectivesMapper = PlanningObjectivesAndGoalsObjectivesMapper::getInstance($this->adapter);
490
        $objectives = $planningObjectivesAndGoalsObjectivesMapper->fetchOneByUuid($uuid);
491
 
492
        if (!$objectives) {
493
            $data = [
494
                'success' => false,
495
                'data' => 'ERROR_RECORD_NOT_FOUND'
496
            ];
497
 
498
            return new JsonModel($data);
499
        }
500
 
501
        if ($objectives->company_id != $currentCompany->id) {
502
            return new JsonModel([
503
                'success' => false,
504
                'data' => 'ERROR_UNAUTHORIZED'
12437 nelberth 505
            ]);
12443 nelberth 506
        }
507
 
508
        if($request->isPost()) {
509
            $form = new  PlanningObjectivesAndGoalsObjectivesForm();
510
            $dataPost = $request->getPost()->toArray();
511
            $dataPost['status'] =  isset($dataPost['status']) ? $dataPost['status'] : PlanningObjectivesAndGoalsObjectives::STATUS_INACTIVE;
512
            $form->setData($dataPost);
12437 nelberth 513
 
12443 nelberth 514
            if($form->isValid()) {
515
                $dataPost = (array) $form->getData();
516
 
517
                $hydrator = new ObjectPropertyHydrator();
518
                $hydrator->hydrate($dataPost, $objectives);
519
                $result = $planningObjectivesAndGoalsObjectivesMapper->update($objectives);
520
 
521
                if($result) {
522
                    $this->logger->info('Se actualizo el objetivo ' . $objectives->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
523
 
524
                    $data = [
525
                        'success' => true,
526
                        'data' => 'LABEL_RECORD_UPDATED'
527
                    ];
528
                } else {
529
                    $data = [
530
                        'success'   => false,
531
                        'data'      => $planningObjectivesAndGoalsObjectivesMapper->getError()
532
                    ];
533
                }
534
 
535
                return new JsonModel($data);
536
 
537
            } else {
538
                $messages = [];
539
                $form_messages = (array) $form->getMessages();
540
                foreach($form_messages  as $fieldname => $field_messages)
541
                {
542
                    $messages[$fieldname] = array_values($field_messages);
543
                }
544
 
545
                return new JsonModel([
546
                    'success'   => false,
547
                    'data'   => $messages
548
                ]);
549
            }
550
        }else if ($request->isGet()) {
551
            $hydrator = new ObjectPropertyHydrator();
12437 nelberth 552
 
12443 nelberth 553
            $data = [
554
                'success' => true,
555
                'data' => $hydrator->extract($objectives)
556
            ];
557
 
558
            return new JsonModel($data);
12437 nelberth 559
        } else {
12443 nelberth 560
            $data = [
561
                'success' => false,
562
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
563
            ];
564
 
565
            return new JsonModel($data);
566
        }
567
 
568
        return new JsonModel($data);
569
 
570
    }
571
 
572
 
573
 
574
 
575
    public function deleteAction(){
576
        $currentUserPlugin = $this->plugin('currentUserPlugin');
577
        $currentCompany = $currentUserPlugin->getCompany();
578
        $currentUser = $currentUserPlugin->getUser();
579
 
580
        $request = $this->getRequest();
581
        $uuid = $this->params()->fromRoute('id');
12670 nelberth 582
        $group_uuid = $this->params()->fromRoute('group_id');
583
        if(!$group_uuid) {
584
            $data = [
585
                'success'   => false,
586
                'data'   => 'ERROR_INVALID_PARAMETER'
587
            ];
588
 
589
            return new JsonModel($data);
590
        }
12443 nelberth 591
 
12670 nelberth 592
        $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
593
        $highPerformanceTeamsGroups = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($group_uuid);
594
 
595
        if (!$highPerformanceTeamsGroups) {
596
            $data = [
597
                'success' => false,
598
                'data' => 'ERROR_RECORD_NOT_FOUND'
599
            ];
12443 nelberth 600
 
12670 nelberth 601
            return new JsonModel($data);
602
        }
603
        if($highPerformanceTeamsGroups->status != HighPerformanceTeamsGroups::STATUS_ACTIVE) {
604
 
605
            return new JsonModel([
606
                'success' => false,
607
                'data' => 'ERROR_UNAUTHORIZED'
608
            ]);
609
 
610
        }
611
        $highPerformanceTeamsGroupsMembersMapper = HighPerformanceTeamsGroupsMembersMapper::getInstance($this->adapter);
612
        $highPerformanceTeamsGroupsMember = $highPerformanceTeamsGroupsMembersMapper->fetchOneByGroupIdAndUserId($highPerformanceTeamsGroups->id, $currentUser->id);
613
        if(!$highPerformanceTeamsGroupsMember) {
614
            return new JsonModel([
615
                'success' => false,
616
                'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
617
            ]);
618
        }
619
        if(!($highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ACCEPTED || $highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ADDED_BY_ADMIN) ){
620
            return new JsonModel([
621
                'success' => false,
622
                'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
623
            ]);
624
        }
625
 
626
 
12443 nelberth 627
        if (!$uuid) {
628
            $data = [
629
                'success' => false,
630
                'data' => 'ERROR_INVALID_PARAMETER'
631
            ];
632
 
633
            return new JsonModel($data);
634
        }
635
 
636
 
637
 
638
        $planningObjectivesAndGoalsObjectivesMapper = PlanningObjectivesAndGoalsObjectivesMapper::getInstance($this->adapter);
639
        $objectives = $planningObjectivesAndGoalsObjectivesMapper->fetchOneByUuid($uuid);
640
 
641
        if (!$objectives) {
642
            $data = [
643
                'success' => false,
644
                'data' => 'ERROR_RECORD_NOT_FOUND'
645
            ];
646
 
647
            return new JsonModel($data);
648
        }
649
 
650
        if ($objectives->company_id != $currentCompany->id) {
12437 nelberth 651
            return new JsonModel([
652
                'success' => false,
12443 nelberth 653
                'data' => 'ERROR_UNAUTHORIZED'
654
            ]);
655
        }
656
 
657
        if ($request->isPost()) {
658
 
659
 
660
            $result = $planningObjectivesAndGoalsObjectivesMapper->delete($objectives->id);
661
            if ($result) {
662
                $this->logger->info('Se borro el objetivo con el titulo ' . $objectives->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
663
 
664
                $data = [
665
                    'success' => true,
666
                    'data' => 'LABEL_RECORD_DELETED'
667
                ];
668
            } else {
669
 
670
                $data = [
671
                    'success' => false,
672
                    'data' => $planningObjectivesAndGoalsObjectivesMapper->getError()
673
                ];
674
 
675
                return new JsonModel($data);
676
            }
677
        } else {
678
            $data = [
679
                'success' => false,
12437 nelberth 680
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
12443 nelberth 681
            ];
682
 
683
            return new JsonModel($data);
12437 nelberth 684
        }
12443 nelberth 685
 
686
        return new JsonModel($data);
687
    }
12437 nelberth 688
 
12443 nelberth 689
 
690
 
691
    public function reportAction() {
692
        $currentUserPlugin = $this->plugin('currentUserPlugin');
693
        $currentUser = $currentUserPlugin->getUser();
694
        $currentCompany = $currentUserPlugin->getCompany();
695
 
696
        $request = $this->getRequest();
697
        $uuid = $this->params()->fromRoute('id');
12691 nelberth 698
        $group_uuid = $this->params()->fromRoute('group_id');
699
        if(!$group_uuid) {
700
            $data = [
701
                'success'   => false,
702
                'data'   => 'ERROR_INVALID_PARAMETER'
703
            ];
704
 
705
            return new JsonModel($data);
706
        }
707
 
708
        $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
709
        $highPerformanceTeamsGroups = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($group_uuid);
710
 
711
        if (!$highPerformanceTeamsGroups) {
712
            $data = [
713
                'success' => false,
714
                'data' => 'ERROR_RECORD_NOT_FOUND'
715
            ];
12443 nelberth 716
 
12691 nelberth 717
            return new JsonModel($data);
718
        }
719
        if($highPerformanceTeamsGroups->status != HighPerformanceTeamsGroups::STATUS_ACTIVE) {
12443 nelberth 720
 
12691 nelberth 721
            return new JsonModel([
722
                'success' => false,
723
                'data' => 'ERROR_UNAUTHORIZED'
724
            ]);
725
 
726
        }
727
        $highPerformanceTeamsGroupsMembersMapper = HighPerformanceTeamsGroupsMembersMapper::getInstance($this->adapter);
728
        $highPerformanceTeamsGroupsMember = $highPerformanceTeamsGroupsMembersMapper->fetchOneByGroupIdAndUserId($highPerformanceTeamsGroups->id, $currentUser->id);
729
        if(!$highPerformanceTeamsGroupsMember) {
730
            return new JsonModel([
731
                'success' => false,
732
                'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
733
            ]);
734
        }
735
        if(!($highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ACCEPTED || $highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ADDED_BY_ADMIN) ){
736
            return new JsonModel([
737
                'success' => false,
738
                'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
739
            ]);
740
        }
741
 
742
 
12443 nelberth 743
        if (!$uuid) {
744
            $data = [
745
                'success' => false,
746
                'data' => 'ERROR_INVALID_PARAMETER'
747
            ];
748
 
749
            return new JsonModel($data);
750
        }
751
 
752
        $planningObjectivesAndGoalsObjectivesMapper = PlanningObjectivesAndGoalsObjectivesMapper::getInstance($this->adapter);
753
        $recordsObjectives = $planningObjectivesAndGoalsObjectivesMapper->fetchOneByUuid($uuid);
12437 nelberth 754
 
12443 nelberth 755
        if (!$recordsObjectives) {
756
            $data = [
757
                'success' => false,
758
                'data' => 'Objetivo ERROR_RECORD_NOT_FOUND'
759
            ];
760
 
761
            return new JsonModel($data);
762
        }
763
 
764
        if ($recordsObjectives->company_id != $currentCompany->id) {
765
            return new JsonModel([
766
                'success' => false,
767
                'data' => 'ERROR_UNAUTHORIZED'
768
            ]);
769
        }
770
        $planningObjectivesAndGoalsGoalsMapper = PlanningObjectivesAndGoalsGoalsMapper::getInstance($this->adapter);
12692 nelberth 771
        $recordsGoals = $planningObjectivesAndGoalsGoalsMapper->fetchAll($recordsObjectives->id);
12443 nelberth 772
 
773
        $planningObjectivesAndGoalsTaskMapper = PlanningObjectivesAndGoalsTaskMapper::getInstance($this->adapter);
774
        $userMapper = UserMapper::getInstance($this->adapter);
775
        $PlanningObjectivesAndGoalsTaskMemberMapper = PlanningObjectivesAndGoalsTaskMembersMapper::getInstance($this->adapter);
776
 
777
            foreach($recordsGoals as $record2){
778
 
779
                $recordsTask = $planningObjectivesAndGoalsTaskMapper->fetchAll($record2->id);
780
 
781
 
782
                foreach($recordsTask as $record3){
783
                    $recordsTaskMembers =  $PlanningObjectivesAndGoalsTaskMemberMapper->fetchAll($record3->id);
784
                    $usersName=[];
785
                    foreach($recordsTaskMembers as $record4){
786
                        $datosUser = $userMapper->fetchOne($record4->user_id);
787
                        $usersName[$record4->id]=$datosUser->first_name.' '.$datosUser->last_name;
788
                    }
789
                    $record3->who=$usersName;
790
                }
791
 
792
 
793
 
794
 
795
                $record2->task = $recordsTask;
796
            }
797
           $items=[
798
            'objetives' => [
799
                'title'=>$recordsObjectives->title,
800
                'description' =>$recordsObjectives->description,
801
                'date'=>$recordsObjectives->date,
802
                'status'=>$recordsObjectives->status,
803
                'goals'=>$recordsGoals
804
            ]
805
            ];
806
 
807
 
808
 
809
 
810
 
811
        if ($request->isGet()) {
812
 
813
 
814
            return $this->renderPdf($currentCompany,$items);
815
        } else {
816
            $data = [
817
                'success' => false,
818
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
819
            ];
820
 
821
            return new JsonModel($data);
822
        }
823
 
824
        return new JsonModel($data);
12437 nelberth 825
    }
12443 nelberth 826
    public function reportallAction() {
827
        $currentUserPlugin = $this->plugin('currentUserPlugin');
828
        $currentUser = $currentUserPlugin->getUser();
829
        $currentCompany = $currentUserPlugin->getCompany();
12437 nelberth 830
 
12443 nelberth 831
        $request = $this->getRequest();
832
 
12693 nelberth 833
        $group_uuid = $this->params()->fromRoute('group_id');
834
        if(!$group_uuid) {
835
            $data = [
836
                'success'   => false,
837
                'data'   => 'ERROR_INVALID_PARAMETER'
838
            ];
839
 
840
            return new JsonModel($data);
841
        }
842
 
843
        $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
844
        $highPerformanceTeamsGroups = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($group_uuid);
845
 
846
        if (!$highPerformanceTeamsGroups) {
847
            $data = [
848
                'success' => false,
849
                'data' => 'ERROR_RECORD_NOT_FOUND'
850
            ];
12443 nelberth 851
 
12693 nelberth 852
            return new JsonModel($data);
853
        }
854
        if($highPerformanceTeamsGroups->status != HighPerformanceTeamsGroups::STATUS_ACTIVE) {
855
 
856
            return new JsonModel([
857
                'success' => false,
858
                'data' => 'ERROR_UNAUTHORIZED'
859
            ]);
860
 
861
        }
862
        $highPerformanceTeamsGroupsMembersMapper = HighPerformanceTeamsGroupsMembersMapper::getInstance($this->adapter);
863
        $highPerformanceTeamsGroupsMember = $highPerformanceTeamsGroupsMembersMapper->fetchOneByGroupIdAndUserId($highPerformanceTeamsGroups->id, $currentUser->id);
864
        if(!$highPerformanceTeamsGroupsMember) {
865
            return new JsonModel([
866
                'success' => false,
867
                'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
868
            ]);
869
        }
870
        if(!($highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ACCEPTED || $highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ADDED_BY_ADMIN) ){
871
            return new JsonModel([
872
                'success' => false,
873
                'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
874
            ]);
875
        }
876
 
12443 nelberth 877
 
878
 
879
        $planningObjectivesAndGoalsObjectivesMapper = PlanningObjectivesAndGoalsObjectivesMapper::getInstance($this->adapter);
12693 nelberth 880
        $recordsObjectives = $planningObjectivesAndGoalsObjectivesMapper->fetchAllHptg($currentCompany->id,$highPerformanceTeamsGroups->id);
12443 nelberth 881
 
882
        if (!$recordsObjectives) {
883
            $data = [
884
                'success' => false,
885
                'data' => 'Objetivo ERROR_RECORD_NOT_FOUND'
886
            ];
887
 
888
            return new JsonModel($data);
889
        }
890
 
891
 
892
        $planningObjectivesAndGoalsGoalsMapper = PlanningObjectivesAndGoalsGoalsMapper::getInstance($this->adapter);
893
 
894
        $planningObjectivesAndGoalsTaskMapper = PlanningObjectivesAndGoalsTaskMapper::getInstance($this->adapter);
895
        $items;
896
        $contador=0;
897
        foreach($recordsObjectives as $record){
898
 
899
            if ($record->company_id != $currentCompany->id) {
900
                return new JsonModel([
901
                    'success' => false,
902
                    'data' => 'ERROR_UNAUTHORIZED'
903
                ]);
904
            }
905
            $userMapper = UserMapper::getInstance($this->adapter);
906
            $PlanningObjectivesAndGoalsTaskMemberMapper = PlanningObjectivesAndGoalsTaskMembersMapper::getInstance($this->adapter);
907
            $recordsGoals = $planningObjectivesAndGoalsGoalsMapper->fetchAll($record->id);
908
 
909
                foreach($recordsGoals as $record2){
910
 
911
                    $recordsTask = $planningObjectivesAndGoalsTaskMapper->fetchAll($record2->id);
912
 
913
 
914
                    foreach($recordsTask as $record3){
915
                        $recordsTaskMembers =  $PlanningObjectivesAndGoalsTaskMemberMapper->fetchAll($record3->id);
916
                        $usersName=[];
917
                        foreach($recordsTaskMembers as $record4){
918
                            $datosUser = $userMapper->fetchOne($record4->user_id);
919
                            $usersName[$record4->id]=$datosUser->first_name.' '.$datosUser->last_name;
920
                        }
921
                        $record3->who=$usersName;
922
                    }
923
 
924
                    $record2->task = $recordsTask;
925
                }
926
         /*   $item=[
927
                $contador => [
928
                    'title'=>$record->title,
929
                    'description' =>$record->description,
930
                    'date'=>$record->date,
931
                    'status'=>$record->status,
932
                    'goals'=>$recordsGoals
933
                ]
934
            ];
935
            array_push($items, $item);
936
            $contador++;*/
937
        $record->goals=$recordsGoals;
938
        }
939
 
940
 
941
 
942
        if ($request->isGet()) {
943
 
944
 
945
            return $this->renderPdf($currentCompany,$recordsObjectives,2);
946
        } else {
947
            $data = [
948
                'success' => false,
949
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
950
            ];
951
 
952
            return new JsonModel($data);
953
        }
954
 
955
        return new JsonModel($data);
956
    }
957
    public function matrizAction() {
958
        $currentUserPlugin = $this->plugin('currentUserPlugin');
959
        $currentUser = $currentUserPlugin->getUser();
960
        $currentCompany = $currentUserPlugin->getCompany();
961
 
962
        $request = $this->getRequest();
12694 nelberth 963
        $group_uuid = $this->params()->fromRoute('group_id');
964
        if(!$group_uuid) {
965
            $data = [
966
                'success'   => false,
967
                'data'   => 'ERROR_INVALID_PARAMETER'
968
            ];
969
 
970
            return new JsonModel($data);
971
        }
972
 
973
        $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
974
        $highPerformanceTeamsGroups = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($group_uuid);
975
 
976
        if (!$highPerformanceTeamsGroups) {
977
            $data = [
978
                'success' => false,
979
                'data' => 'ERROR_RECORD_NOT_FOUND'
980
            ];
12443 nelberth 981
 
12694 nelberth 982
            return new JsonModel($data);
983
        }
984
        if($highPerformanceTeamsGroups->status != HighPerformanceTeamsGroups::STATUS_ACTIVE) {
12443 nelberth 985
 
12694 nelberth 986
            return new JsonModel([
987
                'success' => false,
988
                'data' => 'ERROR_UNAUTHORIZED'
989
            ]);
990
 
991
        }
992
        $highPerformanceTeamsGroupsMembersMapper = HighPerformanceTeamsGroupsMembersMapper::getInstance($this->adapter);
993
        $highPerformanceTeamsGroupsMember = $highPerformanceTeamsGroupsMembersMapper->fetchOneByGroupIdAndUserId($highPerformanceTeamsGroups->id, $currentUser->id);
994
        if(!$highPerformanceTeamsGroupsMember) {
995
            return new JsonModel([
996
                'success' => false,
997
                'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
998
            ]);
999
        }
1000
        if(!($highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ACCEPTED || $highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ADDED_BY_ADMIN) ){
1001
            return new JsonModel([
1002
                'success' => false,
1003
                'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
1004
            ]);
1005
        }
1006
 
12443 nelberth 1007
 
1008
 
1009
        $planningObjectivesAndGoalsObjectivesMapper = PlanningObjectivesAndGoalsObjectivesMapper::getInstance($this->adapter);
12694 nelberth 1010
        $recordsObjectives = $planningObjectivesAndGoalsObjectivesMapper->fetchAllHptg($currentCompany->id,$highPerformanceTeamsGroups->id);
12443 nelberth 1011
 
1012
        if (!$recordsObjectives) {
1013
            $data = [
1014
                'success' => false,
1015
                'data' => 'Objetivo ERROR_RECORD_NOT_FOUND'
1016
            ];
1017
 
1018
            return new JsonModel($data);
1019
        }
1020
 
1021
 
1022
        $planningObjectivesAndGoalsGoalsMapper = PlanningObjectivesAndGoalsGoalsMapper::getInstance($this->adapter);
1023
 
1024
        $planningObjectivesAndGoalsTaskMapper = PlanningObjectivesAndGoalsTaskMapper::getInstance($this->adapter);
1025
        $items;
1026
        $contador=0;
1027
        foreach($recordsObjectives as $record){
1028
 
1029
            if ($record->company_id != $currentCompany->id) {
1030
                return new JsonModel([
1031
                    'success' => false,
1032
                    'data' => 'ERROR_UNAUTHORIZED'
1033
                ]);
1034
            }
1035
            $recordsGoals = $planningObjectivesAndGoalsGoalsMapper->fetchAll($record->id);
1036
 
1037
                foreach($recordsGoals as $record2){
1038
 
1039
                    $recordsTask = $planningObjectivesAndGoalsTaskMapper->fetchAll($record2->id);
1040
                    $record2->task = $recordsTask;
1041
                }
1042
         /*   $item=[
1043
                $contador => [
1044
                    'title'=>$record->title,
1045
                    'description' =>$record->description,
1046
                    'date'=>$record->date,
1047
                    'status'=>$record->status,
1048
                    'goals'=>$recordsGoals
1049
                ]
1050
            ];
1051
            array_push($items, $item);
1052
            $contador++;*/
1053
        $record->goals=$recordsGoals;
1054
        }
1055
 
1056
 
1057
 
1058
        if ($request->isGet()) {
1059
 
1060
 
1061
            return $this->renderPdf($currentCompany,$recordsObjectives,3);
1062
        } else {
1063
            $data = [
1064
                'success' => false,
1065
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1066
            ];
1067
 
1068
            return new JsonModel($data);
1069
        }
1070
 
1071
        return new JsonModel($data);
1072
    }
1073
 
1074
 
1075
 
1076
    public function renderPDF($currentCompany,$items,$switch=1) {
1077
 
1078
        $pdf = new PlanningObjectivesAndGoalsObjectivesPdfOne();
1079
 
1080
        $pdf->header = '';
1081
        $pdf->footer = '';
1082
        if ($currentCompany) {
1083
            //get company Data
1084
            $companyMapper = CompanyMapper::getInstance($this->adapter);
1085
            $company = $companyMapper->fetchOne($currentCompany->id);
1086
 
1087
            $target_path = $this->config['leaderslinked.fullpath.company'] . DIRECTORY_SEPARATOR . $company->uuid;
1088
 
1089
            $pdf->header = $company->header ? $target_path . DIRECTORY_SEPARATOR . $company->header : '';
1090
            $pdf->footer = $company->footer ? $target_path . DIRECTORY_SEPARATOR . $company->footer : '';
1091
        }
1092
 
1093
        $pdf->SetMargins(10,200,10);
1094
        $pdf->SetAutoPageBreak(true,40);
1095
        $pdf->AliasNbPages();
1096
 
1097
 
1098
            if($switch==3){
1099
                $pdf->borderTableMatriz('Planificacion - Objetivos y metas',$items);
1100
            }else if($switch==2){
1101
                $pdf->borderTableAll('Planificacion - Objetivos y metas',$items);
1102
 
1103
            }else{
1104
                $pdf->borderTable('Planificacion - Objetivos y metas',$items);
1105
 
1106
            }
1107
 
1108
 
1109
 
1110
 
1111
        return $pdf->Output();
1112
    }
12437 nelberth 1113
}