|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ControleOnline\Service; |
|
4
|
|
|
|
|
5
|
|
|
use ControleOnline\Entity\Document; |
|
6
|
|
|
use ControleOnline\Entity\DocumentType; |
|
7
|
|
|
use ControleOnline\Entity\Email; |
|
8
|
|
|
use ControleOnline\Entity\ExtraData; |
|
|
|
|
|
|
9
|
|
|
use ControleOnline\Entity\ExtraFields; |
|
|
|
|
|
|
10
|
|
|
use ControleOnline\Entity\Language; |
|
|
|
|
|
|
11
|
|
|
use ControleOnline\Entity\People; |
|
12
|
|
|
use ControleOnline\Entity\PeopleLink; |
|
13
|
|
|
use ControleOnline\Entity\Phone; |
|
14
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
|
|
|
|
|
15
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
|
|
|
|
|
16
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface |
|
|
|
|
|
|
17
|
|
|
as Security; |
|
18
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
|
|
|
|
|
19
|
|
|
use Exception; |
|
20
|
|
|
|
|
21
|
|
|
class PeopleService |
|
22
|
|
|
{ |
|
23
|
|
|
private $request; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct( |
|
26
|
|
|
private EntityManagerInterface $manager, |
|
27
|
|
|
private Security $security, |
|
28
|
|
|
private RequestStack $requestStack, |
|
29
|
|
|
) { |
|
30
|
|
|
$this->request = $requestStack->getCurrentRequest(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function prePersist(People $people) |
|
34
|
|
|
{ |
|
35
|
|
|
$language = $this->manager->getRepository(Language::class)->findOneBy(['language' => 'pt-br']); |
|
36
|
|
|
$people->setLanguage($language); |
|
37
|
|
|
return $people; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function addClient() {} |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
public function discoveryClient(People $provider, People $client) {} |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
public function discoveryPeople(?string $document = null, ?string $email = null, ?array $phone = [], ?string $name = null, ?string $peopleType = null): People |
|
46
|
|
|
{ |
|
47
|
|
|
|
|
48
|
|
|
if (!empty($document)) |
|
49
|
|
|
$people = $this->getDocument($document)?->getPeople(); |
|
50
|
|
|
|
|
51
|
|
|
if (!$people && !empty($email)) |
|
|
|
|
|
|
52
|
|
|
$people = $this->getEmail($email)?->getPeople(); |
|
53
|
|
|
|
|
54
|
|
|
if (!$people && !empty($phone)) |
|
55
|
|
|
$people = $this->getPhone($phone['ddd'], $phone['phone'])?->getPeople(); |
|
56
|
|
|
|
|
57
|
|
|
if (!$people) { |
|
58
|
|
|
$people = new People(); |
|
59
|
|
|
$people->setName($name ?? 'Name not given'); |
|
60
|
|
|
$people->setLanguage($this->manager->getRepository(Language::class)->findOneBy(['language' => 'pt-br'])); |
|
61
|
|
|
$people->setPeopleType($peopleType ?: $this->getPeopleTypeByDocumentLen($document)); |
|
62
|
|
|
$this->manager->persist($people); |
|
63
|
|
|
$this->manager->flush(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
if ($document) |
|
67
|
|
|
$this->addDocument($people, $document); |
|
68
|
|
|
if ($email) |
|
69
|
|
|
$this->addEmail($people, $email); |
|
70
|
|
|
if ($phone) |
|
71
|
|
|
$this->addPhone($people, $phone); |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
return $people; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function addPhone(People $people, array $phone_number): Phone |
|
78
|
|
|
{ |
|
79
|
|
|
$phone = $this->getPhone($phone_number['ddd'], $phone_number['phone']); |
|
80
|
|
|
if ($phone && $phone->getPeople()) { |
|
81
|
|
|
if ($phone->getPeople()->getId() != $people->getId()) |
|
82
|
|
|
throw new Exception("Phone is in use by people " . $people->getId(), 1); |
|
83
|
|
|
} else { |
|
84
|
|
|
$phone = new Phone(); |
|
85
|
|
|
$phone->setDdd($phone_number['ddd']); |
|
86
|
|
|
$phone->setPhone($phone_number['phone']); |
|
87
|
|
|
$phone->setPeople($people); |
|
88
|
|
|
$this->manager->persist($phone); |
|
89
|
|
|
$this->manager->flush(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $phone; |
|
93
|
|
|
} |
|
94
|
|
|
public function addDocument(People $people, string $document_number, ?string $document_type = null): Document |
|
95
|
|
|
{ |
|
96
|
|
|
$document = $this->getDocument($document_number, $document_type); |
|
97
|
|
|
if ($document) { |
|
98
|
|
|
if ($document->getPeople()->getId() != $people->getId()) |
|
99
|
|
|
throw new Exception("Document is in use by people " . $people->getId(), 1); |
|
100
|
|
|
} else { |
|
101
|
|
|
$document = new Document(); |
|
102
|
|
|
$document->setDocument($document_number); |
|
|
|
|
|
|
103
|
|
|
$document->setDocumentType($this->discoveryDocumentType($document_type)); |
|
|
|
|
|
|
104
|
|
|
$document->setPeople($people); |
|
105
|
|
|
$this->manager->persist($document); |
|
106
|
|
|
$this->manager->flush(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $document; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function addEmail(People $people, string $email_str): Email |
|
113
|
|
|
{ |
|
114
|
|
|
$email = $this->getEmail($email_str); |
|
115
|
|
|
if ($email && $email->getPeople()) { |
|
116
|
|
|
if ($email->getPeople()->getId() != $people->getId()) |
|
117
|
|
|
throw new Exception("Email is in use by people " . $people->getId(), 1); |
|
118
|
|
|
} else { |
|
119
|
|
|
$email = new Email(); |
|
120
|
|
|
$email->setEmail($email_str); |
|
121
|
|
|
$email->setPeople($people); |
|
122
|
|
|
$this->manager->persist($email); |
|
123
|
|
|
$this->manager->flush(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $email; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function getEmail(string $email): ?Email |
|
130
|
|
|
{ |
|
131
|
|
|
return $this->manager->getRepository(Email::class)->findOneBy(['email' => $email]); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function getPhone(string $ddd, string $phone): ?Phone |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->manager->getRepository(Phone::class)->findOneBy(['ddd' => $ddd, 'ddd' => $phone]); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
public function discoveryDocumentType(string $document_type): DocumentType |
|
141
|
|
|
{ |
|
142
|
|
|
$documentType = $this->manager->getRepository(DocumentType::class)->findOneBy(['documentType' => $document_type]); |
|
143
|
|
|
|
|
144
|
|
|
if (!$documentType) { |
|
145
|
|
|
$documentType = new DocumentType(); |
|
146
|
|
|
$documentType->setDocumentType($document_type); |
|
147
|
|
|
$this->manager->persist($documentType); |
|
148
|
|
|
$this->manager->flush(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return $documentType; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function getDocument(string $document_number, ?string $document_type = null): ?Document |
|
155
|
|
|
{ |
|
156
|
|
|
if (!$document_type) |
|
157
|
|
|
$document_type = $this->getDocumentTypeByDocumentLen($document_number); |
|
158
|
|
|
return $this->manager->getRepository(Document::class)->findOneBy([ |
|
159
|
|
|
'document' => $document_number, |
|
160
|
|
|
'documentType' => |
|
161
|
|
|
$this->discoveryDocumentType($document_type) |
|
162
|
|
|
]); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
public function getPeopleTypeByDocumentLen(?string $document_number = null) |
|
166
|
|
|
{ |
|
167
|
|
|
return strlen($document_number) > 11 ? 'J' : 'F'; |
|
|
|
|
|
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function getDocumentTypeByDocumentLen(?string $document_number = null) |
|
171
|
|
|
{ |
|
172
|
|
|
return strlen($document_number) > 11 ? 'CNPJ' : 'CPF'; |
|
|
|
|
|
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
public function postPersist(People $people) |
|
176
|
|
|
{ |
|
177
|
|
|
$request = $this->requestStack->getCurrentRequest(); |
|
178
|
|
|
$payload = json_decode($request->getContent()); |
|
179
|
|
|
if (isset($payload->link_type)) { |
|
180
|
|
|
$company = $this->manager->getRepository(People::class)->find(preg_replace('/\D/', '', $payload->company)); |
|
181
|
|
|
if ($company) |
|
182
|
|
|
$this->addLink($company, $people, $payload->link_type); |
|
183
|
|
|
else { |
|
184
|
|
|
$link = $this->manager->getRepository(People::class)->find(preg_replace('/\D/', '', $payload->link)); |
|
185
|
|
|
if ($payload->link_type == 'employee' && $link) { |
|
186
|
|
|
$this->addLink($people, $link, $payload->link_type); |
|
187
|
|
|
if ($payload->people_document) |
|
188
|
|
|
$this->addDocument($people, $payload->people_document); |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
public function addLink(People $company, People $people, $link_type) |
|
195
|
|
|
{ |
|
196
|
|
|
|
|
197
|
|
|
$peopleLink = $this->manager->getRepository(PeopleLink::class)->findOneBy([ |
|
198
|
|
|
'company' => $company, |
|
199
|
|
|
'people' => $people, |
|
200
|
|
|
'link_type' => $link_type |
|
201
|
|
|
]); |
|
202
|
|
|
|
|
203
|
|
|
if (!$peopleLink) |
|
204
|
|
|
$peopleLink = new PeopleLink(); |
|
205
|
|
|
|
|
206
|
|
|
$peopleLink->setCompany($company); |
|
207
|
|
|
$peopleLink->setPeople($people); |
|
208
|
|
|
$peopleLink->setLinkType($link_type); |
|
209
|
|
|
|
|
210
|
|
|
$this->manager->persist($peopleLink); |
|
211
|
|
|
$this->manager->flush(); |
|
212
|
|
|
return $peopleLink; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
public function securityFilter(QueryBuilder $queryBuilder, $resourceClass = null, $applyTo = null, $rootAlias = null): void |
|
216
|
|
|
{ |
|
217
|
|
|
$this->checkLink($queryBuilder, $resourceClass, $applyTo, $rootAlias); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
public function checkLink(QueryBuilder $queryBuilder, $resourceClass = null, $applyTo = null, $rootAlias = null): void |
|
|
|
|
|
|
221
|
|
|
{ |
|
222
|
|
|
|
|
223
|
|
|
$link = $this->request->query->get('link', null); |
|
224
|
|
|
$company = $this->request->query->get('company', null); |
|
225
|
|
|
$link_type = $this->request->query->get('link_type', null); |
|
226
|
|
|
|
|
227
|
|
|
if ($link_type) { |
|
228
|
|
|
$queryBuilder->join(sprintf('%s.' . ($link ? 'company' : 'link'), $rootAlias), 'PeopleLink'); |
|
229
|
|
|
$queryBuilder->andWhere('PeopleLink.link_type IN(:link_type)'); |
|
230
|
|
|
$queryBuilder->setParameter('link_type', $link_type); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
if ($company || $link) { |
|
234
|
|
|
$queryBuilder->andWhere('PeopleLink.' . ($link ? 'people' : 'company') . ' IN(:people)'); |
|
235
|
|
|
$queryBuilder->setParameter('people', preg_replace("/[^0-9]/", "", ($link ?: $company))); |
|
236
|
|
|
} |
|
237
|
|
|
} |
|
238
|
|
|
public function checkCompany($type, QueryBuilder $queryBuilder, $resourceClass = null, $applyTo = null, $rootAlias = null): void |
|
|
|
|
|
|
239
|
|
|
{ |
|
240
|
|
|
$companies = $this->getMyCompanies(); |
|
241
|
|
|
$queryBuilder->andWhere(sprintf('%s.' . $type . ' IN(:companies)', $rootAlias, $rootAlias)); |
|
242
|
|
|
$queryBuilder->setParameter('companies', $companies); |
|
243
|
|
|
|
|
244
|
|
|
if ($payer = $this->request->query->get('company', null)) { |
|
245
|
|
|
$queryBuilder->andWhere(sprintf('%s.' . $type . ' IN(:people)', $rootAlias)); |
|
246
|
|
|
$queryBuilder->setParameter('people', preg_replace("/[^0-9]/", "", $payer)); |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
public function getMyCompanies(): array |
|
251
|
|
|
{ |
|
252
|
|
|
/** |
|
253
|
|
|
* @var \ControleOnline\Entity\User $currentUser |
|
254
|
|
|
*/ |
|
255
|
|
|
$token = $this->security->getToken(); |
|
256
|
|
|
if (!$token) return []; |
|
257
|
|
|
$currentUser = $token->getUser(); |
|
258
|
|
|
$companies = []; |
|
259
|
|
|
if (!$currentUser) return []; |
|
260
|
|
|
|
|
261
|
|
|
if (!$currentUser->getPeople()->getLink()->isEmpty()) { |
|
262
|
|
|
foreach ($currentUser->getPeople()->getLink() as $company) { |
|
263
|
|
|
$companies[] = $company->getCompany(); |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
return $companies; |
|
267
|
|
|
} |
|
268
|
|
|
} |
|
269
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths