summaryrefslogtreecommitdiff
path: root/Codemerx/Gestor.Application/ViewModels/Financeiro/CentroDeCustoViewmodel.cs
blob: ef575413e66279f9575dfdbcc956634b2039e18e (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
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 CentroDeCustoViewmodel : BaseFinanceiroViewModel
	{
		private readonly CentroServico _centroServico;

		private ObservableCollection<Centro> _centrosFiltrados = new ObservableCollection<Centro>();

		private bool _isExpanded;

		private Centro _selectedCentro;

		private bool _ativo;

		private long _ultimoId;

		public Centro CancelCentro;

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

		public List<Centro> Centros
		{
			get;
			set;
		}

		public ObservableCollection<Centro> CentrosFiltrados
		{
			get
			{
				return this._centrosFiltrados;
			}
			set
			{
				this._centrosFiltrados = value;
				this.IsExpanded = (value != null ? value.Count > 0 : false);
				base.OnPropertyChanged("CentrosFiltrados");
			}
		}

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

		public Centro SelectedCentro
		{
			get
			{
				return this._selectedCentro;
			}
			set
			{
				this._selectedCentro = value;
				if (value.get_Id() > (long)0)
				{
					this._ultimoId = value.get_Id();
				}
				base.VerificarEnables(new long?(value.get_Id()));
				base.OnPropertyChanged("SelectedCentro");
			}
		}

		public CentroDeCustoViewmodel()
		{
			this._centroServico = new CentroServico();
			base.EnableMenu = true;
			this.Seleciona();
		}

		public async void CancelarAlteracao()
		{
			base.Loading(true);
			base.Alterar(false);
			if (this.SelectedCentro.get_Id() > (long)0)
			{
				await this.SelecionaCentro();
			}
			await this.SelecionaCentro(this.CentrosFiltrados.FirstOrDefault<Centro>((Centro x) => x.get_Id() == this._ultimoId));
			base.Loading(false);
		}

		public async Task<List<Centro>> Filtrar(string value)
		{
			List<Centro> centros = await Task.Run<List<Centro>>(() => this.FiltrarCentro(value));
			return centros;
		}

		public List<Centro> FiltrarCentro(string filter)
		{
			this.CentrosFiltrados = (string.IsNullOrWhiteSpace(filter) ? new ObservableCollection<Centro>(this.Centros) : new ObservableCollection<Centro>(
				from x in this.Centros
				where ValidationHelper.RemoveDiacritics(x.get_Descricao().Trim()).ToUpper().Contains(ValidationHelper.RemoveDiacritics(filter))
				orderby this.Ativo descending, x.get_Descricao()
				select x));
			return this.CentrosFiltrados.ToList<Centro>();
		}

		public void Incluir()
		{
			long id;
			Centro selectedCentro = this.SelectedCentro;
			if (selectedCentro != null)
			{
				id = selectedCentro.get_Id();
			}
			else
			{
				id = (long)0;
			}
			this._ultimoId = id;
			Centro centro = new Centro();
			centro.set_Ativo(true);
			this.SelectedCentro = centro;
			base.Alterar(true);
		}

		public async Task<List<KeyValuePair<string, string>>> Salvar()
		{
			List<KeyValuePair<string, string>> keyValuePairs;
			List<KeyValuePair<string, string>> keyValuePairs1 = this.SelectedCentro.Validate();
			List<KeyValuePair<string, string>> keyValuePairs2 = keyValuePairs1;
			keyValuePairs2.AddRange(await this.Validate());
			keyValuePairs2 = null;
			if (keyValuePairs1.Count <= 0)
			{
				this.SelectedCentro = await this._centroServico.Save(this.SelectedCentro);
				if (this._centroServico.Sucesso)
				{
					await this.SelecionaCentro();
					await this.SelecionaCentro(this.CentrosFiltrados.First<Centro>((Centro x) => x.get_Id() == this.SelectedCentro.get_Id()));
					base.Alterar(false);
					base.ToggleSnackBar("CENTRO DE CUSTO 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(29);
			await this.SelecionaCentro();
			await this.SelecionaCentro(this.CentrosFiltrados.FirstOrDefault<Centro>());
			base.Loading(false);
		}

		private async Task SelecionaCentro(Centro centros)
		{
			if (centros != null)
			{
				base.Loading(true);
				List<Centro> centros1 = await (new BaseServico()).BuscarCentroAsync();
				DomainBase.Copy<Centro, Centro>(this.CentrosFiltrados.First<Centro>((Centro x) => x.get_Id() == centros.get_Id()), centros1.First<Centro>((Centro x) => x.get_Id() == centros.get_Id()));
				this.SelectedCentro = this.CentrosFiltrados.First<Centro>((Centro x) => x.get_Id() == centros.get_Id());
				base.Loading(false);
			}
			else
			{
				base.Alterar(false);
				base.EnableMenu = false;
				base.EnableAlterar = false;
			}
		}

		private async Task SelecionaCentro()
		{
			List<Centro> centros = await (new BaseServico()).BuscarCentroAsync();
			CentroDeCustoViewmodel list = this;
			List<Centro> centros1 = centros;
			IOrderedEnumerable<Centro> ativo = 
				from x in centros1
				orderby x.get_Ativo() descending
				select x;
			IOrderedEnumerable<Centro> centros2 = ativo.ThenBy<Centro, string>((Centro x) => x.get_Descricao());
			list.Centros = centros2.ThenBy<Centro, string>((Centro x) => x.get_Descricao()).ToList<Centro>();
			this.CentrosFiltrados = new ObservableCollection<Centro>(this.Centros);
		}

		public void SelecionaCentroDeCusto(Centro centro)
		{
			this.SelectedCentro = this.CentrosFiltrados.First<Centro>((Centro x) => x.get_Id() == centro.get_Id());
		}

		public async Task<List<KeyValuePair<string, string>>> Validate()
		{
			List<KeyValuePair<string, string>> keyValuePairs = new List<KeyValuePair<string, string>>();
			bool flag = true;
			List<Centro> centros = await (new BaseServico()).BuscarCentroAsync();
			if (centros.Count > 0)
			{
				centros.ForEach((Centro x) => {
					if (x.get_Id() == this.SelectedCentro.get_Id())
					{
						return;
					}
					if (x.get_Descricao() == this.SelectedCentro.get_Descricao() && x.get_IdEmpresa() == this.SelectedCentro.get_IdEmpresa())
					{
						flag = false;
					}
				});
			}
			if (!flag)
			{
				keyValuePairs.Add(new KeyValuePair<string, string>("Descricao", "UM CENTRO COM ESSE NOME JÁ EXISTE."));
			}
			List<KeyValuePair<string, string>> keyValuePairs1 = keyValuePairs;
			keyValuePairs = null;
			return keyValuePairs1;
		}
	}
}