1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NFePHP\Esfinge; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use RuntimeException; |
7
|
|
|
use NFePHP\Esfinge\Response; |
8
|
|
|
use NFePHP\Esfinge\Base; |
9
|
|
|
|
10
|
|
|
class Tools extends Base |
11
|
|
|
{ |
12
|
|
|
const TK_OBTEM = 'O'; |
13
|
|
|
const TK_INICIA = 'I'; |
14
|
|
|
const TK_FINALIZA = 'F'; |
15
|
|
|
const TK_CANCELA = 'C'; |
16
|
|
|
const TK_STATUS = 'S'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Endereços principais dos webservices |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $url = [ |
23
|
|
|
'1' => 'https://esfingews.tce.sc.gov.br', |
24
|
|
|
'2' => 'https://desenv2.tce.sc.gov.br:7443', |
25
|
|
|
]; |
26
|
|
|
/** |
27
|
|
|
* Competência bimestral no formato: AAAABB, onde: |
28
|
|
|
* AAAA = ano a ser enviado os dados |
29
|
|
|
* BB = bimestre de 01 até 06 |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $competencia; |
33
|
|
|
/** |
34
|
|
|
* Token de segurança e queue |
35
|
|
|
* hash com 36 caracteres aleatórios |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $tokenid; |
39
|
|
|
/** |
40
|
|
|
* Flag iniciar tranferencia |
41
|
|
|
* @var bool |
42
|
|
|
*/ |
43
|
|
|
protected $flagIniciar = false; |
44
|
|
|
/** |
45
|
|
|
* Datahora da ultima solicitação da situação do token |
46
|
|
|
* @var timestramp |
47
|
|
|
*/ |
48
|
|
|
protected $tsLastSitToken; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Construtor |
52
|
|
|
* @param string $configJson |
53
|
|
|
*/ |
54
|
6 |
|
public function __construct($configJson = '') |
55
|
|
|
{ |
56
|
6 |
|
parent::__construct($configJson); |
57
|
3 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Define o período de competência das informações |
61
|
|
|
* formado AAAABB sendo BB o bimestre de 01 até 06 |
62
|
|
|
* @param string $valor |
|
|
|
|
63
|
|
|
*/ |
64
|
9 |
|
public function setCompetencia($aaaabb) |
65
|
|
|
{ |
66
|
9 |
|
if (!is_numeric($aaaabb)) { |
67
|
3 |
|
throw new InvalidArgumentException('O periodo de competência é uma informação APENAS numérica.'); |
68
|
|
|
} |
69
|
6 |
|
$bm = intval(substr($aaaabb, -2)); |
70
|
6 |
|
if ($bm > 6 || $bm <= 0) { |
71
|
3 |
|
throw new InvalidArgumentException('O bimestre pode ser de 01 até 06 APENAS.'); |
72
|
|
|
} |
73
|
3 |
|
$this->competencia = $aaaabb; |
|
|
|
|
74
|
3 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Retorna o período de competência informado |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
3 |
|
public function getCompetencia() |
81
|
|
|
{ |
82
|
3 |
|
return $this->competencia; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Operações com token |
87
|
|
|
* O método pode ser: |
88
|
|
|
* C - cancela a transferencia |
89
|
|
|
* F - finaliza a transferencia |
90
|
|
|
* I - inica a transferencia |
91
|
|
|
* O - Obtem o token para realizar as operações |
92
|
|
|
* S - Verifica a situação do token |
93
|
|
|
* @param string $method |
94
|
|
|
*/ |
95
|
|
|
public function token($method = self::TK_OBTEM) |
96
|
|
|
{ |
97
|
|
|
$uri = $this->url[$this->tpAmb].'/esfinge/services/tokenWS'; |
98
|
|
|
$namespace = 'http://token.ws.tce.sc.gov.br/'; |
99
|
|
|
|
100
|
|
|
switch ($method) { |
101
|
|
View Code Duplication |
case self::TK_CANCELA: |
|
|
|
|
102
|
|
|
//cancela as operações realizadas com um determinado token |
103
|
|
|
//se OK o token é removido e todas as operações com ele |
104
|
|
|
//realizadas são descartadas |
105
|
|
|
if ($this->flagIniciar === false) { |
106
|
|
|
//não está iniciada a tranferencia então não dá para cancelar |
107
|
|
|
throw new RuntimeException('A tranferencia não foi iniciada, então não pode ser cancelada'); |
108
|
|
|
} |
109
|
|
|
$met = 'cancelarTransferencia'; |
110
|
|
|
$body = "<svc:cancelarTransferencia>" |
111
|
|
|
. "<chaveToken>$this->tokenid</chaveToken>" |
112
|
|
|
. "</svc:cancelarTransferencia>"; |
113
|
|
|
$resp = $this->envia($uri, $namespace, $body, '', $met); |
114
|
|
|
//$retorno = $this->oSoap->send($uri, $namespace, $this->header, $body, $met); |
|
|
|
|
115
|
|
|
//$resp = Response::readReturn($met, $retorno); |
|
|
|
|
116
|
|
|
if ($resp['bStat']) { |
117
|
|
|
//cancelamento aceito |
118
|
|
|
$this->tokenid = ''; |
119
|
|
|
$this->flagIniciar = false; |
120
|
|
|
} |
121
|
|
|
break; |
122
|
|
View Code Duplication |
case self::TK_FINALIZA: |
|
|
|
|
123
|
|
|
//Ao final da transferência caso queria confirmar todos os elementos inseridos |
124
|
|
|
//(que não retornaram erro) nesta sessão, ou seja todos os elementos ligados a |
125
|
|
|
//determinado token passado para o serviço. Uma vez executado este serviço |
126
|
|
|
//o token atual será descartado. |
127
|
|
|
if ($this->flagIniciar === false) { |
128
|
|
|
//não está iniciada a tranferencia então não dá para finalizar |
129
|
|
|
throw new RuntimeException('A tranferencia não foi iniciada, então não pode ser finalizada'); |
130
|
|
|
} |
131
|
|
|
$met = 'finalizarTransferencia'; |
132
|
|
|
$body = "<svc:finalizarTransferencia>" |
133
|
|
|
. "<chaveToken>$this->tokenid</chaveToken>" |
134
|
|
|
. "</svc:finalizarTransferencia>"; |
135
|
|
|
$resp = $this->envia($uri, $namespace, $body, '', $met); |
136
|
|
|
//$retorno = $this->oSoap->send($uri, $namespace, $this->header, $body, $met); |
|
|
|
|
137
|
|
|
//$resp = Response::readReturn($met, $retorno); |
|
|
|
|
138
|
|
|
if ($resp['bStat']) { |
139
|
|
|
//finalização aceita |
140
|
|
|
$this->tokenid = ''; |
141
|
|
|
$this->flagIniciar = false; |
142
|
|
|
} |
143
|
|
|
break; |
144
|
|
|
case self::TK_INICIA: |
145
|
|
|
//Antes de iniciar a transferência dos dados propriamente dita, será necessário executar |
146
|
|
|
//o serviço iniciarTransferencia |
147
|
|
|
if ($this->tokenid == '') { |
148
|
|
|
//não é possivel iniciar sem um token valido |
149
|
|
|
throw new RuntimeException('Não é possivel iniciar a tranferência sem um token valido'); |
150
|
|
|
//$this->token(self::TK_O); |
|
|
|
|
151
|
|
|
} |
152
|
|
|
if ($this->flagIniciar === true) { |
153
|
|
|
$resp = [ |
154
|
|
|
'bStat' => true, |
155
|
|
|
'message' => 'Início de transferência liberado', |
156
|
|
|
'status' => 'OK' |
157
|
|
|
]; |
158
|
|
|
break; |
159
|
|
|
} |
160
|
|
|
$met = 'iniciarTransferencia'; |
161
|
|
|
$body = "<svc:iniciarTransferencia>" |
162
|
|
|
. "<chaveToken>$this->tokenid</chaveToken>" |
163
|
|
|
. "</svc:iniciarTransferencia>"; |
164
|
|
|
$resp = $this->envia($uri, $namespace, $body, '', $met); |
165
|
|
|
//$retorno = $this->oSoap->send($uri, $namespace, $this->header, $body, $met); |
|
|
|
|
166
|
|
|
//$resp = Response::readReturn($met, $retorno); |
|
|
|
|
167
|
|
|
if ($resp['bStat']) { |
168
|
|
|
$this->flagIniciar = true; |
169
|
|
|
} |
170
|
|
|
break; |
171
|
|
|
case self::TK_OBTEM: |
172
|
|
|
//Retorna um token para a unidade gestora poder usar o serviço do TCE. |
173
|
|
|
//Permite somente um token por unidade gestora. |
174
|
|
View Code Duplication |
if ($this->tokenid != '') { |
|
|
|
|
175
|
|
|
$resp = [ |
176
|
|
|
'bStat' => true, |
177
|
|
|
'message' => 'Token criado com sucesso', |
178
|
|
|
'status' => 'OK', |
179
|
|
|
'chaveToken' => $this->tokenid, |
180
|
|
|
'posicao' => 2, |
181
|
|
|
'situacao' => 'Pronto para envio ou consulta' |
182
|
|
|
]; |
183
|
|
|
break; |
184
|
|
|
} |
185
|
|
|
$met = 'obterToken'; |
186
|
|
|
$body = "<svc:obterToken>" |
187
|
|
|
. "<codigoUg>$this->codigoUnidadeGestora</codigoUg>" |
188
|
|
|
. "</svc:obterToken>"; |
189
|
|
|
$resp = $this->envia($uri, $namespace, $body, '', $met); |
190
|
|
|
//$retorno = $this->oSoap->send($uri, $namespace, $this->header, $body, $met); |
|
|
|
|
191
|
|
|
//$resp = Response::readReturn($met, $retorno); |
|
|
|
|
192
|
|
|
if ($resp['bStat'] && $resp['chaveToken'] != '') { |
193
|
|
|
$this->tokenid = $resp['chaveToken']; |
194
|
|
|
} |
195
|
|
|
break; |
196
|
|
|
case self::TK_STATUS: |
197
|
|
|
//Retorna a situação do token passado como parâmetro. Para evitar solicitações |
198
|
|
|
//indefinidas a este serviço o sistema punirá com a remoção do token da fila |
199
|
|
|
//sempre que for feita duas chamadas seguidas do serviço obterSituacaoToken |
200
|
|
|
//em menos de cinco segundos. |
201
|
|
|
if ($this->tokenid == '') { |
202
|
|
|
//não é possivel verificar o token |
203
|
|
|
throw new RuntimeException('Não existe um token aberto.'); |
204
|
|
|
} |
205
|
|
|
//se tentativa de verificação ocorrer em menos de 2 seg |
206
|
|
|
//retorna como OK |
207
|
|
View Code Duplication |
if ((time()-$this->tsLastSitToken) <= 2) { |
|
|
|
|
208
|
|
|
$resp = [ |
209
|
|
|
'bStat' => true, |
210
|
|
|
'message' => 'Situação token obtida com sucesso', |
211
|
|
|
'status' => 'OK', |
212
|
|
|
'posicao' => 1, |
213
|
|
|
'situacao' => 'Pronto para envio ou consulta' |
214
|
|
|
]; |
215
|
|
|
break; |
216
|
|
|
} |
217
|
|
|
$met = 'obterSituacaoToken'; |
218
|
|
|
$body = "<svc:obterSituacaoToken>" |
219
|
|
|
. "<chaveToken>$this->tokenid</chaveToken>" |
220
|
|
|
. "</svc:obterSituacaoToken>"; |
221
|
|
|
$resp = $this->envia($uri, $namespace, $body, '', $met); |
222
|
|
|
//$retorno = $this->oSoap->send($uri, $namespace, $this->header, $body, $met); |
|
|
|
|
223
|
|
|
//$resp = Response::readReturn($met, $retorno); |
|
|
|
|
224
|
|
|
$this->tsLastSitToken = time(); |
|
|
|
|
225
|
|
|
break; |
226
|
|
|
} |
227
|
|
|
return $resp; |
|
|
|
|
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Inicia o processo de tranferência de dados |
232
|
|
|
* @param array $data |
233
|
|
|
* @throws InvalidArgumentException |
234
|
|
|
* @throws RuntimeException |
235
|
|
|
*/ |
236
|
|
|
protected function obterTokenIniciarTransferencia($data = array()) |
237
|
|
|
{ |
238
|
|
|
if (empty($data)) { |
239
|
|
|
throw new InvalidArgumentException('Não foram passados dados para o método'); |
240
|
|
|
} |
241
|
|
|
$this->token(self::TK_OBTEM); |
242
|
|
|
$this->token(self::TK_INICIA); |
243
|
|
|
if ($this->tokenid == '' || $this->flagIniciar === false) { |
244
|
|
|
throw new RuntimeException("Falha token:$this->tokenid , Iniciar: $this->flagIniciar"); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Servidor |
250
|
|
|
* se ainda não tiver o TOKEN -> Obtem (automático) |
251
|
|
|
* se ainda não tiver iniciado -> inicia (automático) |
252
|
|
|
* @param array $data |
253
|
|
|
* @param string $method |
254
|
|
|
* @return array |
255
|
|
|
*/ |
256
|
|
View Code Duplication |
public function servidor($data = array(), $method = 'L') |
|
|
|
|
257
|
|
|
{ |
258
|
|
|
$this->obterTokenIniciarTransferencia($data); |
259
|
|
|
$uri = $this->url[$this->tpAmb].'/esfinge/services/servidorWS'; |
260
|
|
|
$namespace = 'http://servidor.ws.tce.sc.gov.br/'; |
261
|
|
|
$met = 'servidor'.$method; |
262
|
|
|
//envia a mensagem via cURL |
263
|
|
|
$resp = $this->envia($uri, $namespace, $data, $method, $met); |
264
|
|
|
return $resp; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Situação Servidor Folha Pagamento |
269
|
|
|
* se ainda não tiver o TOKEN -> Obtem (automático) |
270
|
|
|
* se ainda não tiver iniciado -> inicia (automático) |
271
|
|
|
* @param array $data |
272
|
|
|
* @param string $method |
273
|
|
|
* @return array |
274
|
|
|
*/ |
275
|
|
View Code Duplication |
public function situacaoServidorFolhaPagamento($data = array(), $method = 'L') |
|
|
|
|
276
|
|
|
{ |
277
|
|
|
$this->obterTokenIniciarTransferencia($data); |
278
|
|
|
$uri = $this->url[$this->tpAmb].'/esfinge/services/situacaoServidorFolhaPagamentoWS'; |
279
|
|
|
$namespace = 'http://situacaoservidorfolhapagamento.ws.tce.sc.gov.br/'; |
280
|
|
|
$met = 'situacaoServidorFolhaPagamento'.$method; |
281
|
|
|
$resp = $this->envia($uri, $namespace, $data, $method, $met); |
282
|
|
|
return $resp; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Componentes Folha Pagamento |
287
|
|
|
* se ainda não tiver o TOKEN -> Obtem (automático) |
288
|
|
|
* se ainda não tiver iniciado -> inicia (automático) |
289
|
|
|
* @param array $data |
290
|
|
|
* @param string $method |
291
|
|
|
* @return array |
292
|
|
|
*/ |
293
|
|
View Code Duplication |
public function componentesFolhaPagamento($data = array(), $method = 'L') |
|
|
|
|
294
|
|
|
{ |
295
|
|
|
$this->obterTokenIniciarTransferencia($data); |
296
|
|
|
$uri = $this->url[$this->tpAmb].'/esfinge/services/componentesFolhaPagamentoWS'; |
297
|
|
|
$namespace = 'http://componentesfolhapagamento.ws.tce.sc.gov.br/'; |
298
|
|
|
$met = 'componentesFolhaPagamento'.$method; |
299
|
|
|
$resp = $this->envia($uri, $namespace, $data, $method, $met); |
300
|
|
|
return $resp; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Folha Pagamento |
305
|
|
|
* se ainda não tiver o TOKEN -> Obtem (automático) |
306
|
|
|
* se ainda não tiver iniciado -> inicia (automático) |
307
|
|
|
* @param array $data |
308
|
|
|
* @param string $method |
309
|
|
|
* @return array |
310
|
|
|
*/ |
311
|
|
View Code Duplication |
public function folhaPagamento($data = array(), $method = 'L') |
|
|
|
|
312
|
|
|
{ |
313
|
|
|
$this->obterTokenIniciarTransferencia($data); |
314
|
|
|
$uri = $this->url[$this->tpAmb].'/esfinge/services/folhaPagamentoWS'; |
315
|
|
|
$namespace = 'http://folhapagamento.ws.tce.sc.gov.br/'; |
316
|
|
|
$met = 'folhaPagamento'.$method; |
317
|
|
|
$resp = $this->envia($uri, $namespace, $data, $method, $met); |
318
|
|
|
return $resp; |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.