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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
|
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Gestor.Application.Helpers;
using Gestor.Application.Servicos;
using Gestor.Application.Servicos.Ajuda;
using Gestor.Application.Servicos.Ferramentas;
using Gestor.Application.Servicos.Generic;
using Gestor.Application.ViewModels.Generic;
using Gestor.Common.Security;
using Gestor.Model.API;
using Gestor.Model.Common;
using Gestor.Model.Domain.Common;
using Gestor.Model.Domain.Generic;
using Gestor.Model.Domain.Seguros;
using Gestor.Model.License;
namespace Gestor.Application.ViewModels;
public class TutorialViewModel : BaseViewModel
{
private bool _fase1;
private bool _fase2;
private bool _fase3;
private string _mensagem = "ESTAMOS PREPARANDO TUDO PRA VOCÊ INICIAR";
private string _nome;
private string _usuario;
private string _senha;
private string _confirmacaoSenha;
private ObservableCollection<Seguradora> _seguradoras = new ObservableCollection<Seguradora>();
private ObservableCollection<Ramo> _ramos = new ObservableCollection<Ramo>();
private bool _loadingVisibility;
public bool Fase1
{
get
{
return _fase1;
}
set
{
_fase1 = value;
OnPropertyChanged("Fase1");
}
}
public bool Fase2
{
get
{
return _fase2;
}
set
{
_fase2 = value;
OnPropertyChanged("Fase2");
}
}
public bool Fase3
{
get
{
return _fase3;
}
set
{
_fase3 = value;
OnPropertyChanged("Fase3");
}
}
public string Mensagem
{
get
{
return _mensagem;
}
set
{
_mensagem = value;
OnPropertyChanged("Mensagem");
}
}
private long _idUsuario { get; set; } = -1L;
public string Nome
{
get
{
return _nome;
}
set
{
_nome = value;
OnPropertyChanged("Nome");
}
}
public string Usuario
{
get
{
return _usuario;
}
set
{
_usuario = value;
OnPropertyChanged("Usuario");
}
}
public string Senha
{
get
{
return _senha;
}
set
{
_senha = value;
OnPropertyChanged("Senha");
}
}
public string ConfirmacaoSenha
{
get
{
return _confirmacaoSenha;
}
set
{
_confirmacaoSenha = value;
OnPropertyChanged("ConfirmacaoSenha");
}
}
public ObservableCollection<Seguradora> Seguradoras
{
get
{
return _seguradoras;
}
set
{
_seguradoras = value;
OnPropertyChanged("Seguradoras");
}
}
public ObservableCollection<Ramo> Ramos
{
get
{
return _ramos;
}
set
{
_ramos = value;
OnPropertyChanged("Ramos");
}
}
private Customer Empresa { get; set; }
public bool LoadingVisibility
{
get
{
return _loadingVisibility;
}
set
{
_loadingVisibility = value;
OnPropertyChanged("LoadingVisibility");
}
}
public TutorialViewModel()
{
base.EnableFields = true;
}
public async Task IniciarConfiguracoes()
{
base.EnableButtons = false;
ConnectionHelper connectionHelper = new ConnectionHelper();
EmpresaServico servicoEmpresa = new EmpresaServico();
Empresa savedEmpresa = await servicoEmpresa.BuscarEmpresaPorId(1L);
Empresa = await Connection.Get<Customer>($"Customer/{ApplicationHelper.IdFornecedor}");
if (savedEmpresa == null)
{
await servicoEmpresa.Save(Empresa);
}
BaseServico servico = new BaseServico();
ControleArquivoDigital val = (ControleArquivoDigital)(((object)(await servico.BuscarControle())) ?? ((object)new ControleArquivoDigital
{
Catalogo = Connection.Catalog,
Tabela = "Arquivos"
}));
if (((DomainBase)val).Id == 0L)
{
await servico.SalvarControle(val);
}
VendedorServico vendedorServico = new VendedorServico();
Vendedor val2 = (Vendedor)(((object)(await vendedorServico.BuscarCorretora())) ?? ((object)new Vendedor
{
IdEmpresa = 1L,
Nome = Empresa.Name,
Ativo = true,
Corretora = true
}));
if (((DomainBase)val2).Id == 0L)
{
Repasse repasse = new Repasse
{
Forma = (FormaRepasse)1,
Incidencia = (TipoIncidencia)1,
Ativo = true,
Tipo = (TipoRepasse)2,
ValorNovo = 0m,
ValorRenovacao = 0m
};
await vendedorServico.Save(val2, repasse);
if (!vendedorServico.Sucesso)
{
return;
}
}
await new ArquivoDigitalServico().Criar();
string text = await connectionHelper.AtualizarBanco();
if (text != null)
{
await ShowMessage(text);
return;
}
List<Seguradora> seguradora = await new SeguradoraServico().BuscarSeguradoras();
List<Ramo> source = await new RamoServico().BuscarRamos();
Seguradoras = new ObservableCollection<Seguradora>(seguradora.OrderBy((Seguradora x) => x.Nome));
Ramos = new ObservableCollection<Ramo>(source.OrderBy((Ramo x) => x.Nome));
base.EnableButtons = true;
}
public async Task<bool> ValidarLogin()
{
Fase1 = false;
Mensagem = "ESTAMOS VALIDANDO AS INFORMAÇÕES";
if (string.IsNullOrEmpty(Usuario) || string.IsNullOrEmpty(Senha) || string.IsNullOrEmpty(ConfirmacaoSenha) || string.IsNullOrEmpty(Nome))
{
await ShowMessage("NECESSÁRIO PREENCHER O NOME COMPLETO, USUÁRIO, SENHA E CONFIRMAÇÃO DA SENHA PARA PROSSEGUIR.");
Fase1 = true;
return true;
}
if (!string.IsNullOrEmpty(Usuario) && !Regex.IsMatch(Usuario, "^(?!.*(@.*@|@\\.|\\.@|--|\\.\\.|-\\.|\\.-|-@|@-))[a-zA-Z0-9](?:[a-zA-Z0-9@.-]*[a-zA-Z0-9])?(?: [a-zA-Z0-9](?:[a-zA-Z0-9@.-]*[a-zA-Z0-9])?)?$"))
{
await ShowMessage("USUÁRIO DEVE CONTER APENAS LETRAS E NÚMEROS," + Environment.NewLine + "NÃO DEVE CONTER MAIS DE UM ESPAÇO," + Environment.NewLine + "NÃO DEVE TER ESPAÇO NO INICIO E NO FIM." + Environment.NewLine + "EXEMPLO VALIDO:" + Environment.NewLine + "USUARIO01 OU USUARIO 02 OU USUARIO.03");
Fase1 = true;
return true;
}
if (Usuario.Length > 30)
{
await ShowMessage("USUÁRIO SOMENTE DEVE CONTER NO MÁXIMO 30 CARACTERES");
Fase1 = true;
return true;
}
if (Senha != ConfirmacaoSenha)
{
await ShowMessage("A SENHA E A CONFIRMAÇÃO DA SENHA ESTÃO DIFERENTES, CORRIJA PARA PROSSEGUIR.");
Fase1 = true;
return true;
}
if (!(await UsuarioInicial()))
{
return false;
}
while (Seguradoras.Count == 0)
{
await Task.Delay(25);
}
Fase1 = false;
Fase2 = true;
return true;
}
public async Task<string> CriarBanco()
{
string text = await new ConnectionHelper().VerifyConnection();
if (text == null)
{
Instancia.ExcluirCfg();
return await VerificaEmpresa();
}
return text;
}
public async Task<string> VerificaEmpresa()
{
ConnectionHelper connectionHelper = new ConnectionHelper();
Empresa = await Connection.Get<Customer>($"Customer/{ApplicationHelper.IdFornecedor}");
EmpresaServico servico = new EmpresaServico();
if (!connectionHelper.ExisteEmpresa())
{
await servico.Save(Empresa);
if (!servico.Sucesso)
{
return "ERRO AO CRIAR BANCO DE DADOS";
}
}
else if (!connectionHelper.BDConvertido())
{
return "HOUVE UM ERRO NO ACESSO! REINICIE O SISTEMA!";
}
return null;
}
public void DesativarRamos()
{
foreach (Ramo ramo in Ramos)
{
ramo.Ativo = false;
}
Ramos = new ObservableCollection<Ramo>(Ramos);
}
public void DesativarSeguradoras()
{
foreach (Seguradora seguradora in Seguradoras)
{
seguradora.Ativo = false;
}
Seguradoras = new ObservableCollection<Seguradora>(Seguradoras);
}
public async Task<bool> Concluir()
{
Fase3 = false;
if (Ramos.All((Ramo x) => !x.Ativo))
{
await ShowMessage("NECESSÁRIO AO MENOS UM RAMO ATIVO PARA PROSSEGUIR.");
Fase3 = true;
return false;
}
Mensagem = "ESTAMOS TERMINANDO AS CONFIGURAÇÕES INICIAIS E LOGO MAIS VOCÊ PODERÁ UTILIZAR.";
if (!(await UsuarioInicial()))
{
return false;
}
SeguradoraServico seguradoraServico = new SeguradoraServico();
foreach (Seguradora seguradora in Seguradoras)
{
await seguradoraServico.Save(seguradora, null, primeiroAcesso: true);
if (!seguradoraServico.Sucesso)
{
return false;
}
}
RamoServico ramoServico = new RamoServico();
foreach (Ramo ramo in Ramos)
{
await ramoServico.Save(ramo, null);
if (!ramoServico.Sucesso)
{
return false;
}
}
await new BaseServico().LoadInicialParameters();
return true;
}
public async Task<bool> UsuarioInicial()
{
Usuario val = await new UsuarioServico().BuscaUsuarioInicial(Usuario, Senha);
if (val == null)
{
await ShowMessage("USUÁRIO JÁ EXISTE E A SENHA ESTÁ INCORRETA, REINICIE O SISTEMA!");
return false;
}
await SalvarUsuario(val);
return true;
}
private async Task SalvarUsuario(Usuario verificaUsuario)
{
_idUsuario = ((verificaUsuario != null) ? ((DomainBase)verificaUsuario).Id : 0);
Usuario val = new Usuario
{
Id = _idUsuario,
Nome = Nome,
Login = Usuario,
Senha = new Token().AggerEncrypt(Senha),
Administrador = true,
Excluido = false,
IdEmpresa = 1L,
PermissaoAggilizador = 1L
};
val.SsoId = ((verificaUsuario != null) ? verificaUsuario.SsoId : null);
Usuario usuario = val;
usuario = await new UsuarioServico().Save(usuario);
_idUsuario = ((DomainBase)usuario).Id;
Recursos.Usuario = usuario;
}
public async Task ValidarSeguradoras()
{
Fase2 = false;
if (Seguradoras.All((Seguradora x) => !x.Ativo))
{
await ShowMessage("NECESSÁRIO AO MENOS UMA SEGURADORA ATIVA PARA PROSSEGUIR.");
Fase2 = true;
}
else
{
Fase2 = false;
Fase3 = true;
}
}
public async Task VerificarUsuario()
{
Instancia.ExcluirCfg();
Usuario val = await new UsuarioServico().BuscarUsuarioPorId(1L);
if (val != null)
{
Nome = val.Nome;
Usuario = val.Login;
_idUsuario = ((DomainBase)val).Id;
}
}
public async Task<bool> VerificaAcesso()
{
LicenseHelper license = new LicenseHelper();
bool registrar = false;
if (!(await license.VerificarMaquina()))
{
Loading(isLoading: false);
while (!registrar)
{
List<Instalacao> licencas = await license.GetInstalacoes();
if (licencas.Count == 0)
{
await ShowMessage("ACESSO NEGADO, NÃO EXISTEM LICENÇAS VÁLIDAS.");
return false;
}
Instalacao instalacaoUsuario = ((IEnumerable<Instalacao>)licencas.OrderByDescending((Instalacao x) => x.UltimoAcesso)).FirstOrDefault((Func<Instalacao, bool>)((Instalacao x) => x.UsuarioId == ((DomainBase)Recursos.Usuario).Id));
bool flag = instalacaoUsuario != null;
if (flag)
{
flag = await ShowMessage("SEU ULTIMO ACESSO FOI NA MAQUINA " + instalacaoUsuario.NomeMaquina + ". DESEJA SUBSTITUIR A LICENÇA PARA ESSA MAQUINA?", "SIM", "NÃO");
}
Instalacao val = ((!flag) ? (await ShowInstalacoes(licencas)) : instalacaoUsuario);
Instalacao val2 = val;
if (val2 == null)
{
await ShowMessage("MÁQUINA NÃO REGISTRADA");
return false;
}
await new AjudaServico().ExcluirInstalacao(((DomainBase)val2).Id);
registrar = await license.VerificarMaquina();
}
}
return true;
}
}
|