summaryrefslogtreecommitdiff
path: root/Gestor.Application/ViewModels/Seguros/Itens/ConsorcioViewModel.cs
blob: 0470058f246dff1a560ab5ae61dbc19f9a8887b3 (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
using Gestor.Application.Actions;
using Gestor.Application.Componentes;
using Gestor.Application.Servicos.Generic;
using Gestor.Application.Servicos.Seguros.Itens;
using Gestor.Application.ViewModels;
using Gestor.Application.ViewModels.Generic;
using Gestor.Application.ViewModels.Seguros;
using Gestor.Model.Common;
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.Seguros.Itens
{
	public class ConsorcioViewModel : ItemViewModel, IDisposable
	{
		private readonly ItemServico _itemServico;

		private readonly ConsorcioServico _servico;

		private bool _visibilityTitulares;

		private bool _allowAlterarExcluir = true;

		private object _cobertura;

		private object _sinistro;

		private string _codigoItem;

		private Item _selectedItem = new Item();

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

		public object Cobertura
		{
			get
			{
				return this._cobertura;
			}
			set
			{
				this._cobertura = value;
				base.OnPropertyChanged("Cobertura");
			}
		}

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

		public bool Invoke
		{
			get;
			set;
		}

		private Item ItemSelecionado
		{
			get;
			set;
		}

		private Gestor.Model.Domain.Seguros.Ramo Ramo
		{
			get;
		}

		public Item SelectedItem
		{
			get
			{
				return this._selectedItem;
			}
			set
			{
				long? nullable;
				this._selectedItem = value;
				if (value != null)
				{
					nullable = new long?(value.get_Id());
				}
				else
				{
					nullable = null;
				}
				base.VerificarEnables(nullable);
				base.OnPropertyChanged("SelectedItem");
			}
		}

		public object Sinistro
		{
			get
			{
				return this._sinistro;
			}
			set
			{
				this._sinistro = value;
				base.OnPropertyChanged("Sinistro");
			}
		}

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

		public ConsorcioViewModel(bool substituir = false, ManutencaoItem manutencao = null, Item itemSelecionado = null, Gestor.Model.Domain.Seguros.Ramo ramo = null, bool endossoRenovacao = false)
		{
			this.Invoke = itemSelecionado == null;
			this.ItemSelecionado = itemSelecionado ?? ConsultaViewModel.ItemSelecionado;
			this.Ramo = ramo ?? ConsultaViewModel.DocumentoSelecionado.get_Controle().get_Ramo();
			if (this.Invoke)
			{
				this.ItemSelecionado = this.ItemSelecionado ?? new Item();
				this.ItemSelecionado.set_Documento(ConsultaViewModel.DocumentoSelecionado);
			}
			this._itemServico = new ItemServico();
			this._servico = new ConsorcioServico();
			this.Seleciona(this.ItemSelecionado);
			if (substituir)
			{
				this.Substituir(this.ItemSelecionado);
			}
			if (manutencao != null)
			{
				this.Manter(this.ItemSelecionado, manutencao);
			}
			this.AllowAlterarExcluir = !endossoRenovacao;
			this.CarregaCobertura();
			this.VisibilityTitulares = this.Ramo.get_Id() == (long)31;
		}

		public async void CancelarAlteracao()
		{
			base.Loading(true);
			await this.SelecionaItem(this.ItemSelecionado, false);
			await this.CoberturaViewModel.CancelarAlteracao();
			base.Alterar(false);
			base.Loading(false);
		}

		public void CarregaCobertura()
		{
			this.CoberturaViewModel.Item = this.ItemSelecionado;
			this.CoberturaViewModel.Ramo = this.Ramo;
			this.Cobertura = new CoberturaView(this.CoberturaViewModel);
		}

		public void Descarregar()
		{
			this.Cobertura = null;
			this.Sinistro = null;
		}

		public void Dispose()
		{
			Gestor.Application.Actions.Actions.UpdateItem = (Action<Item>)Delegate.Remove(Gestor.Application.Actions.Actions.UpdateItem, new Action<Item>(this.Seleciona));
			Gestor.Application.Actions.Actions.SubstituirItem = (Action<Item>)Delegate.Remove(Gestor.Application.Actions.Actions.SubstituirItem, new Action<Item>(this.Substituir));
			Gestor.Application.Actions.Actions.ManterItem = (Action<Item, ManutencaoItem>)Delegate.Remove(Gestor.Application.Actions.Actions.ManterItem, new Action<Item, ManutencaoItem>(this.Manter));
		}

		public async Task<bool> Excluir()
		{
			bool flag;
			if (this.SelectedItem == null || this.SelectedItem.get_Id() == 0)
			{
				flag = false;
			}
			else if (await base.ShowMessage("DESEJA EXCLUIR?", "SIM", "NÃO", false))
			{
				base.Loading(true);
				bool flag1 = await this._servico.Delete(this.SelectedItem);
				base.Loading(false);
				if (flag1)
				{
					string str = string.Concat("EXCLUIU ITEM DE DESCRIÇÃO \"", this.SelectedItem.get_Descricao(), "\"");
					long id = this.SelectedItem.get_Id();
					TipoTela? nullable = new TipoTela?(3);
					object[] objArray = new object[] { this.SelectedItem.get_Id(), this.SelectedItem.get_Documento().get_Id(), this.SelectedItem.get_Documento().get_Controle().get_Ramo().get_Nome(), this.SelectedItem.get_Documento().get_Controle().get_Cliente().get_Nome(), this.SelectedItem.get_Documento().get_Controle().get_Cliente().get_Id() };
					base.RegistrarAcao(str, id, nullable, string.Format("ID ITEM: {0}\nID DOCUMENTO: {1}\nRAMO: {2}\nCLIENTE: {3}\nID CLIENTE: {4}", objArray));
					base.ToggleSnackBar(string.Concat(this.Ramo.get_Nome(), " EXCLUÍDO COM SUCESSO"), true);
					flag = true;
				}
				else
				{
					await base.ShowMessage("NÃO POSSÍVEL EXCLUIR O ITEM", "OK", "", false);
					flag = false;
				}
			}
			else
			{
				flag = false;
			}
			return flag;
		}

		public async Task Incluir(bool substituir = false)
		{
			int? ordem;
			List<Gestor.Model.Domain.Seguros.Cobertura> coberturas;
			long? nullable;
			coberturas = (this.CoberturaViewModel.Coberturas == null ? this.CoberturaViewModel.CarregaCoberturaPadrao().ToList<Gestor.Model.Domain.Seguros.Cobertura>() : this.CoberturaViewModel.Coberturas.ToList<Gestor.Model.Domain.Seguros.Cobertura>());
			List<Gestor.Model.Domain.Seguros.Cobertura> coberturas1 = coberturas;
			Item item = new Item();
			item.set_Documento(this.ItemSelecionado.get_Documento());
			Item item1 = item;
			ordem = (substituir ? base.ItemSubstituido.get_Ordem() : new int?(await base.QuantidadeDeItens(this.ItemSelecionado.get_Documento().get_Controle().get_Id())));
			item1.set_Ordem(ordem);
			item.set_Consorcio(new Consorcio());
			Item item2 = item;
			if (substituir)
			{
				nullable = new long?(base.ItemSubstituido.get_Id());
			}
			else
			{
				nullable = null;
			}
			item2.set_Substituicao(nullable);
			Item item3 = item;
			item1 = null;
			item = null;
			base.Titulares = new ObservableCollection<TitularesVida>();
			ordem = item3.get_Ordem();
			if (ordem.GetValueOrDefault() == 2)
			{
				if (!await base.ShowMessage("ESTA APÓLICE SERÁ UMA FROTA, DESEJA CONTINUAR?", "SIM", "NÃO", false))
				{
					coberturas1 = null;
					item3 = null;
					return;
				}
			}
			bool valueOrDefault = substituir;
			if (valueOrDefault)
			{
				valueOrDefault = await base.ShowMessage("DESEJA COPIAR OS DADOS DO ITEM?", "SIM", "NÃO", false);
			}
			if (!valueOrDefault)
			{
				this.SelectedItem = item3;
				this.SelectedItem.get_Consorcio().set_Item(item3);
			}
			else
			{
				this.SelectedItem.set_Id((long)0);
				this.SelectedItem.set_Documento(this.ItemSelecionado.get_Documento());
			}
			ordem = item3.get_Ordem();
			int num = 1;
			valueOrDefault = ordem.GetValueOrDefault() > num & ordem.HasValue;
			if (valueOrDefault)
			{
				valueOrDefault = await base.ShowMessage("DESEJA REPLICAR AS COBERTURAS DO ITEM SELECIONADO?", "SIM", "NÃO", false);
			}
			if (!valueOrDefault)
			{
				this.CoberturaViewModel.CarregaPadrao();
			}
			else
			{
				this.CoberturaViewModel.Coberturas = new ObservableCollection<Gestor.Model.Domain.Seguros.Cobertura>();
				coberturas1.ForEach((Gestor.Model.Domain.Seguros.Cobertura x) => {
					x.set_Item(this.SelectedItem);
					this.CoberturaViewModel.Coberturas.Add(x);
				});
			}
			if (substituir)
			{
				this.CoberturaViewModel.Coberturas = new ObservableCollection<Gestor.Model.Domain.Seguros.Cobertura>();
				coberturas1.ForEach((Gestor.Model.Domain.Seguros.Cobertura x) => {
					x.set_Item(this.SelectedItem);
					this.CoberturaViewModel.Coberturas.Add(x);
				});
			}
			base.Alterar(true);
			coberturas1 = null;
			item3 = null;
		}

		public void Manter(Item item, ManutencaoItem manutencao)
		{
			if (manutencao.get_Tipo() == 1)
			{
				base.ItemSubstituido = item;
			}
			this.Manter(manutencao);
			base.Mantido = true;
		}

		public void Manter(ManutencaoItem manutencao)
		{
			long? nullable;
			object list;
			ObservableCollection<Gestor.Model.Domain.Seguros.Cobertura> coberturas = this.CoberturaViewModel.Coberturas;
			if (coberturas != null)
			{
				list = coberturas.ToList<Gestor.Model.Domain.Seguros.Cobertura>();
			}
			else
			{
				list = null;
			}
			this.SelectedItem.set_Id((long)0);
			this.SelectedItem.get_Consorcio().set_Id((long)0);
			Item selectedItem = this.SelectedItem;
			Item itemSubstituido = base.ItemSubstituido;
			if (itemSubstituido != null)
			{
				nullable = new long?(itemSubstituido.get_Id());
			}
			else
			{
				nullable = null;
			}
			selectedItem.set_Substituicao(nullable);
			this.SelectedItem.set_Documento(this.ItemSelecionado.get_Documento());
			if (manutencao.get_Tipo() == null)
			{
				this.SelectedItem.set_Sinistrado(false);
				this.SelectedItem.set_Status("");
				this.SelectedItem.set_Sinistro(null);
				this.SelectedItem.set_Sinistros(null);
			}
			this.CoberturaViewModel.CarregaPadrao();
			this.CoberturaViewModel.Coberturas = new ObservableCollection<Gestor.Model.Domain.Seguros.Cobertura>();
			if (list != null)
			{
				((List<Gestor.Model.Domain.Seguros.Cobertura>)list).ForEach((Gestor.Model.Domain.Seguros.Cobertura x) => {
					x.set_Item(this.SelectedItem);
					this.CoberturaViewModel.Coberturas.Add(x);
				});
			}
			else
			{
			}
			base.Alterar(true);
		}

		public async Task<List<KeyValuePair<string, string>>> Salvar()
		{
			List<KeyValuePair<string, string>> keyValuePairs;
			string str;
			string str1;
			string str2;
			string str3;
			this.SelectedItem.set_Descricao(this.SelectedItem.get_Consorcio().get_BensConsorcio());
			List<KeyValuePair<string, string>> keyValuePairs1 = this.SelectedItem.Validate();
			if (keyValuePairs1.Count <= 0)
			{
				Item selectedItem = this.SelectedItem;
				selectedItem.set_Coberturas(await this.CoberturaViewModel.LimpaCoberturas());
				selectedItem = null;
				if (this.SelectedItem.get_Documento().get_Ordem() <= 0)
				{
					str = "";
				}
				else if (base.Mantido)
				{
					str = string.Format("ITEM ALTERADO NO ENDOSSO ORDEM {0}", this.SelectedItem.get_Documento().get_Ordem());
				}
				else
				{
					str = (base.ItemSubstituido != null ? string.Format("SUBSTITUIÇÃO DO ITEM {0} NO ENDOSSO ORDEM {1}", base.ItemSubstituido.get_Ordem(), this.SelectedItem.get_Documento().get_Ordem()) : string.Format("ITEM INCLUÍDO NO ENDOSSO ORDEM {0}", this.SelectedItem.get_Documento().get_Ordem()));
				}
				string str4 = str;
				if (this.SelectedItem.get_Id() == 0)
				{
					this.SelectedItem.set_Status(str4);
				}
				str1 = (this.SelectedItem.get_Id() == 0 ? "INCLUIU" : "ALTEROU");
				str3 = str1;
				this.SelectedItem = await this._itemServico.Save(this.SelectedItem);
				if (this._itemServico.Sucesso)
				{
					if (base.ItemSubstituido != null)
					{
						str2 = (base.Mantido ? string.Format("ITEM ALTERADO NO ENDOSSO ORDEM {0}", this.SelectedItem.get_Documento().get_Ordem()) : string.Format("ITEM SUBSTITUIDO NO ENDOSSO ORDEM {0}", this.SelectedItem.get_Documento().get_Ordem()));
						string str5 = str2;
						await this._itemServico.Substituir(base.ItemSubstituido, this.SelectedItem.get_Id(), str5);
					}
					base.Mantido = false;
					await this.SelecionaItem(this.SelectedItem, false);
					this.ItemSelecionado = this.SelectedItem;
					if (this.Invoke)
					{
						ConsultaViewModel.ItemSelecionado = this.SelectedItem;
					}
					base.ToggleSnackBar(string.Concat(this.Ramo.get_Nome(), " SALVO COM SUCESSO"), true);
					string str6 = string.Concat(str3, " ITEM DE DESCRIÇÃO \"", this.SelectedItem.get_Descricao(), "\"");
					long id = this.SelectedItem.get_Id();
					TipoTela? nullable = new TipoTela?(3);
					object[] objArray = new object[] { this.SelectedItem.get_Id(), this.SelectedItem.get_Documento().get_Id(), this.SelectedItem.get_Documento().get_Controle().get_Ramo().get_Nome(), this.SelectedItem.get_Documento().get_Controle().get_Cliente().get_Nome(), this.SelectedItem.get_Documento().get_Controle().get_Cliente().get_Id() };
					base.RegistrarAcao(str6, id, nullable, string.Format("ID ITEM: {0}\nID DOCUMENTO: {1}\nRAMO: {2}\nCLIENTE: {3}\nID CLIENTE: {4}", objArray));
					base.Alterar(false);
					keyValuePairs = null;
				}
				else
				{
					keyValuePairs = null;
				}
			}
			else
			{
				keyValuePairs = keyValuePairs1;
			}
			str3 = null;
			return keyValuePairs;
		}

		private async void Seleciona(Item item)
		{
			base.Loading(true);
			await base.PermissaoTela(3);
			await this.SelecionaItem(item, true);
			base.Loading(false);
		}

		public async Task SelecionaItem(Item item, bool registrar = true)
		{
			if (item == null || this.LastAccessId != item.get_Id() || this.LastAccessTela != 9)
			{
				base.Mantido = false;
				this.CoberturaViewModel.Ramo = this.Ramo;
				if (this.CoberturaViewModel.CoberturasPadrao == null)
				{
					await this.CoberturaViewModel.SelectionaCoberturas();
				}
				if (item == null || item.get_Id() == 0)
				{
					this.SelectedItem = null;
					this.CoberturaViewModel.Coberturas = null;
					base.IsVisible = Visibility.Visible;
					base.Titulares = new ObservableCollection<TitularesVida>();
				}
				else
				{
					base.Loading(true);
					item = await this._itemServico.BuscarItemPorIdAsync(item.get_Id());
					Item item1 = item;
					Consorcio consorcio = await this._itemServico.BuscaConsorcio(item.get_Id());
					Item item2 = item1;
					Consorcio consorcio1 = consorcio;
					if (consorcio1 == null)
					{
						consorcio1 = new Consorcio();
						consorcio1.set_Item(item);
					}
					item2.set_Consorcio(consorcio1);
					item1 = null;
					if (this.VisibilityTitulares)
					{
						await base.CarregarTitulares(item.get_Id());
					}
					this.SelectedItem = item;
					this.SelectedItem.Initialize();
					this.Initialized = true;
					this.CoberturaViewModel.Item = item;
					await this.CoberturaViewModel.CancelarAlteracao();
					if (registrar)
					{
						string str = string.Concat("ACESSOU ITEM DE DESCRIÇÃO \"", this.SelectedItem.get_Descricao(), "\"");
						long id = this.SelectedItem.get_Id();
						TipoTela? nullable = new TipoTela?(3);
						object[] objArray = new object[] { this.SelectedItem.get_Id(), this.SelectedItem.get_Documento().get_Id(), this.SelectedItem.get_Documento().get_Controle().get_Ramo().get_Nome(), this.SelectedItem.get_Documento().get_Controle().get_Cliente().get_Nome(), this.SelectedItem.get_Documento().get_Controle().get_Cliente().get_Id() };
						base.RegistrarAcao(str, id, nullable, string.Format("ID ITEM: {0}\nID DOCUMENTO: {1}\nRAMO: {2}\nCLIENTE: {3}\nID CLIENTE: {4}", objArray));
					}
					this.LastAccessId = this.SelectedItem.get_Id();
					this.LastAccessTela = 3;
					this.CodigoItem = this.SelectedItem.get_Id().ToString();
					base.Loading(false);
				}
			}
		}

		public async void Substituir(Item item)
		{
			base.ItemSubstituido = item;
			await this.Incluir(true);
		}
	}
}