Proyectos de Subversion LeadersLinked - Backend

Rev

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