Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev Autor Línea Nro. Línea
1 www 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Controller;
5
 
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
16766 efrain 8
use LeadersLinked\Cache\CacheInterface;
1 www 9
use Laminas\Mvc\Controller\AbstractActionController;
10
use Laminas\Log\LoggerInterface;
11
use Laminas\View\Model\ViewModel;
12
use Laminas\View\Model\JsonModel;
13
use LeadersLinked\Library\Functions;
14
use LeadersLinked\Mapper\CompanyMapper;
15
use LeadersLinked\Model\Company;
16
use LeadersLinked\Mapper\ServiceMapper;
17
use LeadersLinked\Mapper\CompanyServiceMapper;
18
use LeadersLinked\Mapper\RoleMapper;
19
use LeadersLinked\Mapper\CompanyRoleMapper;
20
use LeadersLinked\Mapper\CompanyUserMapper;
21
use LeadersLinked\Mapper\UserMapper;
22
use LeadersLinked\Model\CompanyService;
23
use LeadersLinked\Mapper\CompanyUserRoleMapper;
24
use LeadersLinked\Model\Role;
25
use LeadersLinked\Model\CompanyRole;
26
use LeadersLinked\Model\CompanyUserRole;
27
use LeadersLinked\Model\CompanyUser;
28
use Google\Service\Directory\UserMakeAdmin;
15338 efrain 29
use LeadersLinked\Mapper\NetworkMapper;
15540 efrain 30
use LeadersLinked\Model\Service;
1 www 31
 
32
class CompanyController extends AbstractActionController
33
{
34
    /**
35
     *
36
     * @var AdapterInterface
37
     */
38
    private $adapter;
39
 
40
 
41
    /**
42
     *
16766 efrain 43
     * @var CacheInterface
1 www 44
     */
45
    private $cache;
46
 
47
    /**
48
     *
49
     * @var  LoggerInterface
50
     */
51
    private $logger;
52
 
53
    /**
54
     *
55
     * @var array
56
     */
57
    private $config;
58
 
59
 
60
 
61
    /**
62
     *
63
     * @param AdapterInterface $adapter
16766 efrain 64
     * @param CacheInterface $cache
1 www 65
     * @param LoggerInterface $logger
66
     * @param array $config
67
     */
68
    public function __construct($adapter, $cache , $logger, $config)
69
    {
70
        $this->adapter      = $adapter;
71
        $this->cache        = $cache;
72
        $this->logger       = $logger;
73
        $this->config       = $config;
74
 
75
 
76
    }
77
 
78
    public function indexAction()
79
    {
80
        $request = $this->getRequest();
81
        $currentUserPlugin = $this->plugin('currentUserPlugin');
82
        $currentUser    = $currentUserPlugin->getUser();
83
 
15351 efrain 84
 
85
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
86
        $network = $currentNetworkPlugin->getNetwork();
87
 
1 www 88
        if($request->isGet()) {
89
 
90
 
91
            $headers  = $request->getHeaders();
92
 
93
            $isJson = false;
94
            if($headers->has('Accept')) {
95
                $accept = $headers->get('Accept');
96
 
97
                $prioritized = $accept->getPrioritized();
98
 
99
                foreach($prioritized as $key => $value) {
100
                    $raw = trim($value->getRaw());
101
 
102
                    if(!$isJson) {
103
                        $isJson = strpos($raw, 'json');
104
                    }
105
 
106
                }
107
            }
108
 
16299 efrain 109
            //$isJson = true;
1 www 110
            if($isJson) {
111
 
112
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
113
                $allowServices = $acl->isAllowed($currentUser->usertype_id, 'companies/services');
114
                $allowRoles = $acl->isAllowed($currentUser->usertype_id, 'companies/roles');
115
                $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'companies/edit');
116
 
117
 
118
                $serviceMapper = ServiceMapper::getInstance($this->adapter);
119
                $records = $serviceMapper->fetchAll();
120
 
121
                $services = [];
122
                foreach($records as $record)
123
                {
124
                    $services[ $record->id ] = $record->name;
125
                }
126
 
127
 
128
                $roleMapper = RoleMapper::getInstance($this->adapter);
129
                $records = $roleMapper->fetchAll();
130
 
131
                $roles = [];
132
                foreach($records as $record)
133
                {
134
                    $roles[ $record->id ] = $record->name;
135
                }
15338 efrain 136
 
137
                $networkMapper = NetworkMapper::getInstance($this->adapter);
138
                $records = $networkMapper->fetchAll();
139
 
140
                $networks = [];
141
                foreach($records as $record)
142
                {
16299 efrain 143
                    $networks[ $record->id ] = $record;
15338 efrain 144
                }
16299 efrain 145
 
15338 efrain 146
 
1 www 147
 
148
 
149
                $search = $this->params()->fromQuery('search', []);
16766 efrain 150
                $search = empty($search['value']) ? '' :  Functions::sanitizeFilterString($search['value']);
1 www 151
 
152
                $page               = intval($this->params()->fromQuery('start', 1), 10);
153
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
154
                $order =  $this->params()->fromQuery('order', []);
155
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
16766 efrain 156
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(Functions::sanitizeFilterString($order[0]['dir']));
1 www 157
 
158
                $fields =  ['name'];
159
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'name';
160
 
161
                if(!in_array($order_direction, ['ASC', 'DESC'])) {
162
                    $order_direction = 'ASC';
163
                }
164
 
16766 efrain 165
                $status = Functions::sanitizeFilterString($this->params()->fromQuery('status'));
1 www 166
                if(!in_array($status, [
167
                    Company::STATUS_ACTIVE,
168
                    Company::STATUS_DELETED,
169
                    Company::STATUS_INACTIVE,
170
                    Company::STATUS_PENDING,
171
                    Company::STATUS_REJECTED
172
                ])) {
173
                    $status = '';
174
                }
175
 
16766 efrain 176
                $network_id = Functions::sanitizeFilterString($this->params()->fromQuery('network_id'));
15338 efrain 177
 
178
                $network = $networkMapper->fetchOneByUuid($network_id);
179
                if($network) {
180
                    $network_id = $network->id;
181
                } else {
182
                    $network_id = 0;
183
                }
184
 
1 www 185
 
186
                $companyMapper = CompanyMapper::getInstance($this->adapter);
16767 efrain 187
                $paginator = $companyMapper->fetchAllDataTable($search, $status, $network_id, $page,  $records_x_page, $order_field, $order_direction);
1 www 188
 
189
                $companyServiceMapper = CompanyServiceMapper::getInstance($this->adapter);
190
                $companyRoleMapper = CompanyRoleMapper::getInstance($this->adapter);
191
                $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
192
                $userMapper = UserMapper::getInstance($this->adapter);
193
 
15351 efrain 194
 
1 www 195
 
196
                $items = [];
197
                $records = $paginator->getCurrentItems();
198
                foreach($records as $record)
199
                {
200
                    $security = [
201
                        'first_name' => '',
202
                        'last_name' => '',
203
                        'email' => '',
204
                    ];
205
 
206
                    $companyUser = $companyUserMapper->fetchCreatorByCompanyId($record->id);
207
                    if($companyUser) {
208
                        $user = $userMapper->fetchOne($companyUser->user_id);
209
                        if($user) {
210
                            $security['first_name'] = $user->first_name;
211
                            $security['last_name'] = $user->last_name;
212
                            $security['email'] = $user->email;
213
                        }
214
                    }
215
 
216
                    $companyServices = $companyServiceMapper->fetchAllByCompanyId($record->id);
217
                    $companyRoles = $companyRoleMapper->fetchAllByCompanyId($record->id);
218
 
219
 
220
                    $details['services'] = [] ;
221
 
222
                    foreach($companyServices as $companyService)
223
                    {
224
                        if($companyService->status == CompanyService::ACTIVE) {
225
 
226
 
227
                            array_push($details['services'], $services[$companyService->service_id]);
228
                        }
229
                    }
230
 
231
                    $details['roles'] = [] ;
232
 
233
                    foreach($companyRoles as $companyRole)
234
                    {
235
                        array_push($details['roles'], $roles[$companyRole->role_id]);
236
                    }
237
 
238
 
15358 efrain 239
 
1 www 240
 
15358 efrain 241
 
1 www 242
                    $details['internal'] = $record->internal == Company::INTERNAL_YES ? 'LABEL_YES' : 'LABEL_NO';
243
 
244
 
245
                    $link_profile = '';
246
                    switch($record->status)
247
                    {
248
                        case Company::STATUS_PENDING :
249
                            $details['status'] = 'LABEL_PENDING';
250
 
251
                            break;
252
 
253
                        case Company::STATUS_ACTIVE :
15358 efrain 254
                            $link_profile = 'https://'. $networks[$record->network_id]->main_hostname . '/company/view/' . $record->uuid;
1 www 255
                            $details['status'] = 'LABEL_ACTIVE';
256
                            break;
257
 
258
                        case Company::STATUS_INACTIVE :
259
                            $details['status'] = 'LABEL_INACTIVE';
260
                            break;
261
 
262
                        case Company::STATUS_REJECTED :
263
                            $details['status'] = 'LABEL_REJECTED';
264
                            break;
265
 
266
                        case Company::STATUS_DELETED :
267
                            $details['status'] = 'LABEL_DELETED';
268
                            break;
269
 
270
                        default :
271
                            $details['status'] = 'LABEL_UNKNOWN';
272
                            break;
273
                    }
274
 
275
 
276
                    $item = [
277
                        'id' => $record->id,
278
                        'name' => $record->name,
15358 efrain 279
                        'network' => $networks[$record->network_id]->name,
1 www 280
                        'link_profile' => $link_profile,
281
                        'details' => $details,
282
                        'security' => $security,
283
                        'actions' => [
284
                           'link_services' => $allowServices ? $this->url()->fromRoute('companies/services', ['id' => $record->uuid ]) : '',
285
                           'link_roles' => $allowRoles ? $this->url()->fromRoute('companies/roles', ['id' => $record->uuid ]) : '',
286
                           'link_edit' => $allowEdit ? $this->url()->fromRoute('companies/edit', ['id' => $record->uuid ]) : '',
287
                        ]
288
 
289
                    ];
290
 
291
                    array_push($items, $item);
292
                }
293
 
294
                return new JsonModel([
295
                    'success' => true,
296
                    'data' => [
297
                        'items' => $items,
298
                        'total' => $paginator->getTotalItemCount(),
299
                    ]
300
                ]);
301
            } else  {
302
 
15338 efrain 303
                $networkMapper = NetworkMapper::getInstance($this->adapter);
304
                $records = $networkMapper->fetchAll();
305
 
306
                $networks = [];
307
                foreach($records as $record)
308
                {
309
                    $networks[ $record->uuid ] = $record->name;
310
                }
311
 
312
 
313
 
1 www 314
                $this->layout()->setTemplate('layout/layout-backend');
315
                $viewModel = new ViewModel();
316
                $viewModel->setTemplate('leaders-linked/companies/index.phtml');
15338 efrain 317
                $viewModel->setVariables(['networks' => $networks ]);
1 www 318
                return $viewModel ;
319
            }
320
 
321
        } else {
322
            return new JsonModel([
323
                'success' => false,
324
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
325
            ]);
326
        }
327
    }
328
 
329
    public function servicesAction()
330
    {
331
        $currentUserPlugin = $this->plugin('currentUserPlugin');
332
        $currentUser = $currentUserPlugin->getUser();
333
 
334
        $request = $this->getRequest();
335
        $uuid = $this->params()->fromRoute('id');
336
 
337
 
338
        if(!$uuid) {
339
            $data = [
340
                'success'   => false,
341
                'data'   => 'ERROR_INVALID_PARAMETER'
342
            ];
343
 
344
            return new JsonModel($data);
345
        }
346
 
347
        $companyMapper = CompanyMapper::getInstance($this->adapter);
348
        $company = $companyMapper->fetchOneByUuid($uuid);
349
        if(!$company) {
350
            $data = [
351
                'success'   => false,
352
                'data'   => 'ERROR_COMPANY_NOT_FOUND'
353
            ];
354
 
355
            return new JsonModel($data);
356
        }
357
 
358
        if($request->isPost()) {
359
 
360
            $companyServiceMapper = CompanyServiceMapper::getInstance($this->adapter);
361
 
362
            $serviceMapper = ServiceMapper::getInstance($this->adapter);
363
            $services = $serviceMapper->fetchAll();
364
 
365
            foreach($services as $service)
366
            {
16766 efrain 367
                $status     = Functions::sanitizeFilterString($this->params()->fromPost('status' . $service->id));
368
                $paid_from  = Functions::sanitizeFilterString($this->params()->fromPost('paid_from' . $service->id));
369
                $paid_to    = Functions::sanitizeFilterString($this->params()->fromPost('paid_to' . $service->id));
1 www 370
 
371
                $ok = true;
372
                switch ($status) {
373
 
374
                    case CompanyService::ACTIVE :
375
                    case CompanyService::CANCELLED :
376
                    case CompanyService::SUSPENDED :
377
 
378
                        $dt_paid_from = \DateTime::createFromFormat('d/m/Y', $paid_from);
379
                        $dt_paid_to = \DateTime::createFromFormat('d/m/Y', $paid_to);
380
 
381
                        if(!$dt_paid_from || !$dt_paid_to) {
382
                            $ok = false;
383
                        }  else {
384
                            if($dt_paid_from->getTimestamp() > $dt_paid_to->getTimestamp()) {
385
                                $ok = false;
386
                            } else {
387
                                $dt_paid_from->setTime(0, 0, 0);
388
                                $paid_from = $dt_paid_from->format('Y-m-d');
389
 
390
 
391
                                $dt_paid_to->setTime(23, 59, 59);
392
                                $paid_to = $dt_paid_to->format('Y-m-d');
393
                            }
394
                        }
395
 
396
                        break;
397
 
398
                    case CompanyService::INACTIVE :
399
                        $paid_from = null;
400
                        $paid_to = null;
401
                        break;
402
 
403
                    default :
404
                        $ok = false;
405
                        break;
406
 
407
 
408
                }
409
 
410
                if(!$ok) {
411
                    return new JsonModel([
412
                        'success' => false,
413
                        'data' => 'ERROR_PARAMETERS_ARE_INVALID'
414
                    ]);
415
                }
416
 
417
 
418
                $companyService = $companyServiceMapper->fetchOneByCompanyIdAndServiceId($company->id, $service->id);
419
                if($companyService) {
420
 
421
                    $companyService->status = $status;
422
                    $companyService->paid_from = $paid_from;
423
                    $companyService->paid_to = $paid_to;
424
 
425
                    if(!$companyServiceMapper->update($companyService)) {
426
                        return new JsonModel([
427
                            'success' => false,
428
                            'data' => $companyServiceMapper->getError()
429
                        ]);
430
                    }
431
 
432
 
433
                } else {
434
                    $companyService = new CompanyService();
435
                    $companyService->service_id = $service->id;
436
                    $companyService->company_id = $company->id;
437
                    $companyService->status = $status;
438
                    $companyService->paid_from = $paid_from;
439
                    $companyService->paid_to = $paid_to;
440
 
441
 
442
                    if(!$companyServiceMapper->insert($companyService)) {
443
                        return new JsonModel([
444
                            'success' => false,
445
                            'data' => $companyServiceMapper->getError()
446
                        ]);
447
                    }
448
                }
449
 
450
 
451
            }
452
 
453
            $this->logger->info('Se actualizo los servicios de la empresa ' . $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
454
 
455
            return new JsonModel([
456
                'success' => true,
457
                'data' => 'LABEL_RECORD_UPDATED'
458
            ]);
459
 
460
 
461
        } else if ($request->isGet()) {
15540 efrain 462
 
463
 
464
 
1 www 465
            $serviceMapper = ServiceMapper::getInstance($this->adapter);
466
            $records = $serviceMapper->fetchAll();
467
 
468
            $services = [];
469
            foreach($records as $record)
470
            {
15540 efrain 471
                if($company->default_for_network == Company::DEFAULT_FOR_NETWORK_NO) {
472
                    if($record->default_company_is_required == Service::DEFAULT_COMPANY_IS_REQUIRED_YES) {
473
                        continue;
474
                    }
475
                }
476
 
477
 
1 www 478
                $services[ $record->id ] = [
479
                    'id'        => $record->id,
480
                    'name'      => $record->name,
481
                    'status'    => '',
482
                    'paid_from' => '',
483
                    'paid_to'   => '',
484
                ];
485
            }
486
 
487
 
488
 
489
            $companyServiceMapper = CompanyServiceMapper::getInstance($this->adapter);
490
            $records = $companyServiceMapper->fetchAllByCompanyId($company->id);
491
 
492
 
493
            foreach($records as $record)
494
            {
495
                $paid_from = '';
496
                if($record->paid_from) {
497
                    $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $record->paid_from);
498
                    if($dt) {
499
                        $paid_from = $dt->format('d/m/Y');
500
                    }
501
 
502
                }
503
 
504
                $paid_to = '';
505
                if($record->paid_to) {
506
                    $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $record->paid_to);
507
                    if($dt) {
508
                        $paid_to = $dt->format('d/m/Y');
509
                    }
510
 
511
                }
512
 
513
 
514
                $services[ $record->service_id ] ['status']  = $record->status;
515
                $services[ $record->service_id ] ['paid_from']  = $paid_from;
516
                $services[ $record->service_id ] ['paid_to']  = $paid_to;
517
 
518
            }
519
 
520
            $data = [
521
                'success' => true,
522
                'data' => $services,
523
            ];
524
 
525
 
526
 
527
            return new JsonModel($data);
528
        } else {
529
            $data = [
530
                'success' => false,
531
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
532
            ];
533
 
534
            return new JsonModel($data);
535
        }
