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_O = 'O'; |
13
|
|
|
const TK_I = 'I'; |
14
|
|
|
const TK_F = 'F'; |
15
|
|
|
const TK_C = 'C'; |
16
|
|
|
const TK_S = '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
|
|
|
private $competencia; |
33
|
|
|
/** |
34
|
|
|
* Token de segurança e queue |
35
|
|
|
* hash com 36 caracteres aleatórios |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $tokenid; |
39
|
|
|
/** |
40
|
|
|
* Flag iniciar tranferencia |
41
|
|
|
* @var bool |
42
|
|
|
*/ |
43
|
|
|
private $flagIniciar = false; |
44
|
|
|
/** |
45
|
|
|
* Datahora da ultima solicitação da situação do token |
46
|
|
|
* @var timestramp |
47
|
|
|
*/ |
48
|
|
|
private $tsLastSitToken; |
49
|
|
|
|
50
|
|
|
|
51
|
6 |
|
public function __construct($configJson = '') |
52
|
|
|
{ |
53
|
6 |
|
parent::__construct($configJson); |
54
|
3 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Define o período de competência das informações |
58
|
|
|
* formado AAAABB sendo BB o bimestre de 01 até 06 |
59
|
|
|
* @param string $valor |
|
|
|
|
60
|
|
|
*/ |
61
|
9 |
|
public function setCompetencia($aaaabb) |
62
|
|
|
{ |
63
|
9 |
|
if (!is_numeric($aaaabb)) { |
64
|
3 |
|
throw new InvalidArgumentException('O periodo de competência é uma informação APENAS numérica.'); |
65
|
|
|
} |
66
|
6 |
|
$bm = intval(substr($aaaabb, -2)); |
67
|
6 |
|
if ($bm > 6 || $bm <= 0) { |
68
|
3 |
|
throw new InvalidArgumentException('O bimestre pode ser de 01 até 06 APENAS.'); |
69
|
|
|
} |
70
|
3 |
|
$this->competencia = $aaaabb; |
|
|
|
|
71
|
3 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Retorna o período de competência informado |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
3 |
|
public function getCompetencia() |
78
|
|
|
{ |
79
|
3 |
|
return $this->competencia; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Operações com token |
84
|
|
|
* O método pode ser: |
85
|
|
|
* C - cancela a transferencia |
86
|
|
|
* F - finaliza a transferencia |
87
|
|
|
* I - inica a transferencia |
88
|
|
|
* O - Obtem o token para realizar as operações |
89
|
|
|
* S - Verifica a situação do token |
90
|
|
|
* @param string $method |
91
|
|
|
*/ |
92
|
|
|
public function token($method = self::TK_O) |
93
|
|
|
{ |
94
|
|
|
$uri = $this->url[$this->tpAmb].'/esfinge/services/tokenWS'; |
95
|
|
|
$namespace = 'http://token.ws.tce.sc.gov.br/'; |
96
|
|
|
|
97
|
|
|
switch ($method) { |
98
|
|
|
case 'C': |
99
|
|
|
if ($this->flagIniciar === false) { |
100
|
|
|
//não está iniciada a tranferencia então não dá para cancelar |
101
|
|
|
throw new RuntimeException('A tranferencia não foi iniciada, então não pode ser cancelada'); |
102
|
|
|
} |
103
|
|
|
//cancela as operações realizadas com um determinado token |
104
|
|
|
//se OK o token é removido e todas as operações com ele |
105
|
|
|
//realizadas são descartadas |
106
|
|
|
$x = 'http://token.ws.tce.sc.gov.br/FilaAcesso/cancelarTransferencia'; |
|
|
|
|
107
|
|
|
$met = 'cancelarTransferencia'; |
108
|
|
|
$body = "<cancelarTransferencia xmlns=\"$namespace\">" |
109
|
|
|
. "<chaveToken>$this->tokenid</chaveToken>" |
110
|
|
|
. "</cancelarTransferencia>"; |
111
|
|
|
$retorno = $this->oSoap->send($uri, $namespace, $this->header, $body, $met); |
112
|
|
|
$resp = Response::readReturn($met, $retorno); |
113
|
|
|
if ($resp['bStat']) { |
114
|
|
|
$this->tokenid = ''; |
115
|
|
|
$this->flagIniciar = false; |
116
|
|
|
} |
117
|
|
|
break; |
118
|
|
|
case 'F': |
119
|
|
|
if ($this->flagIniciar === false) { |
120
|
|
|
//não está iniciada a tranferencia então não dá para finalizar |
121
|
|
|
throw new RuntimeException('A tranferencia não foi iniciada, então não pode ser finalizada'); |
122
|
|
|
} |
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
|
|
|
$met = 'finalizarTransferencia'; |
128
|
|
|
$body = "<finalizarTransferencia xmlns=\"$namespace\">" |
129
|
|
|
. "<chaveToken>$this->tokenid</chaveToken>" |
130
|
|
|
. "</finalizarTransferencia>"; |
131
|
|
|
$retorno = $this->oSoap->send($uri, $namespace, $this->header, $body, $met); |
132
|
|
|
$resp = Response::readReturn($met, $retorno); |
133
|
|
|
if ($resp['bStat']) { |
134
|
|
|
$this->tokenid = ''; |
135
|
|
|
$this->flagIniciar = false; |
136
|
|
|
} |
137
|
|
|
break; |
138
|
|
|
case 'I': |
139
|
|
|
if ($this->tokenid == '') { |
140
|
|
|
//não é possivel iniciar sem um token valido |
141
|
|
|
throw new RuntimeException('Não é possivel iniciar a tranferência sem um token valido'); |
142
|
|
|
//$this->token(self::TK_O); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
if ($this->flagIniciar === true) { |
145
|
|
|
$resp = [ |
146
|
|
|
'bStat' => true, |
147
|
|
|
'message' => 'Início de transferência liberado', |
148
|
|
|
'status' => 'OK' |
149
|
|
|
]; |
150
|
|
|
break; |
151
|
|
|
} |
152
|
|
|
//Antes de iniciar a transferência dos dados propriamente dita será necessário executar |
153
|
|
|
//o serviço iniciarTransferencia |
154
|
|
|
$met = 'iniciarTransferencia'; |
155
|
|
|
$body = "<iniciarTransferencia xmlns=\"$namespace\">" |
156
|
|
|
. "<chaveToken>$this->tokenid</chaveToken>" |
157
|
|
|
. "</iniciarTransferencia>"; |
158
|
|
|
$retorno = $this->oSoap->send($uri, $namespace, $this->header, $body, $met); |
159
|
|
|
$resp = Response::readReturn($met, $retorno); |
160
|
|
|
if ($resp['bStat']) { |
161
|
|
|
$this->flagIniciar = true; |
162
|
|
|
} |
163
|
|
|
break; |
164
|
|
|
case 'O': |
165
|
|
View Code Duplication |
if ($this->tokenid != '') { |
|
|
|
|
166
|
|
|
$resp = [ |
167
|
|
|
'bStat' => true, |
168
|
|
|
'message' => 'Token criado com sucesso', |
169
|
|
|
'status' => 'OK', |
170
|
|
|
'chaveToken' => $this->tokenid, |
171
|
|
|
'posicao' => 2, |
172
|
|
|
'situacao' => 'Pronto para envio ou consulta' |
173
|
|
|
]; |
174
|
|
|
break; |
175
|
|
|
} |
176
|
|
|
$met = 'obterToken'; |
177
|
|
|
$body = "<obterToken xmlns=\"$namespace\">" |
178
|
|
|
. "<codigoUg>$this->codigoUnidadeGestora</codigoUg>" |
179
|
|
|
. "</obterToken>"; |
180
|
|
|
$retorno = $this->oSoap->send($uri, $namespace, $this->header, $body, $met); |
181
|
|
|
$resp = Response::readReturn($met, $retorno); |
182
|
|
|
if ($resp['bStat'] && $resp['chaveToken'] != '') { |
183
|
|
|
$this->tokenid = $resp['chaveToken']; |
184
|
|
|
} |
185
|
|
|
break; |
186
|
|
|
case 'S': |
187
|
|
|
//Retorna a situação do token passado como parâmetro. Para evitar solicitações |
188
|
|
|
//indefinidas a este serviço o sistema punirá com a remoção do token da fila |
189
|
|
|
//sempre que for feita duas chamadas seguidas do serviço obterSituacaoToken |
190
|
|
|
//em menos de cinco segundos. |
191
|
|
|
if ($this->tokenid == '') { |
192
|
|
|
//não é possivel verificar o token |
193
|
|
|
throw new RuntimeException('Não existe um token aberto.'); |
194
|
|
|
} |
195
|
|
|
//se tentativa de verificação ocorrer em menos de 2 seg |
196
|
|
|
//retorna como OK |
197
|
|
View Code Duplication |
if ((time()-$this->tsLastSitToken) <= 2) { |
|
|
|
|
198
|
|
|
$resp = [ |
199
|
|
|
'bStat' => true, |
200
|
|
|
'message' => 'Situação token obtida com sucesso', |
201
|
|
|
'status' => 'OK', |
202
|
|
|
'posicao' => 1, |
203
|
|
|
'situacao' => 'Pronto para envio ou consulta' |
204
|
|
|
]; |
205
|
|
|
break; |
206
|
|
|
} |
207
|
|
|
$met = 'obterSituacaoToken'; |
208
|
|
|
$body = "<obterSituacaoToken xmlns=\"$namespace\">" |
209
|
|
|
. "<chaveToken>$this->tokenid</chaveToken>" |
210
|
|
|
. "</obterSituacaoToken>"; |
211
|
|
|
$retorno = $this->oSoap->send($uri, $namespace, $this->header, $body, $met); |
212
|
|
|
$resp = Response::readReturn($met, $retorno); |
213
|
|
|
$this->tsLastSitToken = time(); |
|
|
|
|
214
|
|
|
break; |
215
|
|
|
} |
216
|
|
|
return $resp; |
|
|
|
|
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
protected function obterTokenIniciarTransferencia($data = array()) |
220
|
|
|
{ |
221
|
|
|
if (empty($data)) { |
222
|
|
|
throw new InvalidArgumentException('Não foram passados dados para o método'); |
223
|
|
|
} |
224
|
|
|
$this->token(self::TK_O); |
225
|
|
|
$this->token(self::TK_I); |
226
|
|
|
if ($this->tokenid == '' || $this->flagIniciar === false) { |
227
|
|
|
throw new RuntimeException("Falha token:$this->tokenid , Iniciar: $this->flagIniciar"); |
228
|
|
|
} |
229
|
|
|
$this->buildSoapHeader(); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Servidor |
234
|
|
|
* @param array $data |
235
|
|
|
* @param string $method |
236
|
|
|
* @return array |
237
|
|
|
*/ |
238
|
|
View Code Duplication |
public function servidor($data = array(), $method = 'L') |
|
|
|
|
239
|
|
|
{ |
240
|
|
|
$this->obterTokenIniciarTransferencia($data); |
241
|
|
|
$uri = $this->url[$this->tpAmb].'/esfinge/services/servidorWS'; |
242
|
|
|
$namespace = 'http://servidor.ws.tce.sc.gov.br/'; |
243
|
|
|
$met = 'servidor'.$method; |
244
|
|
|
//envia a mensagem via cURL |
245
|
|
|
$resp = $this->envia($uri, $namespace, $data, $method, $met); |
246
|
|
|
return $resp; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Situação Servidor Folha Pagamento |
251
|
|
|
* @param array $data |
252
|
|
|
* @param string $method |
253
|
|
|
* @return array |
254
|
|
|
*/ |
255
|
|
View Code Duplication |
public function situacaoServidorFolhaPagamento($data = array(), $method = 'L') |
|
|
|
|
256
|
|
|
{ |
257
|
|
|
$this->obterTokenIniciarTransferencia($data); |
258
|
|
|
$uri = $this->url[$this->tpAmb].'/esfinge/services/situacaoServidorFolhaPagamentoWS'; |
259
|
|
|
$namespace = 'http://situacaoservidorfolhapagamento.ws.tce.sc.gov.br/'; |
260
|
|
|
$met = 'situacaoServidorFolhaPagamento'.$method; |
261
|
|
|
$resp = $this->envia($uri, $namespace, $data, $method, $met); |
262
|
|
|
return $resp; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Componentes Folha Pagamento |
267
|
|
|
* @param array $data |
268
|
|
|
* @param string $method |
269
|
|
|
* @return array |
270
|
|
|
*/ |
271
|
|
View Code Duplication |
public function componentesFolhaPagamento($data = array(), $method = 'L') |
|
|
|
|
272
|
|
|
{ |
273
|
|
|
$this->obterTokenIniciarTransferencia($data); |
274
|
|
|
$uri = $this->url[$this->tpAmb].'/esfinge/services/componentesFolhaPagamentoWS'; |
275
|
|
|
$namespace = 'http://componentesfolhapagamento.ws.tce.sc.gov.br/'; |
276
|
|
|
$met = 'componentesFolhaPagamento'.$method; |
277
|
|
|
$resp = $this->envia($uri, $namespace, $data, $method, $met); |
278
|
|
|
return $resp; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Folha Pagamento |
283
|
|
|
* @param array $data |
284
|
|
|
* @param string $method |
285
|
|
|
* @return array |
286
|
|
|
*/ |
287
|
|
View Code Duplication |
public function folhaPagamento($data = array(), $method = 'L') |
|
|
|
|
288
|
|
|
{ |
289
|
|
|
$this->obterTokenIniciarTransferencia($data); |
290
|
|
|
$uri = $this->url[$this->tpAmb].'/esfinge/services/folhaPagamentoWS'; |
291
|
|
|
$namespace = 'http://folhapagamento.ws.tce.sc.gov.br/'; |
292
|
|
|
$met = 'folhaPagamento'.$method; |
293
|
|
|
$resp = $this->envia($uri, $namespace, $data, $method, $met); |
294
|
|
|
return $resp; |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|
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.