1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ControleOnline\Service; |
4
|
|
|
|
5
|
|
|
use ControleOnline\Entity\People; |
|
|
|
|
6
|
|
|
use ControleOnline\Entity\Contract; |
7
|
|
|
use App\Library\Provider\Signature\Document; |
|
|
|
|
8
|
|
|
use App\Library\Provider\Signature\Signer; |
|
|
|
|
9
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
|
|
|
10
|
|
|
use App\Library\Provider\Signature\SignatureFactory; |
|
|
|
|
11
|
|
|
use ControleOnline\Entity\Config; |
|
|
|
|
12
|
|
|
use ControleOnline\Entity\Status; |
|
|
|
|
13
|
|
|
use ControleOnline\Service\ContractService; |
14
|
|
|
use ControleOnline\Service\PeopleRoleService; |
|
|
|
|
15
|
|
|
use Exception; |
16
|
|
|
|
17
|
|
|
class SignatureService |
18
|
|
|
{ |
19
|
|
|
protected $request; |
20
|
|
|
protected $signatureProvider; |
21
|
|
|
public function __construct( |
22
|
|
|
private EntityManagerInterface $manager, |
23
|
|
|
private PeopleRoleService $peopleRoleService, |
24
|
|
|
private ContractService $contractService, |
25
|
|
|
private StatusService $statusService |
|
|
|
|
26
|
|
|
) {} |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
public function sign(Contract $data) |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
$this->signatureProvider = $this->getFactory($data); |
33
|
|
|
|
34
|
|
|
if ($this->signatureProvider !== null) { |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
$document = ($this->signatureProvider->createDocument()) |
38
|
|
|
->setFileName( |
39
|
|
|
sprintf('Contrato-%s', $this->getContractContractorSignerName($data)) |
40
|
|
|
) |
41
|
|
|
->setContent( |
42
|
|
|
$this->contractService->getContractPDFContent($data) |
43
|
|
|
) |
44
|
|
|
->setDeadlineAt( |
45
|
|
|
(new \DateTime('now')) |
46
|
|
|
->add(new \DateInterval('P7D')) |
47
|
|
|
->format('c') |
48
|
|
|
); |
49
|
|
|
/* |
50
|
|
|
$this->addDocumentSignersFromContract($document, $data); |
51
|
|
|
$this->signatureProvider->saveDocument($document); |
52
|
|
|
*/ |
53
|
|
|
|
54
|
|
|
$data->setStatus( |
55
|
|
|
$this->statusService->discoveryStatus( |
56
|
|
|
'open', |
57
|
|
|
'Waiting Signature', |
58
|
|
|
'contract' |
59
|
|
|
) |
60
|
|
|
); |
61
|
|
|
$data->setDocKey($document->getKey()); |
62
|
|
|
|
63
|
|
|
$this->manager->persist($data); |
64
|
|
|
$this->manager->flush(); |
65
|
|
|
} |
66
|
|
|
return $data; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
private function getProviderConfig(string $providerName): ?array |
71
|
|
|
{ |
72
|
|
|
|
73
|
|
|
$myCompany = $this->peopleRoleService->getMainCompany(); |
74
|
|
|
if ($myCompany instanceof People) { |
75
|
|
|
|
76
|
|
|
return $this->manager->getRepository(Config::class) |
77
|
|
|
->getKeyValuesByPeople( |
78
|
|
|
$myCompany, |
79
|
|
|
strtolower($providerName) |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
throw new \Exception('Company not found'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function getDefaultProviderFromConfig(Contract $data): ?string |
87
|
|
|
{ |
88
|
|
|
$myCompany = $data->getBeneficiary(); |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
if ($myCompany instanceof People) { |
92
|
|
|
$configs = $this->manager->getRepository(Config::class) |
93
|
|
|
->getKeyValuesByPeople( |
94
|
|
|
$myCompany, |
95
|
|
|
'provider' |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
if ($configs === null) { |
99
|
|
|
return null; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return isset($configs['provider-signature']) ? |
103
|
|
|
$configs['provider-signature'] : null; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
throw new \Exception('Company not found'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private function getFactory(Contract $data): ?SignatureFactory |
110
|
|
|
{ |
111
|
|
|
$providerName = |
112
|
|
|
$this->getDefaultProviderFromConfig($data); |
113
|
|
|
|
114
|
|
|
if ($providerName === null) { |
115
|
|
|
return null; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$provider = sprintf( |
119
|
|
|
'\\App\\Library\\Provider\\Signature\\%s\\Factory', |
120
|
|
|
ucfirst(strtolower($providerName)) |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
if (!class_exists($provider)) { |
124
|
|
|
throw new \Exception('Signature provider factory not found'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return new $provider( |
128
|
|
|
$this->getProviderConfig($providerName) |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
protected function addDocumentSignersFromContract(Document $document, Contract $contract) |
133
|
|
|
{ |
134
|
|
|
if ($contract->getPeoples()->isEmpty()) { |
135
|
|
|
throw new Exception('Este contrato não tem assinantes'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$contractProviders = $contract->getPeoples() |
139
|
|
|
->filter(function ($contractPeople) { |
140
|
|
|
return $contractPeople->getPeopleType() == 'Beneficiary'; |
141
|
|
|
}); |
142
|
|
|
if ($contractProviders->isEmpty()) { |
143
|
|
|
throw new Exception('O prestador de serviços não foi definido'); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
foreach ($contractProviders as $provider) { |
147
|
|
|
$document->addSigner( |
148
|
|
|
$this->getSignerFromPeople($provider->getPeople(), 'prestador de serviços') |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$contractParticipants = $contract->getPeoples() |
153
|
|
|
->filter(function ($contractPeople) { |
154
|
|
|
return $contractPeople->getPeopleType() != 'Beneficiary'; |
155
|
|
|
}); |
156
|
|
|
if ($contractParticipants->isEmpty()) { |
157
|
|
|
throw new Exception( |
158
|
|
|
'Devem existir pelo menos 1 assinante no contrato' |
159
|
|
|
); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
foreach ($contractParticipants as $participant) { |
163
|
|
|
$document->addSigner( |
164
|
|
|
$this->getSignerFromPeople($participant->getPeople(), 'assinante') |
165
|
|
|
); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
protected function getContractContractorSignerName(Contract $contract): string |
170
|
|
|
{ |
171
|
|
|
$contractPayers = $contract->getPeoples() |
172
|
|
|
->filter(function ($contractPeople) { |
173
|
|
|
return $contractPeople->getPeopleType() == 'Contractor'; |
174
|
|
|
}); |
175
|
|
|
if ($contractPayers->isEmpty()) { |
176
|
|
|
throw new Exception( |
177
|
|
|
'Devem existir pelo menos 1 assinante como contratante' |
178
|
|
|
); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $contractPayers->first()->getPeople()->getFullName(); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
|
185
|
|
|
protected function getSignerFromPeople(People $people, string $role): Signer |
186
|
|
|
{ |
187
|
|
|
$signer = $this->signatureProvider->createSigner(); |
188
|
|
|
|
189
|
|
|
$signer->setKey($people->getId()); |
190
|
|
|
$signer->setName($people->getFullName()); |
191
|
|
|
|
192
|
|
|
if (($email = $people->getOneEmail()) === null) { |
193
|
|
|
throw new Exception( |
194
|
|
|
sprintf('O %s "%s" não possui um email', $role, $people->getFullName()) |
195
|
|
|
); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
$signer->setEmail($email->getEmail()); |
199
|
|
|
|
200
|
|
|
if ($people->getPeopleType() == 'F') { |
201
|
|
|
$signer->setHasCPF(true); |
202
|
|
|
|
203
|
|
|
if (($document = $people->getOneDocument()) === null) { |
204
|
|
|
throw new Exception( |
205
|
|
|
sprintf('O %s "%s" não possui um CPF/CNPJ', $role, $people->getFullName()) |
206
|
|
|
); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
$signer->setCPF($document->getDocument()); |
210
|
|
|
if (($birthday = $people->getBirthdayAsString()) === null) { |
211
|
|
|
throw new Exception( |
212
|
|
|
sprintf( |
213
|
|
|
'O %s "%s" não tem data de nascimento definida', |
214
|
|
|
$role, |
215
|
|
|
$people->getFullName() |
216
|
|
|
) |
217
|
|
|
); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
$signer->setBirthday($birthday); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return $signer; |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
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