536
 
537
        return new JsonModel($data);
538
    }
539
 
540
 
541
    public function rolesAction()
542
    {
543
        $currentUserPlugin = $this->plugin('currentUserPlugin');
544
        $currentUser = $currentUserPlugin->getUser();
545
 
546
        $request = $this->getRequest();
547
        $uuid = $this->params()->fromRoute('id');
548
 
549
 
550
        if(!$uuid) {
551
            $data = [
552
                'success'   => false,
553
                'data'   => 'ERROR_INVALID_PARAMETER'
554
            ];
555
 
556
            return new JsonModel($data);
557
        }
558
 
559
        $companyMapper = CompanyMapper::getInstance($this->adapter);
560
        $company = $companyMapper->fetchOneByUuid($uuid);
561
        if(!$company) {
562
            $data = [
563
                'success'   => false,
564
                'data'   => 'ERROR_COMPANY_NOT_FOUND'
565
            ];
566
 
567
            return new JsonModel($data);
568
        }
569
 
570
        if($request->isPost()) {
571
 
572
            $companyRoleMapper = CompanyRoleMapper::getInstance($this->adapter);
573
            $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
574
            $companyUserRoleMapper = CompanyUserRoleMapper::getInstance($this->adapter);
575
 
576
            $roleMapper = RoleMapper::getInstance($this->adapter);
577
            $roles = $roleMapper->fetchAll();
578
 
579
 
580
            foreach($roles as $role)
581
            {
582
                $companyRole = $companyRoleMapper->fetchOneByCompanyIdAndRoleId($company->id, $role->id);
583
                $checked     = filter_var( $this->params()->fromPost('checked' . $role->id), FILTER_SANITIZE_NUMBER_INT);
584
 
585
                if($checked) {
586
 
587
                    if(!$companyRole) {
588
                        $companyRole = new CompanyRole();
589
                        $companyRole->company_id = $company->id;
590
                        $companyRole->role_id = $role->id;
591
 
592
                        if(!$companyRoleMapper->insert($companyRole)) {
593
                            return new JsonModel([
594
                                'success' => false,
595
                                'data' => $companyRoleMapper->getError()
596
                            ]);
597
                        }
598
 
599
                    }
600
 
601
                } else {
602
                    if($companyRole) {
603
                        $companyUserRoleMapper->deleteByCompanyIdAndRoleId($company->id, $role->id);
604
                        $companyRoleMapper->deleteByCompanyIdAndRoleId($company->id, $role->id);
605
                    }
606
 
607
                }
608
            }
609
 
610
            $this->logger->info('Se actualizo los roles de la empresa ' . $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
611
 
612
            return new JsonModel([
613
                'success' => true,
614
                'data' => 'LABEL_RECORD_UPDATED'
615
            ]);
616
 
617
 
618
        } else if ($request->isGet()) {
619
            $companyServiceMapper = CompanyServiceMapper::getInstance($this->adapter);
620
 
621
            $roleMapper = RoleMapper::getInstance($this->adapter);
622
            $records = $roleMapper->fetchAll();
623
 
624
 
625
 
626
            $roles = [];
627
            foreach($records as $record)
628
            {
629
                if($record->service_id) {
630
                    $companyService = $companyServiceMapper->fetchOneByCompanyIdAndServiceId($company->id, $record->service_id);
631
 
632
                    if(!$companyService || $companyService->status == CompanyService::INACTIVE) {
633
                        continue;
634
                    }
635
 
636
                }
637
 
638
 
639
 
640
                $roles[ $record->id ] = [
641
                   'id' => $record->id,
642
                   'name' => $record->name,
643
                   'fixed' => $record->creator == Role::CREATOR_YES ? true : false,
644
                   'checked' => $record->creator == Role::CREATOR_YES ? true : false,
645
                ];
646
            }
647
 
648
 
649
            $companyRoleMapper = CompanyRoleMapper::getInstance($this->adapter);
650
            $records = $companyRoleMapper->fetchAllByCompanyId($company->id);
651
 
652
            foreach($records as $record)
653
            {
654
                $roles[ $record->role_id ]['checked'] = true;
655
 
656
            }
657
 
658
            $data = [
659
                'success' => true,
660
                'data' => $roles,
661
            ];
662
 
663
 
664
 
665
            return new JsonModel($data);
666
        } else {
667
            $data = [
668
                'success' => false,
669
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
670
            ];
671
 
672
            return new JsonModel($data);
673
        }
674
 
675
        return new JsonModel($data);
676
    }
677
 
678
    public function editAction()
679
    {
680
        $currentUserPlugin = $this->plugin('currentUserPlugin');
681
        $currentUser = $currentUserPlugin->getUser();
682
 
683
        $request = $this->getRequest();
684
        $uuid = $this->params()->fromRoute('id');
685
 
686
 
687
        if(!$uuid) {
688
            $data = [
689
                'success'   => false,
690
                'data'   => 'ERROR_INVALID_PARAMETER'
691
            ];
692
 
693
            return new JsonModel($data);
694
        }
695
 
696
        $companyMapper = CompanyMapper::getInstance($this->adapter);
697
        $company = $companyMapper->fetchOneByUuid($uuid);
698
        if(!$company) {
699
            $data = [
700
                'success'   => false,
701
                'data'   => 'ERROR_COMPANY_NOT_FOUND'
702
            ];
703
 
704
            return new JsonModel($data);
705
        }
706
 
707
        if($request->isPost()) {
708
 
709
 
16766 efrain 710
            $status = Functions::sanitizeFilterString($this->params()->fromPost('status'));
711
            $user_uuid = Functions::sanitizeFilterString($this->params()->fromPost('user_uuid'));
1 www 712
 
713
 
714
            if(!in_array($status, [
715
                Company::STATUS_ACTIVE,
716
                Company::STATUS_INACTIVE,
717
                Company::STATUS_DELETED,
718
                Company::STATUS_PENDING,
719
                Company::STATUS_REJECTED,
720
 
721
            ])) {
722
                return new JsonModel([
723
                    'success'   => false,
724
                    'data'   => 'ERROR_PARAMETERS_ARE_INVALID'
725
                ]);
726
            }
727
 
728
            $userMapper = UserMapper::getInstance($this->adapter);
729
            $user = $userMapper->fetchOneByUuid($user_uuid);
730
 
731
            if(!$user) {
732
                return new JsonModel([
733
                    'success'   => false,
734
                    'data'   => 'ERROR_USER_NOT_FOUND'
735
                ]);
736
            }
737
 
738
            $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
739
            $companyUser = $companyUserMapper->fetchOneByCompanyIdAndUserId($company->id, $user->id);
740
 
741
            if(!$companyUser) {
742
                return new JsonModel([
743
                    'success'   => false,
744
                    'data'   => 'ERROR_COMPANY_USER_NOT_FOUND'
745
                ]);
746
            }
747
 
748
 
749
            if($companyUser->status != CompanyUser::STATUS_ACCEPTED && $companyUser->status != CompanyUser::STATUS_ADMIN_WILL_ADD) {
750
                return new JsonModel([
751
                    'success'   => false,
752
                    'data'   => 'ERROR_COMPANY_USER_IS_NOT_ACTIVE'
753
                ]);
754
            }
755
 
756
            $creator = $companyUserMapper->fetchCreatorByCompanyId($company->id);
757
            if($creator->user_id != $user->id) {
758
 
759
                $creator->creator = CompanyUser::CREATOR_NO;
760
                $creator->backend = CompanyUser::BACKEND_NO;
761
                $companyUserMapper->update($creator);
762
 
763
                $companyUser->creator = CompanyUser::CREATOR_YES;
764
                $companyUser->backend = CompanyUser::BACKEND_YES;
765
                $companyUserMapper->update($companyUser);
766
 
767
            }
768
 
769
            $company->status = $status;
770
            if($companyMapper->update($company)) {
771
 
772
                $this->logger->info('Se edito la empresa ' . $company->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
773
 
774
                return new JsonModel([
775
                    'success' => true,
776
                    'data' => 'LABEL_RECORD_UPDATED'
777
                ]);
778
            }  else {
779
                return new JsonModel([
780
                    'success' => false,
781
                    'data' => $companyMapper->getError(),
782
                ]);
783
            }
784
 
785
 
786
 
787
 
788
 
789
 
790
        } else if ($request->isGet()) {
791
            $userMapper = UserMapper::getInstance($this->adapter);
792
            $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
793
            $records = $companyUserMapper->fetchAllByCompanyId($company->id);
794
 
795
            $users = [] ;
796
            foreach($records as $record)
797
            {
798
                if($record->owner == CompanyUser::OWNER_YES) {
799
                    continue;
800
                }
801
 
802
 
803
                if($record->status != CompanyUser::STATUS_ACCEPTED && $record->status != CompanyUser::STATUS_ADMIN_WILL_ADD) {
804
                    continue;
805
                }
806
 
807
                $user = $userMapper->fetchOne($record->user_id);
808
 
809
                array_push($users, [
810
                    'uuid' => $user->uuid,
811
                    'name' => trim($user->first_name . ' ' . $user->last_name) . ' (' . $user->email . ')',
812
 
813
                ] );
814
 
815
 
816
 
817
 
818
 
819
 
820
            }
821
 
822
            usort($users, function($a, $b) {
823
                return $a['name'] <=> $b['name'];
824
            });
825
 
826
 
827
            $creator = $companyUserMapper->fetchCreatorByCompanyId($company->id);
828
            if($creator) {
829
                $userMapper = UserMapper::getInstance($this->adapter);
830
                $user = $userMapper->fetchOne($creator->user_id);
831
                if($user) {
832
                    $creator = $user->uuid;
833
                } else {
834
                    $creator = '';
835
                }
836
            } else {
837
                $creator = '';
838
            }
839
 
840
            $data = [
841
                'success' => true,
842
                'data' => [
843
                    'status' => $company->status,
844
                    'users' => $users,
845
                    'creator' => $creator,
846
                ],
847
            ];
848
 
849
 
850
 
851
            return new JsonModel($data);
852
        } else {
853
            $data = [
854
                'success' => false,
855
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
856
            ];
857
 
858
            return new JsonModel($data);
859
        }
860
 
861
        return new JsonModel($data);
862
    }
863
 
864
}
865