summaryrefslogtreecommitdiff
path: root/Codemerx/Gestor.Application/ViewModels/TutorialViewModel.cs
blob: d027b834f0d4606188661e5428dea4b1b2ee0fd6 (plain)
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
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;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

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;

		private long _idUsuario { get; set; } = (long)-1;

		public string ConfirmacaoSenha
		{
			get
			{
				return this._confirmacaoSenha;
			}
			set
			{
				this._confirmacaoSenha = value;
				base.OnPropertyChanged("ConfirmacaoSenha");
			}
		}

		private Customer Empresa
		{
			get;
			set;
		}

		public bool Fase1
		{
			get
			{
				return this._fase1;
			}
			set
			{
				this._fase1 = value;
				base.OnPropertyChanged("Fase1");
			}
		}

		public bool Fase2
		{
			get
			{
				return this._fase2;
			}
			set
			{
				this._fase2 = value;
				base.OnPropertyChanged("Fase2");
			}
		}

		public bool Fase3
		{
			get
			{
				return this._fase3;
			}
			set
			{
				this._fase3 = value;
				base.OnPropertyChanged("Fase3");
			}
		}

		public bool LoadingVisibility
		{
			get
			{
				return this._loadingVisibility;
			}
			set
			{
				this._loadingVisibility = value;
				base.OnPropertyChanged("LoadingVisibility");
			}
		}

		public string Mensagem
		{
			get
			{
				return this._mensagem;
			}
			set
			{
				this._mensagem = value;
				base.OnPropertyChanged("Mensagem");
			}
		}

		public string Nome
		{
			get
			{
				return this._nome;
			}
			set
			{
				this._nome = value;
				base.OnPropertyChanged("Nome");
			}
		}

		public ObservableCollection<Ramo> Ramos
		{
			get
			{
				return this._ramos;
			}
			set
			{
				this._ramos = value;
				base.OnPropertyChanged("Ramos");
			}
		}

		public ObservableCollection<Seguradora> Seguradoras
		{
			get
			{
				return this._seguradoras;
			}
			set
			{
				this._seguradoras = value;
				base.OnPropertyChanged("Seguradoras");
			}
		}

		public string Senha
		{
			get
			{
				return this._senha;
			}
			set
			{
				this._senha = value;
				base.OnPropertyChanged("Senha");
			}
		}

		public string Usuario
		{
			get
			{
				return this._usuario;
			}
			set
			{
				this._usuario = value;
				base.OnPropertyChanged("Usuario");
			}
		}

		public TutorialViewModel()
		{
			base.EnableFields = true;
		}

		public async Task<bool> Concluir()
		{
			bool flag;
			SeguradoraServico seguradoraServico;
			RamoServico ramoServico;
			this.Fase3 = false;
			ObservableCollection<Ramo> ramos = this.Ramos;
			if (!ramos.All<Ramo>((Ramo x) => !x.get_Ativo()))
			{
				this.Mensagem = "ESTAMOS TERMINANDO AS CONFIGURAÇÕES INICIAIS E LOGO MAIS VOCÊ PODERÁ UTILIZAR.";
				if (await this.UsuarioInicial())
				{
					seguradoraServico = new SeguradoraServico();
					foreach (Seguradora seguradora in this.Seguradoras)
					{
						await seguradoraServico.Save(seguradora, null, true);
						if (seguradoraServico.Sucesso)
						{
							continue;
						}
						flag = false;
						seguradoraServico = null;
						ramoServico = null;
						return flag;
					}
					ramoServico = new RamoServico();
					foreach (Ramo ramo in this.Ramos)
					{
						await ramoServico.Save(ramo, null);
						if (ramoServico.Sucesso)
						{
							continue;
						}
						flag = false;
						seguradoraServico = null;
						ramoServico = null;
						return flag;
					}
					await (new BaseServico()).LoadInicialParameters();
					flag = true;
				}
				else
				{
					flag = false;
				}
			}
			else
			{
				await base.ShowMessage("NECESSÁRIO AO MENOS UM RAMO ATIVO PARA PROSSEGUIR.", "OK", "", false);
				this.Fase3 = true;
				flag = false;
			}
			seguradoraServico = null;
			ramoServico = null;
			return flag;
		}

		public async Task<string> CriarBanco()
		{
			string str;
			string str1 = await (new ConnectionHelper()).VerifyConnection();
			if (str1 != null)
			{
				str = str1;
			}
			else
			{
				Instancia.ExcluirCfg();
				str = await this.VerificaEmpresa();
			}
			return str;
		}

		public void DesativarRamos()
		{
			foreach (Ramo ramo in this.Ramos)
			{
				ramo.set_Ativo(false);
			}
			this.Ramos = new ObservableCollection<Ramo>(this.Ramos);
		}

		public void DesativarSeguradoras()
		{
			foreach (Seguradora seguradora in this.Seguradoras)
			{
				seguradora.set_Ativo(false);
			}
			this.Seguradoras = new ObservableCollection<Seguradora>(this.Seguradoras);
		}

		public async Task IniciarConfiguracoes()
		{
			List<Seguradora> seguradoras;
			base.EnableButtons = false;
			ConnectionHelper connectionHelper = new ConnectionHelper();
			EmpresaServico empresaServico = new EmpresaServico();
			Gestor.Model.Domain.Common.Empresa empresa = await empresaServico.BuscarEmpresaPorId((long)1);
			this.Empresa = await Connection.Get<Customer>(string.Format("Customer/{0}", ApplicationHelper.IdFornecedor), true, false);
			if (empresa == null)
			{
				await empresaServico.Save(this.Empresa);
			}
			BaseServico baseServico = new BaseServico();
			ControleArquivoDigital controleArquivoDigital = await baseServico.BuscarControle();
			if (controleArquivoDigital == null)
			{
				controleArquivoDigital = new ControleArquivoDigital();
				controleArquivoDigital.set_Catalogo(Connection.Catalog);
				controleArquivoDigital.set_Tabela("Arquivos");
			}
			ControleArquivoDigital controleArquivoDigital1 = controleArquivoDigital;
			if (controleArquivoDigital1.get_Id() == 0)
			{
				await baseServico.SalvarControle(controleArquivoDigital1);
			}
			VendedorServico vendedorServico = new VendedorServico();
			Vendedor vendedor = await vendedorServico.BuscarCorretora();
			if (vendedor == null)
			{
				vendedor = new Vendedor();
				vendedor.set_IdEmpresa((long)1);
				vendedor.set_Nome(this.Empresa.get_Name());
				vendedor.set_Ativo(true);
				vendedor.set_Corretora(true);
			}
			Vendedor vendedor1 = vendedor;
			if (vendedor1.get_Id() == 0)
			{
				Repasse repasse = new Repasse();
				repasse.set_Forma(new FormaRepasse?(1));
				repasse.set_Incidencia(new TipoIncidencia?(1));
				repasse.set_Ativo(true);
				repasse.set_Tipo(new TipoRepasse?(2));
				repasse.set_ValorNovo(decimal.Zero);
				repasse.set_ValorRenovacao(decimal.Zero);
				await vendedorServico.Save(vendedor1, repasse);
				if (!vendedorServico.Sucesso)
				{
					connectionHelper = null;
					empresaServico = null;
					empresa = null;
					baseServico = null;
					vendedorServico = null;
					seguradoras = null;
					return;
				}
			}
			await (new Gestor.Application.Servicos.ArquivoDigitalServico()).Criar();
			string str = await connectionHelper.AtualizarBanco();
			if (str == null)
			{
				seguradoras = await (new SeguradoraServico()).BuscarSeguradoras();
				List<Ramo> ramos = await (new RamoServico()).BuscarRamos();
				TutorialViewModel observableCollection = this;
				List<Seguradora> seguradoras1 = seguradoras;
				observableCollection.Seguradoras = new ObservableCollection<Seguradora>(
					from x in seguradoras1
					orderby x.get_Nome()
					select x);
				TutorialViewModel tutorialViewModel = this;
				List<Ramo> ramos1 = ramos;
				tutorialViewModel.Ramos = new ObservableCollection<Ramo>(
					from x in ramos1
					orderby x.get_Nome()
					select x);
				base.EnableButtons = true;
			}
			else
			{
				await base.ShowMessage(str, "OK", "", false);
			}
			connectionHelper = null;
			empresaServico = null;
			empresa = null;
			baseServico = null;
			vendedorServico = null;
			seguradoras = null;
		}

		private async Task SalvarUsuario(Gestor.Model.Domain.Seguros.Usuario verificaUsuario)
		{
			long id;
			string ssoId;
			TutorialViewModel tutorialViewModel = this;
			Gestor.Model.Domain.Seguros.Usuario usuario = verificaUsuario;
			if (usuario != null)
			{
				id = usuario.get_Id();
			}
			else
			{
				id = (long)0;
			}
			tutorialViewModel._idUsuario = id;
			Gestor.Model.Domain.Seguros.Usuario usuario1 = new Gestor.Model.Domain.Seguros.Usuario();
			usuario1.set_Id(this._idUsuario);
			usuario1.set_Nome(this.Nome);
			usuario1.set_Login(this.Usuario);
			usuario1.set_Senha((new Token()).AggerEncrypt(this.Senha));
			usuario1.set_Administrador(true);
			usuario1.set_Excluido(false);
			usuario1.set_IdEmpresa((long)1);
			usuario1.set_PermissaoAggilizador(new long?((long)1));
			Gestor.Model.Domain.Seguros.Usuario usuario2 = verificaUsuario;
			if (usuario2 != null)
			{
				ssoId = usuario2.get_SsoId();
			}
			else
			{
				ssoId = null;
			}
			usuario1.set_SsoId(ssoId);
			Gestor.Model.Domain.Seguros.Usuario usuario3 = usuario1;
			usuario3 = await (new UsuarioServico()).Save(usuario3);
			this._idUsuario = usuario3.get_Id();
			Recursos.Usuario = usuario3;
		}

		public async Task<bool> UsuarioInicial()
		{
			bool flag;
			Gestor.Model.Domain.Seguros.Usuario usuario = await (new UsuarioServico()).BuscaUsuarioInicial(this.Usuario, this.Senha);
			if (usuario != null)
			{
				await this.SalvarUsuario(usuario);
				flag = true;
			}
			else
			{
				await base.ShowMessage("USUÁRIO JÁ EXISTE E A SENHA ESTÁ INCORRETA, REINICIE O SISTEMA!", "OK", "", false);
				flag = false;
			}
			return flag;
		}

		public async Task<bool> ValidarLogin()
		{
			bool flag;
			this.Fase1 = false;
			this.Mensagem = "ESTAMOS VALIDANDO AS INFORMAÇÕES";
			if (string.IsNullOrEmpty(this.Usuario) || string.IsNullOrEmpty(this.Senha) || string.IsNullOrEmpty(this.ConfirmacaoSenha) || string.IsNullOrEmpty(this.Nome))
			{
				await base.ShowMessage("NECESSÁRIO PREENCHER O NOME COMPLETO, USUÁRIO, SENHA E CONFIRMAÇÃO DA SENHA PARA PROSSEGUIR.", "OK", "", false);
				this.Fase1 = true;
				flag = true;
			}
			else if (!string.IsNullOrEmpty(this.Usuario) && !Regex.IsMatch(this.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])?)?$"))
			{
				string[] newLine = new string[] { "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" };
				await base.ShowMessage(string.Concat(newLine), "OK", "", false);
				this.Fase1 = true;
				flag = true;
			}
			else if (this.Usuario.Length > 30)
			{
				await base.ShowMessage("USUÁRIO SOMENTE DEVE CONTER NO MÁXIMO 30 CARACTERES", "OK", "", false);
				this.Fase1 = true;
				flag = true;
			}
			else if (this.Senha != this.ConfirmacaoSenha)
			{
				await base.ShowMessage("A SENHA E A CONFIRMAÇÃO DA SENHA ESTÃO DIFERENTES, CORRIJA PARA PROSSEGUIR.", "OK", "", false);
				this.Fase1 = true;
				flag = true;
			}
			else if (await this.UsuarioInicial())
			{
				while (this.Seguradoras.Count == 0)
				{
					await Task.Delay(25);
				}
				this.Fase1 = false;
				this.Fase2 = true;
				flag = true;
			}
			else
			{
				flag = false;
			}
			return flag;
		}

		public async Task ValidarSeguradoras()
		{
			this.Fase2 = false;
			ObservableCollection<Seguradora> seguradoras = this.Seguradoras;
			if (!seguradoras.All<Seguradora>((Seguradora x) => !x.get_Ativo()))
			{
				this.Fase2 = false;
				this.Fase3 = true;
			}
			else
			{
				await base.ShowMessage("NECESSÁRIO AO MENOS UMA SEGURADORA ATIVA PARA PROSSEGUIR.", "OK", "", false);
				this.Fase2 = true;
			}
		}

		public async Task<bool> VerificaAcesso()
		{
			bool flag;
			Instalacao instalacao;
			LicenseHelper licenseHelper = new LicenseHelper();
			bool flag1 = false;
			if (!await licenseHelper.VerificarMaquina())
			{
				base.Loading(false);
				while (!flag1)
				{
					List<Instalacao> instalacoes = await licenseHelper.GetInstalacoes();
					if (instalacoes.Count != 0)
					{
						List<Instalacao> instalacaos = instalacoes;
						IOrderedEnumerable<Instalacao> ultimoAcesso = 
							from x in instalacaos
							orderby x.get_UltimoAcesso() descending
							select x;
						Instalacao instalacao1 = ultimoAcesso.FirstOrDefault<Instalacao>((Instalacao x) => x.get_UsuarioId() == Recursos.Usuario.get_Id());
						bool flag2 = instalacao1 != null;
						if (flag2)
						{
							flag2 = await base.ShowMessage(string.Concat("SEU ULTIMO ACESSO FOI NA MAQUINA ", instalacao1.get_NomeMaquina(), ". DESEJA SUBSTITUIR A LICENÇA PARA ESSA MAQUINA?"), "SIM", "NÃO", false);
						}
						if (!flag2)
						{
							instalacao = await base.ShowInstalacoes(instalacoes);
						}
						else
						{
							instalacao = instalacao1;
						}
						Instalacao instalacao2 = instalacao;
						if (instalacao2 != null)
						{
							await (new AjudaServico()).ExcluirInstalacao(instalacao2.get_Id());
							flag2 = await licenseHelper.VerificarMaquina();
							flag1 = flag2;
							instalacoes = null;
							instalacao1 = null;
						}
						else
						{
							await base.ShowMessage("MÁQUINA NÃO REGISTRADA", "OK", "", false);
							flag = false;
							licenseHelper = null;
							return flag;
						}
					}
					else
					{
						await base.ShowMessage("ACESSO NEGADO, NÃO EXISTEM LICENÇAS VÁLIDAS.", "OK", "", false);
						flag = false;
						licenseHelper = null;
						return flag;
					}
				}
			}
			flag = true;
			licenseHelper = null;
			return flag;
		}

		public async Task<string> VerificaEmpresa()
		{
			string str;
			ConnectionHelper connectionHelper = new ConnectionHelper();
			this.Empresa = await Connection.Get<Customer>(string.Format("Customer/{0}", ApplicationHelper.IdFornecedor), true, false);
			EmpresaServico empresaServico = new EmpresaServico();
			if (!connectionHelper.ExisteEmpresa())
			{
				await empresaServico.Save(this.Empresa);
				if (!empresaServico.Sucesso)
				{
					str = "ERRO AO CRIAR BANCO DE DADOS";
					connectionHelper = null;
					empresaServico = null;
					return str;
				}
			}
			else if (!connectionHelper.BDConvertido())
			{
				str = "HOUVE UM ERRO NO ACESSO! REINICIE O SISTEMA!";
				connectionHelper = null;
				empresaServico = null;
				return str;
			}
			str = null;
			connectionHelper = null;
			empresaServico = null;
			return str;
		}

		public async Task VerificarUsuario()
		{
			Instancia.ExcluirCfg();
			Gestor.Model.Domain.Seguros.Usuario usuario = await (new UsuarioServico()).BuscarUsuarioPorId((long)1);
			if (usuario != null)
			{
				this.Nome = usuario.get_Nome();
				this.Usuario = usuario.get_Login();
				this._idUsuario = usuario.get_Id();
			}
		}
	}
}