Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev Autor Línea Nro. Línea
66 efrain 1
<?php
649 geraldo 2
 
66 efrain 3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Controller;
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
8
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
9
use Laminas\Mvc\Controller\AbstractActionController;
10
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
11
use Laminas\Log\LoggerInterface;
12
use Laminas\View\Model\ViewModel;
13
use Laminas\View\Model\JsonModel;
14
use LeadersLinked\Library\Functions;
804 geraldo 15
use LeadersLinked\Mapper\CompanyMapper;
66 efrain 16
use LeadersLinked\Mapper\JobDescriptionMapper;
17
use LeadersLinked\Mapper\CompetencyTypeMapper;
18
use LeadersLinked\Model\JobDescription;
19
use LeadersLinked\Form\JobDescriptionForm;
20
use LeadersLinked\Mapper\CompetencyMapper;
567 geraldo 21
use LeadersLinked\Library\JobPDF;
66 efrain 22
use LeadersLinked\Mapper\JobDescriptionCompetencyMapper;
23
use LeadersLinked\Mapper\JobDescriptionSubordinateMapper;
24
use LeadersLinked\Model\JobDescriptionCompetency;
25
use LeadersLinked\Model\JobDescriptionSubordinate;
26
 
649 geraldo 27
class JobDescriptionController extends AbstractActionController {
66 efrain 28
 
29
    /**
30
     *
31
     * @var AdapterInterface
32
     */
33
    private $adapter;
649 geraldo 34
 
66 efrain 35
    /**
36
     *
37
     * @var AbstractAdapter
38
     */
39
    private $cache;
649 geraldo 40
 
66 efrain 41
    /**
42
     *
43
     * @var  LoggerInterface
44
     */
45
    private $logger;
46
 
47
    /**
48
     *
49
     * @var array
50
     */
51
    private $config;
649 geraldo 52
 
66 efrain 53
    /**
54
     *
55
     * @param AdapterInterface $adapter
56
     * @param AbstractAdapter $cache
57
     * @param LoggerInterface $logger
58
     * @param array $config
59
     */
649 geraldo 60
    public function __construct($adapter, $cache, $logger, $config) {
61
        $this->adapter = $adapter;
62
        $this->cache = $cache;
63
        $this->logger = $logger;
64
        $this->config = $config;
66 efrain 65
    }
649 geraldo 66
 
67
    public function indexAction() {
66 efrain 68
        $currentUserPlugin = $this->plugin('currentUserPlugin');
69
        $currentUser = $currentUserPlugin->getUser();
70
        $currentCompany = $currentUserPlugin->getCompany();
649 geraldo 71
 
66 efrain 72
        $request = $this->getRequest();
649 geraldo 73
 
74
        $headers = $request->getHeaders();
75
 
66 efrain 76
        $request = $this->getRequest();
649 geraldo 77
        if ($request->isGet()) {
78
 
79
 
80
            $headers = $request->getHeaders();
81
 
66 efrain 82
            $isJson = false;
649 geraldo 83
            if ($headers->has('Accept')) {
66 efrain 84
                $accept = $headers->get('Accept');
649 geraldo 85
 
66 efrain 86
                $prioritized = $accept->getPrioritized();
649 geraldo 87
 
88
                foreach ($prioritized as $key => $value) {
66 efrain 89
                    $raw = trim($value->getRaw());
649 geraldo 90
 
91
                    if (!$isJson) {
66 efrain 92
                        $isJson = strpos($raw, 'json');
93
                    }
94
                }
95
            }
649 geraldo 96
 
97
            if ($isJson) {
66 efrain 98
                $search = $this->params()->fromQuery('search', []);
99
                $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
649 geraldo 100
 
101
                $page = intval($this->params()->fromQuery('start', 1), 10);
102
                $records_x_page = intval($this->params()->fromQuery('length', 10), 10);
103
                $order = $this->params()->fromQuery('order', []);
104
                $order_field = empty($order[0]['column']) ? 99 : intval($order[0]['column'], 10);
105
                $order_direction = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var($order[0]['dir'], FILTER_SANITIZE_STRING));
106
 
107
                $fields = ['name'];
66 efrain 108
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'name';
649 geraldo 109
 
110
                if (!in_array($order_direction, ['ASC', 'DESC'])) {
66 efrain 111
                    $order_direction = 'ASC';
112
                }
649 geraldo 113
 
66 efrain 114
                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
115
                $paginator = $jobDescriptionMapper->fetchAllDataTableByCompanyId($currentCompany->id, $search, $page, $records_x_page, $order_field, $order_direction);
116
 
117
                $items = [];
118
                $records = $paginator->getCurrentItems();
649 geraldo 119
                foreach ($records as $record) {
120
 
121
 
122
 
66 efrain 123
                    $item = [
124
                        'name' => $record->name,
125
                        'status' => $record->status,
126
                        'actions' => [
649 geraldo 127
                            'link_report' => $this->url()->fromRoute('settings/jobs-description/report', ['id' => $record->uuid]),
128
                            'link_edit' => $this->url()->fromRoute('settings/jobs-description/edit', ['id' => $record->uuid]),
129
                            'link_delete' => $this->url()->fromRoute('settings/jobs-description/delete', ['id' => $record->uuid])
130
                        ]
66 efrain 131
                    ];
649 geraldo 132
 
66 efrain 133
                    array_push($items, $item);
134
                }
649 geraldo 135
 
66 efrain 136
                return new JsonModel([
137
                    'success' => true,
138
                    'data' => [
139
                        'items' => $items,
140
                        'total' => $paginator->getTotalItemCount(),
141
                    ]
142
                ]);
649 geraldo 143
            } else {
66 efrain 144
                $form = new JobDescriptionForm($this->adapter, $currentCompany->id);
649 geraldo 145
 
66 efrain 146
                $this->layout()->setTemplate('layout/layout-backend');
147
                $viewModel = new ViewModel();
148
                $viewModel->setTemplate('leaders-linked/jobs-description/index.phtml');
149
                $viewModel->setVariable('form', $form);
649 geraldo 150
                return $viewModel;
151
            }
66 efrain 152
        } else {
153
            return new JsonModel([
154
                'success' => false,
155
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
649 geraldo 156
            ]);
157
            ;
66 efrain 158
        }
159
    }
