summaryrefslogtreecommitdiff
path: root/Codemerx/Gestor.Application/ViewModels/Financeiro/PlanosViewModel.cs
blob: e2251f320b8e7ea7d4736e0f6fcb64092d29994d (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
using Gestor.Application.Servicos.Financeiro;
using Gestor.Application.Servicos.Generic;
using Gestor.Application.ViewModels.Generic;
using Gestor.Common.Validation;
using Gestor.Model.Domain.Financeiro;
using Gestor.Model.Domain.Generic;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

namespace Gestor.Application.ViewModels.Financeiro
{
	public class PlanosViewModel : BaseFinanceiroViewModel
	{
		private readonly PlanosServico _planosServico;

		private ObservableCollection<Gestor.Model.Domain.Financeiro.Plano> _plano = new ObservableCollection<Gestor.Model.Domain.Financeiro.Plano>();

		private ObservableCollection<Gestor.Model.Domain.Financeiro.Planos> _planosFiltrados = new ObservableCollection<Gestor.Model.Domain.Financeiro.Planos>();

		private bool _isExpanded;

		private Gestor.Model.Domain.Financeiro.Planos _selectedPlanos;

		private Gestor.Model.Domain.Financeiro.Plano _selectedPlano;

		private bool _ativo;

		private long _ultimoId;

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

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

		public ObservableCollection<Gestor.Model.Domain.Financeiro.Plano> Plano
		{
			get
			{
				return this._plano;
			}
			set
			{
				this._plano = value;
				base.OnPropertyChanged("Plano");
			}
		}

		public List<Gestor.Model.Domain.Financeiro.Planos> Planos
		{
			get;
			set;
		}

		public ObservableCollection<Gestor.Model.Domain.Financeiro.Planos> PlanosFiltrados
		{
			get
			{
				return this._planosFiltrados;
			}
			set
			{
				this._planosFiltrados = value;
				this.IsExpanded = (value != null ? value.Count > 0 : false);
				base.OnPropertyChanged("PlanosFiltrados");
			}
		}

		public Gestor.Model.Domain.Financeiro.Plano SelectedPlano
		{
			get
			{
				return this._selectedPlano;
			}
			set
			{
				this._selectedPlano = value;
				base.OnPropertyChanged("SelectedPlano");
			}
		}

		public Gestor.Model.Domain.Financeiro.Planos SelectedPlanos
		{
			get
			{
				return this._selectedPlanos;
			}
			set
			{
				bool plano;
				Gestor.Model.Domain.Financeiro.Plano plano1;
				bool id;
				long? nullable;
				this._selectedPlanos = value;
				Gestor.Model.Domain.Financeiro.Planos plano2 = value;
				if (plano2 != null)
				{
					plano = plano2.get_Plano();
				}
				else
				{
					plano = false;
				}
				if (plano)
				{
					plano1 = this.Plano.FirstOrDefault<Gestor.Model.Domain.Financeiro.Plano>((Gestor.Model.Domain.Financeiro.Plano x) => x.get_Id() == value.get_Plano().get_Id());
				}
				else
				{
					plano1 = null;
				}
				this.SelectedPlano = plano1;
				Gestor.Model.Domain.Financeiro.Planos plano3 = value;
				if (plano3 != null)
				{
					id = plano3.get_Id() > (long)0;
				}
				else
				{
					id = false;
				}
				if (id)
				{
					this._ultimoId = value.get_Id();
				}
				Gestor.Model.Domain.Financeiro.Planos plano4 = value;
				if (plano4 != null)
				{
					nullable = new long?(plano4.get_Id());
				}
				else
				{
					nullable = null;
				}
				base.VerificarEnables(nullable);
				base.OnPropertyChanged("SelectedPlanos");
			}
		}

		public PlanosViewModel()
		{
			this._planosServico = new PlanosServico();
			base.EnableMenu = true;
			this.Seleciona();
		}

		public async void CancelarAlteracao()
		{
			base.Loading(true);
			base.Alterar(false);
			if (this.SelectedPlanos.get_Id() > (long)0)
			{
				await this.SelecionaPlanos();
			}
			await this.SelecionaPlanos(this.PlanosFiltrados.FirstOrDefault<Gestor.Model.Domain.Financeiro.Planos>((Gestor.Model.Domain.Financeiro.Planos x) => x.get_Id() == this._ultimoId));
			base.Loading(false);
		}

		public async Task<ObservableCollection<Gestor.Model.Domain.Financeiro.Planos>> Filtrar(string value)
		{
			ObservableCollection<Gestor.Model.Domain.Financeiro.Planos> observableCollection = await Task.Run<ObservableCollection<Gestor.Model.Domain.Financeiro.Planos>>(() => this.FiltrarPlanos(value));
			return observableCollection;
		}

		public ObservableCollection<Gestor.Model.Domain.Financeiro.Planos> FiltrarPlanos(string filter)
		{
			this.PlanosFiltrados = (string.IsNullOrWhiteSpace(filter) ? new ObservableCollection<Gestor.Model.Domain.Financeiro.Planos>(this.Planos) : new ObservableCollection<Gestor.Model.Domain.Financeiro.Planos>(
				from x in this.Planos
				where ValidationHelper.RemoveDiacritics(x.get_Descricao().Trim()).ToUpper().Contains(ValidationHelper.RemoveDiacritics(filter))
				orderby this.Ativo descending, x.get_Descricao()
				select x));
			return this.PlanosFiltrados;
		}

		public void Incluir()
		{
			Gestor.Model.Domain.Financeiro.Planos plano = new Gestor.Model.Domain.Financeiro.Planos();
			plano.set_Plano(this.SelectedPlano);
			this.SelectedPlanos = plano;
			base.Alterar(true);
		}

		public async Task<List<KeyValuePair<string, string>>> Salvar()
		{
			List<KeyValuePair<string, string>> keyValuePairs;
			this.SelectedPlanos.set_Plano(this.SelectedPlano);
			List<KeyValuePair<string, string>> keyValuePairs1 = this.SelectedPlanos.Validate();
			List<KeyValuePair<string, string>> keyValuePairs2 = keyValuePairs1;
			keyValuePairs2.AddRange(await this.Validate());
			keyValuePairs2 = null;
			if (keyValuePairs1.Count <= 0)
			{
				this.SelectedPlanos.set_Plano(this.SelectedPlano);
				this.SelectedPlanos = await this._planosServico.Save(this.SelectedPlanos);
				if (this._planosServico.Sucesso)
				{
					await this.SelecionaPlanos();
					await this.SelecionaPlanos(this.PlanosFiltrados.First<Gestor.Model.Domain.Financeiro.Planos>((Gestor.Model.Domain.Financeiro.Planos x) => x.get_Id() == this.SelectedPlanos.get_Id()));
					base.Alterar(false);
					base.ToggleSnackBar("SUBNÍVEL SALVO COM SUCESSO", true);
					keyValuePairs = null;
				}
				else
				{
					keyValuePairs = null;
				}
			}
			else
			{
				keyValuePairs = keyValuePairs1;
			}
			keyValuePairs1 = null;
			return keyValuePairs;
		}

		private async void Seleciona()
		{
			base.Loading(true);
			await base.PermissaoTela(28);
			await this.SelecionaPlanos();
			await this.SelecionaPlanos(this.PlanosFiltrados.FirstOrDefault<Gestor.Model.Domain.Financeiro.Planos>());
			base.Loading(false);
		}

		private async Task SelecionaPlanos()
		{
			List<Gestor.Model.Domain.Financeiro.Planos> planos = await (new BaseServico()).BuscarPlanosAsync();
			PlanosViewModel list = this;
			List<Gestor.Model.Domain.Financeiro.Planos> planos1 = planos;
			IOrderedEnumerable<Gestor.Model.Domain.Financeiro.Planos> ativo = 
				from x in planos1
				orderby x.get_Ativo() descending
				select x;
			IOrderedEnumerable<Gestor.Model.Domain.Financeiro.Planos> planos2 = ativo.ThenBy<Gestor.Model.Domain.Financeiro.Planos, string>((Gestor.Model.Domain.Financeiro.Planos x) => {
				Gestor.Model.Domain.Financeiro.Plano plano = x.get_Plano();
				if (plano != null)
				{
					return plano.get_Descricao();
				}
				return null;
			});
			list.Planos = planos2.ThenBy<Gestor.Model.Domain.Financeiro.Planos, string>((Gestor.Model.Domain.Financeiro.Planos x) => x.get_Descricao()).ToList<Gestor.Model.Domain.Financeiro.Planos>();
			this.PlanosFiltrados = new ObservableCollection<Gestor.Model.Domain.Financeiro.Planos>(this.Planos);
		}

		public async Task SelecionaPlanos(Gestor.Model.Domain.Financeiro.Planos planos)
		{
			if (planos != null)
			{
				base.Loading(true);
				ObservableCollection<Gestor.Model.Domain.Financeiro.Planos> observableCollection = await this._planosServico.BuscarPlanos();
				DomainBase.Copy<Gestor.Model.Domain.Financeiro.Planos, Gestor.Model.Domain.Financeiro.Planos>(this.PlanosFiltrados.First<Gestor.Model.Domain.Financeiro.Planos>((Gestor.Model.Domain.Financeiro.Planos x) => x.get_Id() == planos.get_Id()), observableCollection.First<Gestor.Model.Domain.Financeiro.Planos>((Gestor.Model.Domain.Financeiro.Planos x) => x.get_Id() == planos.get_Id()));
				if (this.Plano == null)
				{
					List<Gestor.Model.Domain.Financeiro.Plano> planos1 = await (new BaseServico()).BuscarPlanoAsync();
					PlanosViewModel planosViewModel = this;
					List<Gestor.Model.Domain.Financeiro.Plano> planos2 = planos1;
					IOrderedEnumerable<Gestor.Model.Domain.Financeiro.Plano> ativo = 
						from x in planos2
						orderby x.get_Ativo() descending
						select x;
					planosViewModel.Plano = new ObservableCollection<Gestor.Model.Domain.Financeiro.Plano>(ativo.ThenBy<Gestor.Model.Domain.Financeiro.Plano, string>((Gestor.Model.Domain.Financeiro.Plano x) => x.get_Descricao()).ToList<Gestor.Model.Domain.Financeiro.Plano>());
				}
				this.SelectedPlanos = this.PlanosFiltrados.First<Gestor.Model.Domain.Financeiro.Planos>((Gestor.Model.Domain.Financeiro.Planos x) => x.get_Id() == planos.get_Id());
				base.Loading(false);
			}
			else
			{
				base.Alterar(false);
				base.EnableMenu = false;
				base.EnableAlterar = false;
			}
		}

		public async Task<List<KeyValuePair<string, string>>> Validate()
		{
			List<KeyValuePair<string, string>> keyValuePairs = new List<KeyValuePair<string, string>>();
			bool flag = true;
			List<Gestor.Model.Domain.Financeiro.Planos> planos = await (new BaseServico()).BuscarPlanosAsync();
			if (planos != null)
			{
				planos.ForEach((Gestor.Model.Domain.Financeiro.Planos x) => {
					if (x.get_Id() == this.SelectedPlanos.get_Id())
					{
						return;
					}
					if (x.get_Descricao() == this.SelectedPlanos.get_Descricao())
					{
						flag = false;
					}
				});
			}
			else
			{
			}
			if (!flag)
			{
				keyValuePairs.Add(new KeyValuePair<string, string>("Descricao", "UM SUBNÍVEL COM ESSE NOME JÁ EXISTE."));
			}
			List<KeyValuePair<string, string>> keyValuePairs1 = keyValuePairs;
			keyValuePairs = null;
			return keyValuePairs1;
		}
	}
}