-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmelhora-sgrh.user.js
348 lines (315 loc) · 13.1 KB
/
melhora-sgrh.user.js
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// ==UserScript==
// @name Melhora Portal do Servidor
// @namespace https://github.com/luizluca/melhora-sgrh
// @version 1.8
// @description Adiciona mais informações ao Portal do Servidor
// @grant none
// @updateURL https://raw.githubusercontent.com/luizluca/melhora-sgrh/master/melhora-sgrh.user.js
// @downloadURL https://raw.githubusercontent.com/luizluca/melhora-sgrh/master/melhora-sgrh.user.js
// @match https://sistemas4.tre-sc.gov.br/sadAdmSRH/frequencianacional/espelhoPontoMensal.do*
// @match https://sistemas5.tre-sc.gov.br/portal-servidor/EspelhoPontoMesAction_recuperar.action
// @match https://sistemas5.tre-sc.gov.br/portal-servidor/_/EspelhoPontoMesAction_recuperar
// @match https://sistemas5.tre-sc.gov.br/portal-servidor/EspelhoPontoMesAction_recuperar
// @match https://sistemas5.tre-sc.gov.br/portal-servidor/EspelhoPontoMesAction_formEspelhoPontoMes_consultar
// @copyright 2016+, Luiz Angelo Daros de Luca <[email protected]>, Luís Flávio Seelig <[email protected]>
// @require https://code.jquery.com/jquery-latest.js
// ==/UserScript==
/*global $:false, jQuery:false */
/*jshint multistr: true */
function pad (str, max) {
return (""+str).length < max ? pad("0" + str, max) : str;
}
function time2str(aTime,hourMinuteSecond=false) {
if (isNaN(aTime)) aTime=0;
var aSig=((aTime<0)?"-":"");
var aHour;
var aMin;
var aTimeStr="";
if (hourMinuteSecond) {
// Second resolution
aTime=Math.floor(aTime/(1000));
var aSec=aTime%60;
aTimeStr=":"+pad(Math.abs(aSec),2);
aTime=(aTime-aSec)/60;
} else {
// Minute resolution
aTime=Math.floor(aTime/(1000*60));
}
aMin=aTime%60;
aTimeStr=":"+pad(Math.abs(aMin),2)+aTimeStr;
aHour=(aTime-aMin)/60;
return aSig+pad(Math.abs(aHour),2)+aTimeStr;
}
function str2time(aTimeStr) {
var horas=parseInt(aTimeStr.substr(0,aTimeStr.indexOf(":")));
var minutos=parseInt(aTimeStr.substr(aTimeStr.indexOf(":")+1));
if (aTimeStr[0]=="-") minutos=-minutos;
return (horas*60+minutos)*60*1000;
}
function str2date(aDate,aLang) {
switch(aLang) {
case "pt_BR":
aDate=aDate.
replace(/Fev/i,"Feb").
replace(/Abr/i,"Apr").
replace(/Mai/i,"May").
replace(/Ago/i,"Aug").
replace(/Set/i,"Sep").
replace(/Out/i,"Oct").
replace(/Dez/i,"Dec").
toString();
}
aDate=aDate.
replace(/Jan/i,"01").
replace(/Fev/i,"02").
replace(/Mar/i,"03").
replace(/Apr/i,"04").
replace(/May/i,"05").
replace(/Jun/i,"06").
replace(/Jul/i,"07").
replace(/Aug/i,"08").
replace(/Sep/i,"09").
replace(/Oct/i,"10").
replace(/Nov/i,"11").
replace(/Dec/i,"12").
toString();
aDate=aDate.split("/").reverse().join("/");
return new Date(aDate);
}
Date.prototype.days_in_month = function() {
return (new Date(this.getFullYear(),this.getMonth()+1,0)).getDate();
};
class EspelhoPonto {
constructor(_tabela) {
if (_tabela === undefined) {
this.tabela=$("#tblEspelhoPontoMesCorrente tr");
this.async=true;
} else {
this.tabela=_tabela;
this.async=false;
}
this.mes=str2date(this.tabela[1].children[0].innerHTML,"pt_BR");
this.num_dias=this.mes.days_in_month()
this.saldo_mes=null;
this.saldo_mes_anterior=null;
this.atualiza_tudo();
}
agora() {
var agora=new Date();
var hoje=new Date(agora.getFullYear(),agora.getMonth(),agora.getDate())
var hoje_str=hoje.toLocaleString().slice(0, 10);
var found=this.tabela.find("td:contains('"+hoje_str+"')")
if (found.length>0) {
var linha=found.parent();
var soma_dia=0
var expediente=0;
if ($(linha).hasClass("fundo2")) {
expediente=0;
} else {
// http://sistemas.tre-sc.gov.br/e-docs/documento/edoc768202/portaria_pres_2015_26.pdf#search=%22%22
// Expediente 7h a partir de Abril de ano eleitoral
if ((hoje.getFullYear()%2==0) && (hoje.getMonth() >= 3)) {
expediente=7*60*60*1000;
} else {
expediente=6*60*60*1000;
}
}
for (var j=0;j<3;j++) {
var entrada=linha.children()[j*2+1].innerHTML;
if (entrada=="") break;
entrada=str2time(entrada);
if (isNaN(entrada)) break;
var saida=linha.children()[j*2+2];
if ($(saida).hasClass("dinamico") || (saida.innerHTML=="")) {
saida=(agora.getTime()-hoje.getTime())
this.atualiza_campo(linha.children()[j*2+2],time2str(saida))
} else {
saida=str2time(saida.innerHTML);
}
soma_dia+=saida-entrada
}
if (j>0) {
this.atualiza_campo(linha.children()[8],time2str(soma_dia));
this.atualiza_campo(linha.children()[9],time2str(soma_dia-expediente));
}
} else {
// Mẽs anterior mas não fechado ainda.
}
this.recarrega();
this.atualiza_resumos();
}
recarrega(){
this.jornada=new Array(this.num_dias);
this.excedentes=new Array(this.num_dias);
this.autorizacoesHE=new Array(this.num_dias);
for (var i=1;i<=this.num_dias;i++) {
var linha=this.tabela[i]
var dia=linha.children[0].innerHTML;
var total=Array.from(linha.children).filter(campo => $(campo).hasClass("h09")).map(campo => campo.innerHTML)[0];
var excedente=Array.from(linha.children).filter(campo => $(campo).hasClass("h10")).map(campo => campo.innerHTML)[0];
var autorizacaoHE=Array.from(linha.children).filter(campo => $(campo).hasClass("h17")).map(campo => campo.children.length>0)[0];
this.jornada[i-1]=total ? str2time(total) : 0;
this.excedentes[i-1]=excedente ? str2time(excedente) : 0;
this.autorizacoesHE[i-1]=autorizacaoHE ? true : false
}
}
jornada_mensal() {
var soma=0;
for (var i=0;i<this.num_dias;i++) {
soma+=this.jornada[i];
}
return soma;
}
atrasos(){
var soma=0;
for (var i=0;i<this.num_dias;i++) {
if (this.excedentes[i]<0) {
soma-=this.excedentes[i];
}
}
return soma;
}
extras(){
var soma=0;
for (var i=0;i<this.num_dias;i++) {
if (this.excedentes[i]>0) {
soma+=this.excedentes[i];
}
}
return soma;
}
extras_autorizacao(autorizado){
var soma=0;
for (var i=0;i<this.num_dias;i++) {
if ((this.excedentes[i]>0) && (this.autorizacoesHE[i]==autorizado)) {
soma+=this.excedentes[i];
}
}
return soma;
}
atualiza_tudo(){
var mes_anterior=new Date(this.mes.getFullYear(),this.mes.getMonth()-1,1);
var mes_ano_anterior=(mes_anterior.getMonth()+1)+"/"+mes_anterior.getFullYear()
var espelho=this;
//console.log("Buscando saldo de "+mes_ano_anterior+"... async="+this.async);
$.ajax({
url: "https://sistemas5.tre-sc.gov.br/portal-servidor/BancoHorasAction_recuperarExtrato",
async: this.async,
success: function ( code, textStatus, request )
{
var html = $(code);
var lingua = request.getResponseHeader('Content-Language');
html.find("table").find("tr").each (function( index, linha ) {
var mes=linha.children[1].innerHTML;
if (mes != mes_ano_anterior) return true;
espelho.saldo_mes_anterior=str2time(linha.children[5].innerHTML)
//console.log("saldo: "+time2str(espelho.saldo_mes_anterior));
//console.log("atrasos:",time2str(espelho.atrasos()));
//console.log("extras:",time2str(espelho.extras()));
//console.log("extras autorizados:",time2str(espelho.extras_autorizacao(true)));
//console.log("extras n autorizados:",time2str(espelho.extras_autorizacao(false)));
//console.log("jornada:",time2str(espelho.jornada_mensal()));
espelho.agora();
return false;
});
if (espelho.saldo_mes_anterior == null) {
console.log ("Sem saldo do mês anterior "+mes_ano_anterior+". Posso tentar estimar pelo espelho do mês "+mes_ano_anterior);
$.ajax({
type: 'post',
async: false,
data: { "unidadeSelecionada.idUnidade":$("#unidadeSelecionada_idUnidade").val(), "servidorSelecionado.matricula":$("#servidorSelecionado_matricula").val(), "anoSelecionado:":mes_anterior.getFullYear(), "mesSelecionado":mes_anterior.getMonth()+1},
success: function ( code, textStatus, request )
{
var html = $(code);
var tabela_mes_anterior=html.find("#tblEspelhoPontoMesCorrente tr");
var espelho_mes_anterior=new EspelhoPonto(tabela_mes_anterior);
espelho.saldo_mes_anterior=espelho_mes_anterior.saldo_mes;
espelho.agora();
},
error: function ( code )
{
alert("Falha ao consultar o espelho do mês anterior para tentar obter o saldo estimado");
}
});
}
},
error: function ( code )
{
alert("Falha ao consultar saldo de horas do mês anterior");
}
});
}
atualiza_resumos(){
var atrasos=this.atrasos()
var extras=this.extras()
var extras_naoautorizados=this.extras_autorizacao(false)
// Totais
this.atualiza_campo(this.tabela[this.num_dias+1].children[1],time2str(this.jornada_mensal()))
this.atualiza_campo(this.tabela[this.num_dias+1].children[2],time2str(extras-atrasos))
// Banco de horas
var saldo_avencer=this.saldo_mes_anterior
if (saldo_avencer==null) {
console.log("Ainda sem saldo do mês anterior")
return true
}
var saldo_consumido=0
var pendente=0
var residuo=0
var msg_pendente=""
var saldo_mes=0
//saldo_avencer=-5*60*60*1000
//console.log("saldo a vencer:",time2str(saldo_avencer))
//console.log("atrasos:",time2str(atrasos));
if (saldo_avencer>0) {
// Estamos sobreando, consuma dos atrasos
pendente=Math.max(0,saldo_avencer-atrasos);
saldo_consumido=Math.min(saldo_avencer, atrasos);
msg_pendente="Gaste ou perca!"
} else {
// Estamos devendo, consuma dos extras não homologados
pendente=Math.min(0,saldo_avencer+extras_naoautorizados);
saldo_consumido=Math.max(saldo_avencer, -extras_naoautorizados);
msg_pendente="Compense ou pague!"
}
residuo=saldo_avencer-saldo_consumido
saldo_mes=extras_naoautorizados-atrasos+saldo_consumido
//console.log("pendente:",time2str(pendente))
//console.log("consumido:",time2str(saldo_consumido))
this.atualiza_campo(this.campo("Horas Utilizadas do Banco de Horas:"),time2str(saldo_consumido))
this.atualiza_campo(this.campo("Resíduo de Horas:"),time2str(pendente))
if (pendente==0) msg_pendente="☺☮☺"
this.atualiza_campo($(this.campo("Resíduo de Horas:")).next()[0],msg_pendente)
this.atualiza_campo(this.campo("Saldo do Banco de Horas:"),time2str(saldo_avencer))
this.atualiza_campo($(this.campo("Saldo do Banco de Horas:")).next()[0],"Saldo atual para o próximo mês: "+time2str(saldo_mes))
this.saldo_mes=saldo_mes
}
campo(label) {
var found=this.tabela.find("td:contains('"+label+"')")
if (found.lentgh==0) return;
return found.next()[0];
}
atualiza_campo(campo,valor) {
$(campo).css('color', 'red');
$(campo).addClass('dinamico');
campo.innerHTML=valor;
}
}
function on_minuto() {
espelho.agora()
}
if ($("td:contains('Mês fechado pelo sistema.')").length==0) {
// Mês não fechado
var espelho=new EspelhoPonto();
espelho.agora();
// Roda na virada do minuto
setTimeout(function() {
on_minuto();
setInterval(on_minuto, 60000);
}, (60 - (new Date()).getSeconds()) * 1000);
// Arruma o alinhamento dos totais
$(".cellTotais").css("text-align","center");
} else {
// Mês fechado
}
// Evita conflitos se a página já usa jQuery
this.$ = this.jQuery = jQuery.noConflict(true);