-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDiligence.php
195 lines (170 loc) · 6.84 KB
/
Diligence.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
namespace Diligence\Repositories;
use Diligence\Entities\Tado;
use MapasCulturais\App;
use Diligence\Entities\Diligence as DiligenceEntity;
use MapasCulturais\Entities\Registration;
class Diligence{
static public function findBy($className = 'Diligence\Entities\Diligence', $array): array
{
$app = App::i();
$entity = $app->em->getRepository($className)->findBy($array);
if(count($entity) > 0){
return $entity;
}
return $entity;
}
static public function getRegistrationAgentOpenAndAgent($number, $agentOpen, $agent): array
{
$app = App::i();
$reg = $app->repo('Registration')->find($number);
$openAgent = $app->repo('Agent')->find($agentOpen);
$agent = $app->repo('Agent')->find($agent);
return ['reg' => $reg, 'openAgent' => $openAgent, 'agent' => $agent];
}
public function findId($diligence): object
{
$app = App::i();
return $app->em->getRepository('Diligence\Entities\Diligence')->find($diligence);
}
/**
* Retorna resposta e diligencia
*/
public static function getDiligenceAnswer(int $registration, bool $sentDiligence = false, bool $sentAnswer = false): ?array
{
$registrationAnswer = $registration;
$app = App::i();
//Verificando se tem resposta para se relacionar a diligencia
$dql = "SELECT ad, d
FROM Diligence\Entities\Diligence d
LEFT JOIN Diligence\Entities\AnswerDiligence ad
WITH ad.diligence = d AND ad.registration = :regAnswer AND ad.status >= :statusAnswer
WHERE d.registration = :reg
AND d.status >= :statusDiligence
ORDER BY d.sendDiligence DESC, ad.createTimestamp DESC";
$query = $app->em->createQuery($dql)
->setParameters([
'reg' => $registration,
'regAnswer' => $registrationAnswer,
'statusAnswer' => $sentAnswer ? 1 : 0,
'statusDiligence' => $sentDiligence ? 1 : 0,
]);
$diligenceAndAnswers = $query->getResult();
if (empty($diligenceAndAnswers)) {
return null;
}
return $diligenceAndAnswers;
}
static function getAuthorizedProject($registration): array
{
$app = App::i();
$reg = $app->repo('Registration')->find($registration);
$optionAuthorized = $reg->getMetadata('option_authorized');
$valueAuthorized = $reg->getMetadata('value_project_diligence');
return [
'optionAuthorized' => $optionAuthorized,
'valueAuthorized' => $valueAuthorized
];
}
static public function getFilesDiligence($diligence): array
{
$app = App::i();
$params = [
"object_type" => "Diligence\Entities\Diligence",
"object_id" => $diligence,
"grp" => "answer-diligence"
];
$query = "SELECT * FROM file WHERE object_type = :object_type and object_id = :object_id and grp = :grp";
$conn = $app->em->getConnection();
$result = $conn->fetchAllAssociative($query, $params);
return $result;
}
public static function getTado($registratrion): ?Tado
{
$app = App::i();
$tado = $app->repo('Diligence\Entities\Tado')->findOneBy([
'registration' => $registratrion
]);
return $tado;
}
/**
* Buscando a ultima diligência relacionado a inscrição Desejada
*
* @param [int|string] $registration
* @return DiligenceEntity
*/
public function getIdLastDiligence($registration) : DiligenceEntity
{
$app = App::i();
$lastDiligence = $app->repo(DiligenceEntity::class)->findOneBy(['registration' => $registration], ['id' => 'desc']);
return $lastDiligence;
}
//Verifica se tem acesso ao relatório financeiro da PC
public function verifyAcessReport(Registration $registration): bool
{
$app = App::i();
$user = $app->user;
$hasAccess = false;
//Se o usuário logado tem permissão de avaliador da inscrição
if($registration->canUser('evaluate', $user)){
$hasAccess = true;
//se o usuario logado é o mesmo dono da inscrição
}elseif( $user->id == $registration->owner->user->id ){
$hasAccess = true;
//Verifica se faz parte do grupo de admin e se é o usuário logado
}else{
foreach ($registration->opportunity->agentRelations as $managers) {
if(
$managers->group == "group-admin" &&
isset($managers->agent->id) &&
$managers->agent->id == $user->profile->id
){
$hasAccess = true;
}
}
}
// Se o usuário não tem permissão, redireciona com mensagem de erro
if(!$hasAccess){
$_SESSION['error'] = "Ops! Você não tem permissão para acessar esse relatório financeiro";
$app->redirect($app->baseUrl.'panel', 403);
}
return $hasAccess;
}
/**
* Função que busca se tem registro do status do PC e retorna o valor para preenchimento
* do select na view
*/
public function getSituacionPC(Registration $registration) : string
{
$app = App::i();
$entity = $app->repo('Registration')->find($registration->id);
//Se não tiver metadata retorna falso
return !is_null($entity->getMetadata('situacion_diligence')) ? $entity->getMetadata('situacion_diligence') : 'all';
}
public static function getFinancialReportsAccountability($registration_id)
{
$app = App::i();
$result = $app->repo('RegistrationFile')->findBy([
'owner' => $registration_id,
'group' => 'financial-report-accountability'
]);
return $result;
}
/**
* Verifica se quem está logado tem controle na opp. e se é o fiscal de uma diligência
* @param $registration
* @return DiligenceEntity|null
*/
public static function getIsAuditor($registration)
{
$app = App::i();
$auditorDiligence = $app->repo('Diligence\Entities\Diligence')->findOneBy(['registration' => $registration], ['id' => 'desc']);
$reg = $app->repo('Registration')->find($registration);
$isAdmin = $reg->opportunity->canUser("@control", $app->user);
if ($auditorDiligence && $auditorDiligence->openAgent->userId !== $app->user->id && !$isAdmin) {
$app->setCookie("denied-auditor", 'Esse monitoramento já está sendo acompanhado por outro Fiscal', time() + 3600);
$app->redirect($app->createUrl('oportunidade', $auditorDiligence->registration->opportunity->id) . '#/tab=evaluations');
}
return $auditorDiligence;
}
}