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
613
614
615
616
617
618
619
620
621
|
using Gestor.Application.Helpers;
using Gestor.Application.Servicos.Ferramentas;
using Gestor.Application.Servicos.Generic;
using Gestor.Application.ViewModels.Generic;
using Gestor.Common.Validation;
using Gestor.Model.Common;
using Gestor.Model.Domain.Configuracoes;
using Gestor.Model.Domain.Generic;
using Gestor.Model.Domain.Seguros;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Windows;
namespace Gestor.Application.ViewModels.Ferramentas
{
public class SeguradoraViewModel : BaseSegurosViewModel
{
private readonly SeguradoraServico _servico;
private bool _apelido;
private Seguradora _selectedSeguradora;
private SeguradoraEndereco _selectedEndereco = new SeguradoraEndereco();
private ObservableCollection<SeguradoraContato> _contatos = new ObservableCollection<SeguradoraContato>();
private ObservableCollection<SeguradoraEndereco> _enderecos = new ObservableCollection<SeguradoraEndereco>();
private SeguradoraContato _selectedTelefone = new SeguradoraContato();
private Visibility _tipoTelefone;
private Visibility _telefone;
private Visibility _prefixo;
private ObservableCollection<ConfigExtratoImport> _configFiltrados = new ObservableCollection<ConfigExtratoImport>();
public Seguradora CancelSeguradora;
public ObservableCollection<SeguradoraEndereco> CancelEnderecos;
public ObservableCollection<SeguradoraContato> CancelContatos;
private ObservableCollection<Seguradora> _seguradorasFiltradas = new ObservableCollection<Seguradora>();
private bool _isExpanded;
public bool Apelido
{
get
{
return this._apelido;
}
set
{
this._apelido = value;
base.OnPropertyChanged("Apelido");
}
}
public List<ConfigExtratoImport> Config
{
get;
set;
}
public ObservableCollection<ConfigExtratoImport> ConfigFiltrados
{
get
{
return this._configFiltrados;
}
set
{
this._configFiltrados = value;
base.OnPropertyChanged("ConfigFiltrados");
}
}
public ObservableCollection<SeguradoraContato> Contatos
{
get
{
return this._contatos;
}
set
{
this._contatos = value;
base.OnPropertyChanged("Contatos");
}
}
public ObservableCollection<SeguradoraEndereco> Enderecos
{
get
{
return this._enderecos;
}
set
{
this._enderecos = value;
base.OnPropertyChanged("Enderecos");
}
}
public bool IsExpanded
{
get
{
return this._isExpanded;
}
set
{
this._isExpanded = value;
base.OnPropertyChanged("IsExpanded");
}
}
public string Pesquisa
{
get;
set;
}
public Visibility PrefixoVisibility
{
get
{
return this._prefixo;
}
set
{
if (this.SelectedTelefone.get_TipoContato() != 2)
{
this._prefixo = Visibility.Visible;
}
else
{
this._prefixo = Visibility.Collapsed;
}
base.OnPropertyChanged("PrefixoVisibility");
}
}
public List<Seguradora> Seguradoras
{
get;
set;
}
public ObservableCollection<Seguradora> SeguradorasFiltradas
{
get
{
return this._seguradorasFiltradas;
}
set
{
this._seguradorasFiltradas = value;
this.IsExpanded = (value != null ? value.Count > 0 : false);
base.OnPropertyChanged("SeguradorasFiltradas");
}
}
public SeguradoraEndereco SelectedEndereco
{
get
{
return this._selectedEndereco;
}
set
{
this._selectedEndereco = value;
base.OnPropertyChanged("SelectedEndereco");
}
}
public Seguradora SelectedSeguradora
{
get
{
return this._selectedSeguradora;
}
set
{
long? nullable;
this._selectedSeguradora = value;
this.WorkOnSelectedSeguradora(value, true);
if (value != null)
{
nullable = new long?(value.get_Id());
}
else
{
nullable = null;
}
base.VerificarEnables(nullable);
base.OnPropertyChanged("SelectedSeguradora");
}
}
public SeguradoraContato SelectedTelefone
{
get
{
return this._selectedTelefone;
}
set
{
this._selectedTelefone = value;
base.OnPropertyChanged("SelectedTelefone");
}
}
public Visibility TelefoneVisibility
{
get
{
return this._telefone;
}
set
{
if (this.SelectedTelefone.get_TipoContato() != 2)
{
this._telefone = Visibility.Visible;
}
else
{
this._telefone = Visibility.Collapsed;
}
base.OnPropertyChanged("TelefoneVisibility");
}
}
public Visibility TipoTelefoneVisibility
{
get
{
return this._tipoTelefone;
}
set
{
if (this.SelectedTelefone.get_TipoContato() != 2)
{
this._tipoTelefone = Visibility.Visible;
}
else
{
this._tipoTelefone = Visibility.Collapsed;
}
base.OnPropertyChanged("TipoTelefoneVisibility");
}
}
public decimal Tolerancia
{
get;
set;
}
public SeguradoraViewModel()
{
this._servico = new SeguradoraServico();
this.Apelido = Recursos.Configuracoes.Any<ConfiguracaoSistema>((ConfiguracaoSistema x) => x.get_Configuracao() == 6);
base.EnableMenu = true;
this.Seleciona();
}
public void CancelarAlteracao()
{
if (this.CancelSeguradora != null && this.Seguradoras.Any<Seguradora>((Seguradora x) => x.get_Id() == this.CancelSeguradora.get_Id()))
{
DomainBase.Copy<Seguradora, Seguradora>(this.Seguradoras.First<Seguradora>((Seguradora x) => x.get_Id() == this.CancelSeguradora.get_Id()), this.CancelSeguradora);
if (this.SeguradorasFiltradas.Count <= 0 || !this.SeguradorasFiltradas.Any<Seguradora>((Seguradora x) => x.get_Id() == this.CancelSeguradora.get_Id()))
{
this.SeguradorasFiltradas.Add(this.CancelSeguradora);
}
else
{
DomainBase.Copy<Seguradora, Seguradora>(this.SeguradorasFiltradas.First<Seguradora>((Seguradora x) => x.get_Id() == this.CancelSeguradora.get_Id()), this.CancelSeguradora);
}
this.SeguradorasFiltradas = new ObservableCollection<Seguradora>(this.SeguradorasFiltradas);
this.ConfigFiltrados = new ObservableCollection<ConfigExtratoImport>(this.ConfigFiltrados);
this.SelectedSeguradora = this.SeguradorasFiltradas.First<Seguradora>((Seguradora x) => x.get_Id() == this.CancelSeguradora.get_Id());
this.Enderecos = this.CancelEnderecos;
this.Contatos = this.CancelContatos;
}
base.Alterar(false);
}
public void Clonar()
{
this.CancelSeguradora = (Seguradora)this.SelectedSeguradora.Clone();
this.CancelEnderecos = new ObservableCollection<SeguradoraEndereco>(this.Enderecos);
this.CancelContatos = new ObservableCollection<SeguradoraContato>(this.Contatos);
}
public void ExcluirEndereco(SeguradoraEndereco endereco)
{
this.Enderecos.Remove(endereco);
}
public void ExcluirTelefone(SeguradoraContato contato)
{
this.Contatos.Remove(contato);
}
internal async Task<List<Seguradora>> Filtrar(string value)
{
List<Seguradora> seguradoras = await Task.Run<List<Seguradora>>(() => this.FiltrarSeguradora(value));
return seguradoras;
}
internal async Task<List<ConfigExtratoImport>> FiltrarConfig(string value)
{
List<ConfigExtratoImport> configExtratoImports = await Task.Run<List<ConfigExtratoImport>>(() => this.FiltrarDescricao(value));
return configExtratoImports;
}
public List<ConfigExtratoImport> FiltrarDescricao(string filter)
{
this.ConfigFiltrados = (string.IsNullOrWhiteSpace(filter) ? new ObservableCollection<ConfigExtratoImport>(this.Config) : new ObservableCollection<ConfigExtratoImport>(
from x in this.Config
where ValidationHelper.RemoveDiacritics(x.get_Descricao().Trim()).ToUpper().Contains(ValidationHelper.RemoveDiacritics(filter))
orderby x.get_Descricao()
select x));
return this.ConfigFiltrados.ToList<ConfigExtratoImport>();
}
public List<Seguradora> FiltrarSeguradora(string filter)
{
this.Pesquisa = filter;
this.SeguradorasFiltradas = (string.IsNullOrWhiteSpace(filter) ? new ObservableCollection<Seguradora>(this.Seguradoras) : new ObservableCollection<Seguradora>(this.Seguradoras.Where<Seguradora>((Seguradora x) => {
if (ValidationHelper.RemoveDiacritics(x.get_Nome().Trim()).ToUpper().Contains(ValidationHelper.RemoveDiacritics(filter)))
{
return true;
}
return ValidationHelper.RemoveDiacritics(x.get_NomeSocial()).ToUpper().Contains(ValidationHelper.RemoveDiacritics(filter));
}).OrderByDescending<Seguradora, bool>((Seguradora x) => x.get_Ativo()).ThenBy<Seguradora, string>((Seguradora x) => x.get_Nome())));
return this.SeguradorasFiltradas.ToList<Seguradora>();
}
public async void Incluir(Seguradora seguradora)
{
seguradora.set_Tolerancia(new decimal?(2));
seguradora.set_ToleranciaPremio(new decimal?(2));
seguradora.set_Ativo(true);
await this._servico.Insert(seguradora);
if (this._servico.Sucesso)
{
await this.SelecionaSeguradoras(seguradora);
}
}
public void IncluirEndereco()
{
if (this.SelectedSeguradora == null)
{
return;
}
if (this.Enderecos == null)
{
this.Enderecos = new ObservableCollection<SeguradoraEndereco>();
}
SeguradoraEndereco seguradoraEndereco = new SeguradoraEndereco();
seguradoraEndereco.set_Empresa(Recursos.Empresa);
seguradoraEndereco.set_Seguradora(this.SelectedSeguradora);
seguradoraEndereco.set_Tipo(4);
SeguradoraEndereco seguradoraEndereco1 = seguradoraEndereco;
this.Enderecos.Add(seguradoraEndereco1);
this.SelectedEndereco = seguradoraEndereco1;
}
public void IncluirTelefone()
{
if (this.SelectedSeguradora == null)
{
return;
}
if (this.Contatos == null)
{
this.Contatos = new ObservableCollection<SeguradoraContato>();
}
SeguradoraContato seguradoraContato = new SeguradoraContato();
seguradoraContato.set_Empresa(Recursos.Empresa);
seguradoraContato.set_Seguradora(this.SelectedSeguradora);
seguradoraContato.set_Tipo(new TipoTelefone?(2));
SeguradoraContato seguradoraContato1 = seguradoraContato;
this.Contatos.Add(seguradoraContato1);
this.SelectedTelefone = seguradoraContato1;
}
public async Task<List<KeyValuePair<string, string>>> Salvar()
{
List<KeyValuePair<string, string>> keyValuePairs;
string str;
List<KeyValuePair<string, string>> keyValuePairs1 = this.SelectedSeguradora.Validate();
keyValuePairs1.AddRange(this.Validate(this.Contatos.ToList<SeguradoraContato>()));
keyValuePairs1.AddRange(this.Validate(this.Enderecos.ToList<SeguradoraEndereco>()));
if (keyValuePairs1.Count <= 0)
{
Seguradora selectedSeguradora = this.SelectedSeguradora;
ObservableCollection<SeguradoraContato> contatos = this.Contatos;
selectedSeguradora.set_Contatos(contatos.Where<SeguradoraContato>((SeguradoraContato x) => {
if (!string.IsNullOrEmpty(x.get_NomeContato()))
{
return true;
}
return x.get_TipoContato() == 1;
}).ToList<SeguradoraContato>());
Seguradora seguradora = this.SelectedSeguradora;
ObservableCollection<SeguradoraEndereco> enderecos = this.Enderecos;
seguradora.set_Enderecos((
from x in enderecos
where !string.IsNullOrEmpty(x.get_Bairro())
select x).ToList<SeguradoraEndereco>());
Seguradora seguradora1 = await this._servico.Save(this.SelectedSeguradora, this.Config, false);
Seguradora seguradora2 = seguradora1;
str = (this.SelectedSeguradora.get_Id() == 0 ? "INCLUIU" : "ALTEROU");
string str1 = str;
if (this._servico.Sucesso)
{
base.RegistrarAcao(string.Concat(str1, " SEGURADORA \"", seguradora2.get_Nome(), "\""), seguradora2.get_Id(), new TipoTela?(13), string.Format("SEGURADORA \"{0}\", ID: {1}", seguradora2.get_Nome(), seguradora2.get_Id()));
await this.SelecionaSeguradoras(null);
this.FiltrarSeguradora(this.Pesquisa);
SeguradoraViewModel seguradoraViewModel = this;
Seguradora seguradora3 = this.SeguradorasFiltradas.FirstOrDefault<Seguradora>((Seguradora x) => x.get_Id() == seguradora2.get_Id());
if (seguradora3 == null)
{
seguradora3 = this.SeguradorasFiltradas.FirstOrDefault<Seguradora>();
}
seguradoraViewModel.SelectedSeguradora = seguradora3;
base.Alterar(false);
base.ToggleSnackBar("SEGURADORA SALVA COM SUCESSO", true);
keyValuePairs = null;
}
else
{
keyValuePairs = null;
}
}
else
{
keyValuePairs = keyValuePairs1;
}
return keyValuePairs;
}
private async void Seleciona()
{
base.Loading(true);
await base.PermissaoTela(13);
await this.SelecionaSeguradoras(null);
base.Loading(false);
}
public async void SelecionaSeguradora(Seguradora seguradora)
{
base.Loading(true);
this.SelectedSeguradora = seguradora;
this.Contatos = await this._servico.BuscarContatos(this.SelectedSeguradora.get_Id());
this.Enderecos = await this._servico.BuscarEnderecos(this.SelectedSeguradora.get_Id());
this.Config = await this._servico.BuscarConfig(this.SelectedSeguradora.get_Id());
this.ConfigFiltrados = new ObservableCollection<ConfigExtratoImport>(this.Config);
base.Loading(false);
}
private async Task SelecionaSeguradoras(Seguradora seguradora = null)
{
base.Loading(true);
List<Seguradora> seguradoras = await (new BaseServico()).BuscarSeguradorasAsync();
SeguradoraViewModel list = this;
List<Seguradora> seguradoras1 = seguradoras;
IOrderedEnumerable<Seguradora> ativo =
from x in seguradoras1
orderby x.get_Ativo() descending
select x;
list.Seguradoras = ativo.ThenBy<Seguradora, string>((Seguradora x) => x.get_Nome()).ToList<Seguradora>();
this.SeguradorasFiltradas = new ObservableCollection<Seguradora>(this.Seguradoras);
if (seguradora != null)
{
this.SelecionaSeguradora(this.SeguradorasFiltradas.FirstOrDefault<Seguradora>((Seguradora x) => x.get_Id() == seguradora.get_Id()));
}
else if (this.SeguradorasFiltradas.Count <= 0)
{
this.SelectedSeguradora = new Seguradora();
base.Alterar(false);
base.EnableMenu = false;
}
else
{
this.SelecionaSeguradora(this.SeguradorasFiltradas.First<Seguradora>());
}
Recursos.Seguradoras = seguradoras;
base.Loading(false);
}
public List<KeyValuePair<string, string>> Validate(List<SeguradoraEndereco> endereco)
{
List<KeyValuePair<string, string>> keyValuePairs = new List<KeyValuePair<string, string>>();
if (endereco != null)
{
endereco.ForEach((SeguradoraEndereco x) => keyValuePairs.AddRange(x.Validate()));
}
return keyValuePairs.Distinct<KeyValuePair<string, string>>().ToList<KeyValuePair<string, string>>();
}
public List<KeyValuePair<string, string>> Validate(List<SeguradoraContato> contatos)
{
List<SeguradoraContato> list;
List<KeyValuePair<string, string>> keyValuePairs = new List<KeyValuePair<string, string>>();
if (contatos != null)
{
contatos.ForEach((SeguradoraContato x) => keyValuePairs.AddRange(x.Validate()));
}
if (contatos != null)
{
list = (
from x in contatos
where x.get_TipoContato() == 1
select x).ToList<SeguradoraContato>();
}
else
{
list = null;
}
List<SeguradoraContato> seguradoraContatos = list;
if (seguradoraContatos == null)
{
return keyValuePairs;
}
if (seguradoraContatos.Count > 4)
{
keyValuePairs.Add(new KeyValuePair<string, string>("ASSISTÊNCIAS 24 HORAS", "NÃO É POSSÍVEL ADICIONAR MAIS QUE 4 NUMEROS DE ASSISTÊNCIA 24 HORAS"));
}
return keyValuePairs.Distinct<KeyValuePair<string, string>>().ToList<KeyValuePair<string, string>>();
}
private void WorkOnSelectedSeguradora(Seguradora value, bool registrar = true)
{
decimal? nullable;
bool contatos;
bool enderecos;
long? nullable1;
decimal? tolerancia;
this.CancelSeguradora = (value == null || value.get_Id() == 0 ? this.CancelSeguradora : (Seguradora)value.Clone());
this.CancelEnderecos = new ObservableCollection<SeguradoraEndereco>(this.Enderecos);
this.CancelContatos = new ObservableCollection<SeguradoraContato>(this.Contatos);
if (value == null || value.get_Id() == 0 || this.LastAccessId == value.get_Id() && this.LastAccessTela == 13)
{
return;
}
Seguradora selectedSeguradora = this.SelectedSeguradora;
if (selectedSeguradora != null)
{
contatos = selectedSeguradora.get_Contatos();
}
else
{
contatos = false;
}
if (contatos)
{
this.Contatos = new ObservableCollection<SeguradoraContato>(this.SelectedSeguradora.get_Contatos());
}
Seguradora seguradora = this.SelectedSeguradora;
if (seguradora != null)
{
enderecos = seguradora.get_Enderecos();
}
else
{
enderecos = false;
}
if (enderecos)
{
this.Enderecos = new ObservableCollection<SeguradoraEndereco>(this.SelectedSeguradora.get_Enderecos());
}
if (registrar)
{
base.RegistrarAcao(string.Concat("ACESSOU SEGURADORA \"", value.get_Nome(), "\""), value.get_Id(), new TipoTela?(13), string.Format("ID SEGURADORA: {0}", value.get_Id()));
}
this.LastAccessId = value.get_Id();
this.LastAccessTela = 13;
Seguradora selectedSeguradora1 = this.SelectedSeguradora;
if (selectedSeguradora1 != null)
{
nullable1 = new long?(selectedSeguradora1.get_Id());
}
else
{
nullable1 = null;
}
long? nullable2 = nullable1;
long id = value.get_Id();
if (nullable2.GetValueOrDefault() != id | !nullable2.HasValue)
{
this.SelectedSeguradora = this.SeguradorasFiltradas.FirstOrDefault<Seguradora>((Seguradora x) => x.get_Id() == value.get_Id());
}
Seguradora seguradora1 = this.SelectedSeguradora;
if (seguradora1 != null)
{
tolerancia = seguradora1.get_Tolerancia();
}
else
{
nullable = null;
tolerancia = nullable;
}
nullable = tolerancia;
this.Tolerancia = nullable.GetValueOrDefault();
}
}
}
|