649 geraldo 160
 
161
    public function addAction() {
66 efrain 162
        $currentUserPlugin = $this->plugin('currentUserPlugin');
163
        $currentUser = $currentUserPlugin->getUser();
164
        $currentCompany = $currentUserPlugin->getCompany();
649 geraldo 165
 
66 efrain 166
        $request = $this->getRequest();
649 geraldo 167
 
168
 
169
        if ($request->isGet()) {
170
 
66 efrain 171
            $data = [
649 geraldo 172
                'competency_types' => [],
173
                'competencies' => [],
174
                'jobs_description' => [],
66 efrain 175
            ];
649 geraldo 176
 
66 efrain 177
            $competencyTypeMapper = CompetencyTypeMapper::getInstance($this->adapter);
178
            $records = $competencyTypeMapper->fetchAllByCompanyId($currentCompany->id);
649 geraldo 179
 
66 efrain 180
            $competencyTypesUUID = [];
649 geraldo 181
            foreach ($records as $record) {
66 efrain 182
                array_push($data['competency_types'], [
183
                    'competency_type_id' => $record->uuid,
184
                    'name' => $record->name,
185
                ]);
649 geraldo 186
 
66 efrain 187
                $competencyTypesUUID[$record->id] = $record->uuid;
188
            }
189
 
649 geraldo 190
 
191
 
66 efrain 192
            $competencyMapper = CompetencyMapper::getInstance($this->adapter);
193
            $records = $competencyMapper->fetchAllActiveByCompanyId($currentCompany->id);
649 geraldo 194
 
195
 
196
            foreach ($records as $record) {
197
                if (isset($competencyTypesUUID[$record->competency_type_id])) {
198
 
66 efrain 199
                    array_push($data['competencies'], [
200
                        'competency_type_id' => $competencyTypesUUID[$record->competency_type_id],
201
                        'competency_id' => $record->uuid,
202
                        'name' => $record->name,
649 geraldo 203
                    ]);
66 efrain 204
                }
205
            }
649 geraldo 206
 
66 efrain 207
            $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
208
            $records = $jobDescriptionMapper->fetchAllActiveByCompanyId($currentCompany->id);
649 geraldo 209
 
210
            foreach ($records as $record) {
66 efrain 211
                array_push($data['jobs_description'], [
212
                    'job_description_id' => $record->uuid,
213
                    'name' => $record->name
214
                ]);
215
            }
649 geraldo 216
 
217
 
66 efrain 218
            $data = [
649 geraldo 219
                'success' => true,
220
                'data' => $data,
66 efrain 221
            ];
649 geraldo 222
 
223
 
66 efrain 224
            return new JsonModel($data);
649 geraldo 225
        } else if ($request->isPost()) {
226
            $form = new JobDescriptionForm($this->adapter, $currentCompany->id);
66 efrain 227
            $dataPost = $request->getPost()->toArray();
649 geraldo 228
 
229
 
230
 
231
 
232
 
66 efrain 233
            $form->setData($dataPost);
649 geraldo 234
 
235
            if ($form->isValid()) {
66 efrain 236
                $dataPost = (array) $form->getData();
237
                $dataPost['status'] = $dataPost['status'] ? $dataPost['status'] : JobDescription::STATUS_INACTIVE;
649 geraldo 238
 
239
 
66 efrain 240
                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
649 geraldo 241
 
242
                if (!empty($dataPost['job_description_id_boss'])) {
243
 
66 efrain 244
                    $jobDescriptionBoss = $jobDescriptionMapper->fetchOneByUuid($dataPost['job_description_id_boss']);
649 geraldo 245
                    if ($jobDescriptionBoss) {
246
                        if ($jobDescriptionBoss->company_id == $currentCompany->id) {
66 efrain 247
                            $dataPost['job_description_id_boss'] = $jobDescriptionBoss->id;
248
                        } else {
249
                            $dataPost['job_description_id_boss'] = null;
250
                        }
251
                    } else {
252
                        $dataPost['job_description_id_boss'] = null;
253
                    }
254
                }
255
 
256
 
649 geraldo 257
 
66 efrain 258
                $hydrator = new ObjectPropertyHydrator();
259
                $jobDescription = new JobDescription();
649 geraldo 260
 
66 efrain 261
                $hydrator->hydrate($dataPost, $jobDescription);
649 geraldo 262
 
263
                if ($currentCompany) {
66 efrain 264
                    $jobDescription->company_id = $currentCompany->id;
265
                }
266
 
649 geraldo 267
 
66 efrain 268
                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
269
                $result = $jobDescriptionMapper->insert($jobDescription);
649 geraldo 270
 
271
 
272
                if ($result) {
66 efrain 273
                    $jobDescriptionSubordinateMapper = JobDescriptionSubordinateMapper::getInstance($this->adapter);
274
                    $jobDescriptionSubordinateMapper->deleteAllByJobDescriptionIdTopLevel($jobDescription->id);
649 geraldo 275
 
66 efrain 276
                    $jobDescriptionCompetencyMapper = JobDescriptionCompetencyMapper::getInstance($this->adapter);
277
                    $jobDescriptionCompetencyMapper->deleteAllBJobDescriptionId($jobDescription->id);
649 geraldo 278
 
279
 
66 efrain 280
                    $competencyMapper = CompetencyMapper::getInstance($this->adapter);
281
                    $competencies = $competencyMapper->fetchAllActiveByCompanyId($currentCompany->id);
649 geraldo 282
 
283
 
284
                    foreach ($competencies as $competency) {
66 efrain 285
                        $level = filter_var($this->params()->fromPost('competency_level' . $competency->uuid, ''), FILTER_SANITIZE_NUMBER_INT);
649 geraldo 286
                        if (in_array($level, [1, 2, 3, 4])) {
66 efrain 287
                            $jobDescriptionCompetency = new JobDescriptionCompetency();
288
                            $jobDescriptionCompetency->competency_id = $competency->id;
289
                            $jobDescriptionCompetency->job_description_id = $jobDescription->id;
290
                            $jobDescriptionCompetency->level = $level;
291
                            $jobDescriptionCompetencyMapper->insert($jobDescriptionCompetency);
292
                        }
293
                    }
649 geraldo 294
 
295
 
66 efrain 296
                    $jobsDescriptionListingSubordinate = $jobDescriptionMapper->fetchAllActiveByCompanyIdWhereIdNotEqual($currentCompany->id, $jobDescription->id);
649 geraldo 297
                    foreach ($jobsDescriptionListingSubordinate as $jobDescriptionListingSubordinate) {
66 efrain 298
                        $job_description_subordinate = filter_var($this->params()->fromPost('job_description_id_subordinate' . $jobDescriptionListingSubordinate->uuid, ''), FILTER_SANITIZE_STRING);
649 geraldo 299
                        if ($job_description_subordinate) {
66 efrain 300
                            $jobDescriptionSubordinate = new JobDescriptionSubordinate();
301
                            $jobDescriptionSubordinate->job_description_id_top_level = $jobDescription->id;
302
                            $jobDescriptionSubordinate->job_description_id_low_level = $jobDescriptionListingSubordinate->id;
303
                            $jobDescriptionSubordinateMapper->insert($jobDescriptionSubordinate);
304
                        }
305
                    }
649 geraldo 306
 
307
 
308
 
66 efrain 309
                    $this->logger->info('Se agrego el cargo ' . $jobDescription->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
649 geraldo 310
 
66 efrain 311
                    $data = [
649 geraldo 312
                        'success' => true,
313
                        'data' => 'LABEL_RECORD_ADDED'
66 efrain 314
                    ];
315
                } else {
316
                    $data = [
649 geraldo 317
                        'success' => false,
318
                        'data' => $jobDescriptionMapper->getError()
66 efrain 319
                    ];
320
                }
649 geraldo 321
 
66 efrain 322
                return new JsonModel($data);
323
            } else {
324
                $messages = [];
325
                $form_messages = (array) $form->getMessages();
649 geraldo 326
                foreach ($form_messages as $fieldname => $field_messages) {
327
 
66 efrain 328
                    $messages[$fieldname] = array_values($field_messages);
329
                }
649 geraldo 330
 
66 efrain 331
                return new JsonModel([
649 geraldo 332
                    'success' => false,
333
                    'data' => $messages
66 efrain 334
                ]);
335
            }
336
        } else {
337
            $data = [
338
                'success' => false,
339
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
340
            ];
649 geraldo 341
 
66 efrain 342
            return new JsonModel($data);
343
        }
649 geraldo 344
 
66 efrain 345
        return new JsonModel($data);
346
    }
649 geraldo 347
 
348
    public function editAction() {
66 efrain 349
        $currentUserPlugin = $this->plugin('currentUserPlugin');
350
        $currentUser = $currentUserPlugin->getUser();
351
        $currentCompany = $currentUserPlugin->getCompany();
649 geraldo 352
 
66 efrain 353
        $request = $this->getRequest();
354
        $uuid = $this->params()->fromRoute('id');
355
 
649 geraldo 356
 
357
        if (!$uuid) {
66 efrain 358
            $data = [
649 geraldo 359
                'success' => false,
360
                'data' => 'ERROR_INVALID_PARAMETER'
66 efrain 361
            ];
649 geraldo 362
 
66 efrain 363
            return new JsonModel($data);
364
        }
365
 
366
        $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
367
        $jobDescription = $jobDescriptionMapper->fetchOneByUuid($uuid);
649 geraldo 368
        if (!$jobDescription) {
66 efrain 369
            $data = [
649 geraldo 370
                'success' => false,
371
                'data' => 'ERROR_RECORD_NOT_FOUND'
66 efrain 372
            ];
649 geraldo 373
 
66 efrain 374
            return new JsonModel($data);
375
        }
649 geraldo 376
 
377
        if ($jobDescription->company_id != $currentCompany->id) {
66 efrain 378
            $data = [
649 geraldo 379
                'success' => false,
380
                'data' => 'ERROR_UNAUTHORIZED'
66 efrain 381
            ];
649 geraldo 382
 
66 efrain 383
            return new JsonModel($data);
384
        }
385
 
649 geraldo 386
 
387
        if ($request->isPost()) {
66 efrain 388
            $form = new JobDescriptionForm($this->adapter, $currentCompany->id, $jobDescription->id);
389
            $dataPost = $request->getPost()->toArray();
649 geraldo 390
 
391
 
66 efrain 392
            $form->setData($dataPost);
649 geraldo 393
 
394
            if ($form->isValid()) {
66 efrain 395
                $dataPost = (array) $form->getData();
396
                $dataPost['status'] = $dataPost['status'] ? $dataPost['status'] : JobDescription::STATUS_INACTIVE;
649 geraldo 397
 
398
 
399
                if (!empty($dataPost['job_description_id_boss'])) {
400
 
66 efrain 401
                    $jobDescriptionBoss = $jobDescriptionMapper->fetchOneByUuid($dataPost['job_description_id_boss']);
649 geraldo 402
                    if ($jobDescriptionBoss) {
403
                        if ($jobDescriptionBoss->id != $jobDescription->id && $jobDescriptionBoss->company_id == $currentCompany->id) {
66 efrain 404
                            $dataPost['job_description_id_boss'] = $jobDescriptionBoss->id;
405
                        } else {
406
                            $dataPost['job_description_id_boss'] = null;
407
                        }
408
                    } else {
409
                        $dataPost['job_description_id_boss'] = null;
410
                    }
411
                }
649 geraldo 412
 
413
 
414
 
66 efrain 415
                $hydrator = new ObjectPropertyHydrator();
416
                $hydrator->hydrate($dataPost, $jobDescription);
417
 
418
                $result = $jobDescriptionMapper->update($jobDescription);
649 geraldo 419
 
420
                if ($result) {
421
 
66 efrain 422
                    $jobDescriptionSubordinateMapper = JobDescriptionSubordinateMapper::getInstance($this->adapter);
423
                    $jobDescriptionSubordinateMapper->deleteAllByJobDescriptionIdTopLevel($jobDescription->id);
649 geraldo 424
 
66 efrain 425
                    $jobDescriptionCompetencyMapper = JobDescriptionCompetencyMapper::getInstance($this->adapter);
426
                    $jobDescriptionCompetencyMapper->deleteAllBJobDescriptionId($jobDescription->id);
649 geraldo 427
 
428
 
66 efrain 429
                    $competencyMapper = CompetencyMapper::getInstance($this->adapter);
430
                    $competencies = $competencyMapper->fetchAllActiveByCompanyId($currentCompany->id);
649 geraldo 431
 
432
 
433
                    foreach ($competencies as $competency) {
66 efrain 434
                        $level = filter_var($this->params()->fromPost('competency_level' . $competency->uuid, ''), FILTER_SANITIZE_NUMBER_INT);
649 geraldo 435
                        if (in_array($level, [1, 2, 3, 4])) {
66 efrain 436
                            $jobDescriptionCompetency = new JobDescriptionCompetency();
437
                            $jobDescriptionCompetency->competency_id = $competency->id;
438
                            $jobDescriptionCompetency->job_description_id = $jobDescription->id;
439
                            $jobDescriptionCompetency->level = $level;
440
                            $jobDescriptionCompetencyMapper->insert($jobDescriptionCompetency);
441
                        }
442
                    }
649 geraldo 443
 
444
 
66 efrain 445
                    $jobsDescriptionListingSubordinate = $jobDescriptionMapper->fetchAllActiveByCompanyIdWhereIdNotEqual($currentCompany->id, $jobDescription->id);
649 geraldo 446
                    foreach ($jobsDescriptionListingSubordinate as $jobDescriptionListingSubordinate) {
66 efrain 447
                        $job_description_subordinate = filter_var($this->params()->fromPost('job_description_id_subordinate' . $jobDescriptionListingSubordinate->uuid, ''), FILTER_SANITIZE_STRING);
649 geraldo 448
                        if ($job_description_subordinate) {
66 efrain 449
                            $jobDescriptionSubordinate = new JobDescriptionSubordinate();
450
                            $jobDescriptionSubordinate->job_description_id_top_level = $jobDescription->id;
451
                            $jobDescriptionSubordinate->job_description_id_low_level = $jobDescriptionListingSubordinate->id;
452
                            $jobDescriptionSubordinateMapper->insert($jobDescriptionSubordinate);
453
                        }
454
                    }
649 geraldo 455
 
456
 
66 efrain 457
                    $this->logger->info('Se actualizo el cargo ' . $jobDescription->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
649 geraldo 458
 
66 efrain 459
                    $data = [
460
                        'success' => true,
461
                        'data' => 'LABEL_RECORD_UPDATED'
462
                    ];
463
                } else {
464
                    $data = [
649 geraldo 465
                        'success' => false,
466
                        'data' => $jobDescriptionMapper->getError()
66 efrain 467
                    ];
468
                }
649 geraldo 469
 
66 efrain 470
                return new JsonModel($data);
471
            } else {
472
                $messages = [];
473
                $form_messages = (array) $form->getMessages();
649 geraldo 474
                foreach ($form_messages as $fieldname => $field_messages) {
66 efrain 475
                    $messages[$fieldname] = array_values($field_messages);
476
                }
649 geraldo 477
 
66 efrain 478
                return new JsonModel([
649 geraldo 479
                    'success' => false,
480
                    'data' => $messages
66 efrain 481
                ]);
482
            }
483
        } else if ($request->isGet()) {
484
 
485
            $hydrator = new ObjectPropertyHydrator();
649 geraldo 486
 
66 efrain 487
            $data = $hydrator->extract($jobDescription);
649 geraldo 488
 
489
            if (!empty($data['job_description_id_boss'])) {
490
 
66 efrain 491
                $jobDescriptionBoss = $jobDescriptionMapper->fetchOne($data['job_description_id_boss']);
649 geraldo 492
                if ($jobDescriptionBoss) {
66 efrain 493
                    $data['job_description_id_boss'] = $jobDescriptionBoss->uuid;
494
                } else {
495
                    $data['job_description_id_boss'] = '';
496
                }
497
            } else {
498
                $data['job_description_id_boss'] = '';
499
            }
500
 
649 geraldo 501
 
66 efrain 502
            $data['competency_types'] = [];
649 geraldo 503
 
66 efrain 504
            $competencyTypeMapper = CompetencyTypeMapper::getInstance($this->adapter);
505
            $records = $competencyTypeMapper->fetchAllByCompanyId($currentCompany->id);
649 geraldo 506
 
66 efrain 507
            $competencyTypesUUID = [];
649 geraldo 508
            foreach ($records as $record) {
66 efrain 509
                array_push($data['competency_types'], [
510
                    'competency_type_id' => $record->uuid,
511
                    'name' => $record->name,
512
                ]);
649 geraldo 513
 
66 efrain 514
                $competencyTypesUUID[$record->id] = $record->uuid;
515
            }
649 geraldo 516
 
66 efrain 517
            $data['competencies'] = [];
649 geraldo 518
 
519
 
66 efrain 520
            $jobDescriptionCompetencyMapper = JobDescriptionCompetencyMapper::getInstance($this->adapter);
649 geraldo 521
 
66 efrain 522
            $competencyMapper = CompetencyMapper::getInstance($this->adapter);
523
            $records = $competencyMapper->fetchAllActiveByCompanyId($currentCompany->id);
649 geraldo 524
 
525
 
526
            foreach ($records as $record) {
527
                if (isset($competencyTypesUUID[$record->competency_type_id])) {
528
 
529
 
66 efrain 530
                    $level = 0;
531
                    $jobDescriptionCompetency = $jobDescriptionCompetencyMapper->fetchOneByJobDescriptionIdAndCompetencyId($jobDescription->id, $record->id);
649 geraldo 532
                    if ($jobDescriptionCompetency) {
66 efrain 533
                        $level = $jobDescriptionCompetency->level;
534
                    }
649 geraldo 535
 
536
 
66 efrain 537
                    array_push($data['competencies'], [
538
                        'competency_type_id' => $competencyTypesUUID[$record->competency_type_id],
539
                        'competency_id' => $record->uuid,
540
                        'name' => $record->name,
541
                        'level' => $level
542
                    ]);
543
                }
544
            }
649 geraldo 545
 
66 efrain 546
            $data['jobs_description'] = [];
649 geraldo 547
 
66 efrain 548
            $jobsDescriptionUUID = [];
549
            $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
550
            $records = $jobDescriptionMapper->fetchAllActiveByCompanyId($currentCompany->id);
649 geraldo 551
 
552
            foreach ($records as $record) {
553
                if ($record->id == $jobDescription->id) {
66 efrain 554
                    continue;
555
                }
649 geraldo 556
 
557
                $jobsDescriptionUUID[$record->id] = $record->uuid;
558
 
559
 
66 efrain 560
                array_push($data['jobs_description'], [
561
                    'job_description_id' => $record->uuid,
562
                    'name' => $record->name
563
                ]);
564
            }
649 geraldo 565
 
66 efrain 566
            $data['subordinates'] = [];
567
            $jobDescriptionSubordinateMapper = JobDescriptionSubordinateMapper::getInstance($this->adapter);
568
            $records = $jobDescriptionSubordinateMapper->fetchAllByJobDescriptionIdTopLevel($jobDescription->id);
569
 
649 geraldo 570
 
571
            foreach ($records as $record) {
572
 
573
                if (isset($jobsDescriptionUUID[$record->job_description_id_low_level])) {
574
 
575
 
66 efrain 576
                    array_push($data['subordinates'], $jobsDescriptionUUID[$record->job_description_id_low_level]);
577
                }
578
            }
579
 
649 geraldo 580
 
581
 
66 efrain 582
            $response = [
583
                'success' => true,
584
                'data' => $data
585
            ];
649 geraldo 586
 
66 efrain 587
            return new JsonModel($response);
588
        } else {
589
            $data = [
590
                'success' => false,
591
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
592
            ];
649 geraldo 593
 
66 efrain 594
            return new JsonModel($data);
595
        }
649 geraldo 596
 
66 efrain 597
        return new JsonModel($data);
598
    }
649 geraldo 599
 
600
    public function deleteAction() {
66 efrain 601
        $currentUserPlugin = $this->plugin('currentUserPlugin');
602
        $currentUser = $currentUserPlugin->getUser();
603
        $currentCompany = $currentUserPlugin->getCompany();
649 geraldo 604
 
66 efrain 605
        $request = $this->getRequest();
606
        $uuid = $this->params()->fromRoute('id');
649 geraldo 607
 
608
        if (!$uuid) {
66 efrain 609
            $data = [
649 geraldo 610
                'success' => false,
611
                'data' => 'ERROR_INVALID_PARAMETER'
66 efrain 612
            ];
649 geraldo 613
 
66 efrain 614
            return new JsonModel($data);
615
        }
649 geraldo 616
 
617
 
66 efrain 618
        $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
619
        $jobDescription = $jobDescriptionMapper->fetchOneByUuid($uuid);
649 geraldo 620
        if (!$jobDescription) {
66 efrain 621
            $data = [
649 geraldo 622
                'success' => false,
623
                'data' => 'ERROR_RECORD_NOT_FOUND'
66 efrain 624
            ];
649 geraldo 625
 
66 efrain 626
            return new JsonModel($data);
627
        }
649 geraldo 628
 
629
        if ($jobDescription->company_id != $currentCompany->id) {
66 efrain 630
            $data = [
649 geraldo 631
                'success' => false,
632
                'data' => 'ERROR_UNAUTHORIZED'
66 efrain 633
            ];
649 geraldo 634
 
66 efrain 635
            return new JsonModel($data);
636
        }
637
 
649 geraldo 638
 
639
        if ($request->isPost()) {
66 efrain 640
            $result = $jobDescriptionMapper->delete($jobDescription);
649 geraldo 641
            if ($result) {
66 efrain 642
                $this->logger->info('Se borro el cargo de trabajo ' . $jobDescription->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
649 geraldo 643
 
66 efrain 644
                $data = [
645
                    'success' => true,
646
                    'data' => 'LABEL_RECORD_DELETED'
647
                ];
648
            } else {
649
 
650
                $data = [
649 geraldo 651
                    'success' => false,
652
                    'data' => $jobDescriptionMapper->getError()
66 efrain 653
                ];
654
 
655
                return new JsonModel($data);
656
            }
657
        } else {
658
            $data = [
659
                'success' => false,
660
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
661
            ];
649 geraldo 662
 
66 efrain 663
            return new JsonModel($data);
664
        }
649 geraldo 665
 
66 efrain 666
        return new JsonModel($data);
667
    }
561 geraldo 668
 
649 geraldo 669
    public function reportAction() {
561 geraldo 670
        $currentUserPlugin = $this->plugin('currentUserPlugin');
671
        $currentUser = $currentUserPlugin->getUser();
672
        $currentCompany = $currentUserPlugin->getCompany();
649 geraldo 673
 
561 geraldo 674
        $request = $this->getRequest();
675
        $uuid = $this->params()->fromRoute('id');
676
 
649 geraldo 677
 
678
        if (!$uuid) {
561 geraldo 679
            $data = [
649 geraldo 680
                'success' => false,
681
                'data' => 'ERROR_INVALID_PARAMETER'
561 geraldo 682
            ];
649 geraldo 683
 
561 geraldo 684
            return new JsonModel($data);
685
        }
686
 
687
        $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
688
        $jobDescription = $jobDescriptionMapper->fetchOneByUuid($uuid);
649 geraldo 689
        if (!$jobDescription) {
561 geraldo 690
            $data = [
649 geraldo 691
                'success' => false,
692
                'data' => 'ERROR_RECORD_NOT_FOUND'
561 geraldo 693
            ];
649 geraldo 694
 
561 geraldo 695
            return new JsonModel($data);
696
        }
649 geraldo 697
 
698
        if ($jobDescription->company_id != $currentCompany->id) {
561 geraldo 699
            $data = [
649 geraldo 700
                'success' => false,
701
                'data' => 'ERROR_UNAUTHORIZED'
561 geraldo 702
            ];
649 geraldo 703
 
561 geraldo 704
            return new JsonModel($data);
705
        }
706
 
649 geraldo 707
 
561 geraldo 708
        if ($request->isGet()) {
709
 
789 geraldo 710
            return $this->renderPdf($currentCompany,$jobDescription);
561 geraldo 711
        } else {
712
            $data = [
713
                'success' => false,
714
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
715
            ];
649 geraldo 716
 
561 geraldo 717
            return new JsonModel($data);
718
        }
649 geraldo 719
 
561 geraldo 720
        return new JsonModel($data);
721
    }
788 geraldo 722
    /**
723
     * Render PDF
724
     * @param type $currentCompany
725
     * @param type $jobDescription
726
     * @return type
727
     */
789 geraldo 728
    public function renderPDF($currentCompany, $jobDescription) {
561 geraldo 729
 
730
        //Generate New PDF
789 geraldo 731
        $pdf = new JobPDF();
732
 
803 geraldo 733
 
734
        //get company Data
804 geraldo 735
        $companyMapper = CompanyMapper::getInstance($this->adapter);
803 geraldo 736
        $company = $companyMapper->fetchOne($currentCompany->id);
737
 
738
        $target_path = $this->config['leaderslinked.fullpath.company'] . DIRECTORY_SEPARATOR . $company->uuid;
739
 
740
        $pdf->header = $target_path  . DIRECTORY_SEPARATOR. $company->cover;
798 geraldo 741
        $pdf->footer = $_SERVER['DOCUMENT_ROOT'] . '/pdf/footer_background_blue.jpg';
561 geraldo 742
 
727 geraldo 743
        $pdf->SetMargins(20, 0, 20);
714 geraldo 744
 
561 geraldo 745
        $pdf->AliasNbPages();
746
        $pdf->AddPage();
564 geraldo 747
 
697 geraldo 748
        // Add Title
749
        $pdf->customHeader();
654 geraldo 750
 
649 geraldo 751
        // Job Description Name
654 geraldo 752
 
649 geraldo 753
        $job_descrition[] = array(
654 geraldo 754
            'title' => 'Categoría Laboral:',
709 geraldo 755
            'content' => ' ' . $jobDescription->name
649 geraldo 756
        );
654 geraldo 757
 
764 geraldo 758
        $pdf->borderTable('Nombre del cargo de trabajo:', $job_descrition);
649 geraldo 759
 
760
 
654 geraldo 761
        //Hierarchical Relationship
762
 
650 geraldo 763
        $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
714 geraldo 764
 
765
        $jobDescriptionSubordinateMapper = JobDescriptionSubordinateMapper::getInstance($this->adapter);
724 geraldo 766
        $jobDescriptionSubordinate = $jobDescriptionSubordinateMapper->fetchAllByJobDescriptionIdTopLevel($jobDescription->id);
714 geraldo 767
 
768
        $subordinate = '';
745 geraldo 769
 
770
        if ($jobDescriptionSubordinate) {
771
 
772
            foreach ($jobDescriptionSubordinate as $rs) {
725 geraldo 773
                $jobSubordinate = $jobDescriptionMapper->fetchOne($rs->job_description_id_low_level);
745 geraldo 774
                if ($jobSubordinate) {
775
                    $subordinate .= $jobSubordinate->name . ' ';
776
                }
717 geraldo 777
            }
778
        }
714 geraldo 779
 
649 geraldo 780
        $jobDescriptionBoss = $jobDescriptionMapper->fetchOne($jobDescription->job_description_id_boss);
781
 
654 geraldo 782
        $relationship = [
614 geraldo 783
            array(
714 geraldo 784
                'title' => 'Depende de: ',
726 geraldo 785
                'content' => $subordinate ? ' ' . $subordinate : ' No aplica'
649 geraldo 786
            ),
787
            array(
788
                'title' => 'Supervisa a: ',
709 geraldo 789
                'content' => $jobDescriptionBoss ? ' ' . $jobDescriptionBoss->name : ' No aplica'
649 geraldo 790
            )
654 geraldo 791
        ];
579 geraldo 792
 
701 geraldo 793
        $pdf->borderTable('Relación jerárquica:', $relationship);
628 geraldo 794
 
654 geraldo 795
        // Objetives
628 geraldo 796
 
654 geraldo 797
        $objetives[] = array('content' => strip_tags($jobDescription->objectives));
708 geraldo 798
        $pdf->borderTable('Objetivos:', $objetives);
653 geraldo 799
 
654 geraldo 800
        // Functions
628 geraldo 801
 
654 geraldo 802
        $functions[] = array('content' => strip_tags($jobDescription->functions));
708 geraldo 803
        $pdf->borderTable('Funciones principales:', $functions);
628 geraldo 804
 
749 geraldo 805
        // add section signature
709 geraldo 806
        $pdf->sectionSignature();
807
 
654 geraldo 808
        //Competencies
628 geraldo 809
 
658 geraldo 810
        $jobDescriptionCompetencyMapper = JobDescriptionCompetencyMapper::getInstance($this->adapter);
667 geraldo 811
        $jobDescriptionCompetency = $jobDescriptionCompetencyMapper->fetchByJobDescriptionId($jobDescription->id);
649 geraldo 812
 
672 geraldo 813
        $competencyMapper = CompetencyMapper::getInstance($this->adapter);
751 geraldo 814
 
815
        $countCompetencies = count($jobDescriptionCompetency);
672 geraldo 816
 
751 geraldo 817
        if ($countCompetencies != 0) {
748 geraldo 818
 
819
            // add new page
820
 
821
            $pdf->AddPage();
679 geraldo 822
 
745 geraldo 823
            $competencies_header[] = array('content' => 'De acuerdo a la siguiente escala, se deberá asignar un valor a cada conducta deseable correspondiente a cada una de las competencias requeridas para el cargo. La valoración refiere a las conductas deseables requeridas en cada cargo, y no refiere a la persona que lo ocupa');
679 geraldo 824
 
745 geraldo 825
            $pdf->singleTable('Competencias requeridas para el cargo:', $competencies_header);
709 geraldo 826
 
745 geraldo 827
 
828
            // Add scale section
829
            $pdf->sectionScale();
830
 
831
            $i = 0;
832
            foreach ($jobDescriptionCompetency as $record) {
833
 
834
                $i = $i + 1;
835
 
836
                $competency = $competencyMapper->fetchOne($record->competency_id);
753 geraldo 837
                $pdf->competencyTable($i, $competency, $record->level);
745 geraldo 838
 
752 geraldo 839
                if ($i % 3 == 0 && $i < $countCompetencies) {
745 geraldo 840
                    $pdf->AddPage();
841
                }
709 geraldo 842
            }
747 geraldo 843
 
669 geraldo 844
        }
658 geraldo 845
 
846
 
561 geraldo 847
        return $pdf->Output();
848
    }
649 geraldo 849
 
66 efrain 850
}