summaryrefslogtreecommitdiff
path: root/Decompiler/Gestor.Application.ViewModels.Financeiro/CentroDeCustoViewmodel.cs
blob: 8f7427a945203d19816c46250af7b4ca25a96377 (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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using Gestor.Application.Servicos.Financeiro;
using Gestor.Application.Servicos.Generic;
using Gestor.Application.ViewModels.Generic;
using Gestor.Common.Validation;
using Gestor.Model.Common;
using Gestor.Model.Domain.Financeiro;
using Gestor.Model.Domain.Generic;

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 List<Centro> Centros { get; set; }

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

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

	public Centro SelectedCentro
	{
		get
		{
			return _selectedCentro;
		}
		set
		{
			_selectedCentro = value;
			if (((DomainBase)value).Id > 0)
			{
				_ultimoId = ((DomainBase)value).Id;
			}
			VerificarEnables(((DomainBase)value).Id);
			OnPropertyChanged("SelectedCentro");
		}
	}

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

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

	private async void Seleciona()
	{
		Loading(isLoading: true);
		await PermissaoTela((TipoTela)29);
		await SelecionaCentro();
		await SelecionaCentro(CentrosFiltrados.FirstOrDefault());
		Loading(isLoading: false);
	}

	private async Task SelecionaCentro(Centro centros)
	{
		if (centros == null)
		{
			Alterar(alterar: false);
			base.EnableMenu = false;
			base.EnableAlterar = false;
			return;
		}
		Loading(isLoading: true);
		List<Centro> source = await new BaseServico().BuscarCentroAsync();
		DomainBase.Copy<Centro, Centro>(CentrosFiltrados.First((Centro x) => ((DomainBase)x).Id == ((DomainBase)centros).Id), source.First((Centro x) => ((DomainBase)x).Id == ((DomainBase)centros).Id));
		SelectedCentro = CentrosFiltrados.First((Centro x) => ((DomainBase)x).Id == ((DomainBase)centros).Id);
		Loading(isLoading: false);
	}

	public void SelecionaCentroDeCusto(Centro centro)
	{
		SelectedCentro = CentrosFiltrados.First((Centro x) => ((DomainBase)x).Id == ((DomainBase)centro).Id);
	}

	public void Incluir()
	{
		//IL_001a: Unknown result type (might be due to invalid IL or missing references)
		//IL_001f: Unknown result type (might be due to invalid IL or missing references)
		//IL_002b: Expected O, but got Unknown
		Centro selectedCentro = SelectedCentro;
		_ultimoId = ((selectedCentro != null) ? ((DomainBase)selectedCentro).Id : 0);
		SelectedCentro = new Centro
		{
			Ativo = true
		};
		Alterar(alterar: true);
	}

	public async Task<List<KeyValuePair<string, string>>> Validate()
	{
		List<KeyValuePair<string, string>> errors = new List<KeyValuePair<string, string>>();
		bool valida = true;
		List<Centro> list = await new BaseServico().BuscarCentroAsync();
		if (list.Count > 0)
		{
			list.ForEach(delegate(Centro x)
			{
				if (((DomainBase)x).Id != ((DomainBase)SelectedCentro).Id && x.Descricao == SelectedCentro.Descricao && x.IdEmpresa == SelectedCentro.IdEmpresa)
				{
					valida = false;
				}
			});
		}
		if (!valida)
		{
			errors.Add(new KeyValuePair<string, string>("Descricao", "UM CENTRO COM ESSE NOME JÁ EXISTE."));
		}
		return errors;
	}

	public async Task<List<KeyValuePair<string, string>>> Salvar()
	{
		List<KeyValuePair<string, string>> errorMessages = SelectedCentro.Validate();
		List<KeyValuePair<string, string>> list = errorMessages;
		list.AddRange(await Validate());
		if (errorMessages.Count > 0)
		{
			return errorMessages;
		}
		SelectedCentro = await _centroServico.Save(SelectedCentro);
		if (!_centroServico.Sucesso)
		{
			return null;
		}
		await SelecionaCentro();
		await SelecionaCentro(CentrosFiltrados.First((Centro x) => ((DomainBase)x).Id == ((DomainBase)SelectedCentro).Id));
		Alterar(alterar: false);
		ToggleSnackBar("CENTRO DE CUSTO SALVO COM SUCESSO");
		return null;
	}

	public async void CancelarAlteracao()
	{
		Loading(isLoading: true);
		Alterar(alterar: false);
		if (((DomainBase)SelectedCentro).Id > 0)
		{
			await SelecionaCentro();
		}
		await SelecionaCentro(((IEnumerable<Centro>)CentrosFiltrados).FirstOrDefault((Func<Centro, bool>)((Centro x) => ((DomainBase)x).Id == _ultimoId)));
		Loading(isLoading: false);
	}

	private async Task SelecionaCentro()
	{
		Centros = (from x in await new BaseServico().BuscarCentroAsync()
			orderby x.Ativo descending, x.Descricao, x.Descricao
			select x).ToList();
		CentrosFiltrados = new ObservableCollection<Centro>(Centros);
	}

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

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