summaryrefslogtreecommitdiff
path: root/Codemerx/Gestor.Application/ViewModels/Seguros/PerfilEmpresaViewModel.cs
blob: 0c116abcb12ec9c27199b344888cd72173d2bcb3 (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
using Gestor.Application.Helpers;
using Gestor.Application.Servicos.Generic;
using Gestor.Application.Servicos.Seguros;
using Gestor.Application.ViewModels.Generic;
using Gestor.Model.Domain.Generic;
using Gestor.Model.Domain.Seguros;
using Gestor.Model.Helper;
using Gestor.Model.Resources;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Windows;

namespace Gestor.Application.ViewModels.Seguros
{
	public class PerfilEmpresaViewModel : BaseViewModel
	{
		private readonly PerfilEmpresaServico _servico;

		private readonly Controle _controle;

		private Visibility _ativaCampos = Visibility.Collapsed;

		private string _equipamentoSeguranca = "";

		private string _equipamentoIncendio = "";

		private bool _enableIncluirPerfil;

		private string _dataConstrucao;

		public long CancelPerfil;

		private PerfilEmpresa _selectedPerfilEmpresa = new PerfilEmpresa();

		public Visibility AtivaCampos
		{
			get
			{
				return this._ativaCampos;
			}
			set
			{
				this._ativaCampos = value;
				base.OnPropertyChanged("AtivaCampos");
			}
		}

		public string DataConstrucao
		{
			get
			{
				return this._dataConstrucao;
			}
			set
			{
				string str;
				if (value != null)
				{
					str = value.Replace(" ", "");
				}
				else
				{
					str = null;
				}
				this._dataConstrucao = str;
				base.OnPropertyChanged("DataConstrucao");
			}
		}

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

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

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

		public PerfilEmpresa SelectedPerfilEmpresa
		{
			get
			{
				return this._selectedPerfilEmpresa;
			}
			set
			{
				long? nullable;
				this._selectedPerfilEmpresa = value;
				if (value != null && value.get_Id() > (long)0)
				{
					this.CancelPerfil = value.get_Id();
				}
				if (value != null)
				{
					nullable = new long?(value.get_Id());
				}
				else
				{
					nullable = null;
				}
				base.VerificarEnables(nullable);
				base.OnPropertyChanged("SelectedPerfilEmpresa");
			}
		}

		public PerfilEmpresaViewModel(Controle controle)
		{
			this._servico = new PerfilEmpresaServico();
			this._controle = controle;
			this.Seleciona(controle.get_Id());
		}

		public void CancelarAlteracao()
		{
			base.Loading(true);
			base.Alterar(false);
			base.Loading(false);
		}

		public async Task Excluir()
		{
			if (this.SelectedPerfilEmpresa != null && this.SelectedPerfilEmpresa.get_Id() != 0)
			{
				if (await base.ShowMessage("DESEJA EXCLUIR?", "SIM", "NÃO", false))
				{
					base.Loading(true);
					if (await this._servico.Delete(this.SelectedPerfilEmpresa))
					{
						await this.SelecionaPerfil(this._controle.get_Id());
						base.Loading(false);
						this.EnableIncluirPerfil = true;
						this.AtivaCampos = Visibility.Collapsed;
						this.DataConstrucao = null;
						base.ToggleSnackBar("PERFIL EXCLUÍDO COM SUCESSO", true);
					}
					else
					{
						base.Loading(false);
					}
				}
			}
		}

		public void Incluir()
		{
			PerfilEmpresa perfilEmpresa = new PerfilEmpresa();
			perfilEmpresa.set_Controle(this._controle);
			perfilEmpresa.set_Cliente(this._controle.get_Cliente());
			perfilEmpresa.set_ExclusivoDeposito(new bool?(false));
			perfilEmpresa.set_DivisaTerrenoBaldio(new bool?(false));
			perfilEmpresa.set_PatrimonioHistorio(new bool?(false));
			perfilEmpresa.set_PossuiTelhado(new bool?(false));
			perfilEmpresa.set_CaixasEletronicos(new bool?(false));
			perfilEmpresa.set_Isopainel(new bool?(false));
			perfilEmpresa.set_ConstrucaoReforma(new bool?(false));
			this.SelectedPerfilEmpresa = perfilEmpresa;
			base.Alterar(true);
		}

		public async Task<List<KeyValuePair<string, string>>> Salvar()
		{
			List<KeyValuePair<string, string>> keyValuePairs;
			List<KeyValuePair<string, string>> keyValuePairs1 = this.SelectedPerfilEmpresa.Validate();
			keyValuePairs1 = this.ValidateCampos(keyValuePairs1);
			if (keyValuePairs1.Count <= 0)
			{
				if (this.DataConstrucao != null && this.DataConstrucao != "")
				{
					this.SelectedPerfilEmpresa.set_AnoConstrucao(new DateTime?(DateTime.Parse(string.Concat("01/01/", this.DataConstrucao))));
				}
				this.SelectedPerfilEmpresa = await this._servico.Save(this.SelectedPerfilEmpresa);
				if (this._servico.Sucesso)
				{
					await this.SelecionaPerfil(this._controle.get_Id());
					base.ToggleSnackBar("PERFIL SALVO COM SUCESSO", true);
					base.Alterar(false);
					keyValuePairs = null;
				}
				else
				{
					keyValuePairs = null;
				}
			}
			else
			{
				keyValuePairs = keyValuePairs1;
			}
			return keyValuePairs;
		}

		private async void Seleciona(long id)
		{
			base.Loading(true);
			await base.PermissaoTela(32);
			await this.SelecionaPerfil(id);
			base.Loading(false);
		}

		private async Task SelecionaPerfil(long id)
		{
			base.Loading(true);
			this.SelectedPerfilEmpresa = await this._servico.BuscarPerfis(id);
			if (this.SelectedPerfilEmpresa == null)
			{
				this.EnableIncluirPerfil = true;
				this.EquipamentoSeguranca = "";
				this.EquipamentoIncendio = "";
			}
			else
			{
				this.EquipamentoSeguranca = BindingEnumHelper.ConcatenarDescricoesEnum<Gestor.Model.Common.EquipamentoSeguranca>(this.SelectedPerfilEmpresa.get_EquipamentoSeguranca());
				this.EquipamentoIncendio = BindingEnumHelper.ConcatenarDescricoesEnum<Gestor.Model.Common.EquipamentoIncendio>(this.SelectedPerfilEmpresa.get_EquipamentoIncendio());
				if (this.SelectedPerfilEmpresa.get_AnoConstrucao().HasValue)
				{
					DateTime? anoConstrucao = this.SelectedPerfilEmpresa.get_AnoConstrucao();
					this.DataConstrucao = anoConstrucao.Value.Year.ToString();
				}
			}
			base.Loading(false);
		}

		private List<KeyValuePair<string, string>> ValidateCampos(List<KeyValuePair<string, string>> errorMessages)
		{
			int num;
			int num1;
			if (this.DataConstrucao == null)
			{
				ValidationHelper.AddValue<string, string>(errorMessages, "ANO DA CONSTRUÇÃO", Messages.get_DataInvalida(), true);
			}
			if (!int.TryParse(this.DataConstrucao, out num))
			{
				ValidationHelper.AddValue<string, string>(errorMessages, "ANO DA CONSTRUÇÃO", Messages.get_DataInvalida(), true);
			}
			if (this.DataConstrucao != null && int.TryParse(this.DataConstrucao, out num1) && (int.Parse(this.DataConstrucao) <= 1755 || int.Parse(this.DataConstrucao) >= 9999))
			{
				ValidationHelper.AddValue<string, string>(errorMessages, "ANO DA CONSTRUÇÃO", Messages.get_DataInvalida(), true);
			}
			return errorMessages;
		}
	}
}