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