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