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 _centrosFiltrados = new ObservableCollection(); 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 Centros { get; set; } public ObservableCollection 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 x) => x.get_Id() == this._ultimoId)); base.Loading(false); } public async Task> Filtrar(string value) { List centros = await Task.Run>(() => this.FiltrarCentro(value)); return centros; } public List FiltrarCentro(string filter) { this.CentrosFiltrados = (string.IsNullOrWhiteSpace(filter) ? new ObservableCollection(this.Centros) : new ObservableCollection( 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(); } 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>> Salvar() { List> keyValuePairs; List> keyValuePairs1 = this.SelectedCentro.Validate(); List> 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 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()); base.Loading(false); } private async Task SelecionaCentro(Centro centros) { if (centros != null) { base.Loading(true); List centros1 = await (new BaseServico()).BuscarCentroAsync(); DomainBase.Copy(this.CentrosFiltrados.First((Centro x) => x.get_Id() == centros.get_Id()), centros1.First((Centro x) => x.get_Id() == centros.get_Id())); this.SelectedCentro = this.CentrosFiltrados.First((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 centros = await (new BaseServico()).BuscarCentroAsync(); CentroDeCustoViewmodel list = this; List centros1 = centros; IOrderedEnumerable ativo = from x in centros1 orderby x.get_Ativo() descending select x; IOrderedEnumerable centros2 = ativo.ThenBy((Centro x) => x.get_Descricao()); list.Centros = centros2.ThenBy((Centro x) => x.get_Descricao()).ToList(); this.CentrosFiltrados = new ObservableCollection(this.Centros); } public void SelecionaCentroDeCusto(Centro centro) { this.SelectedCentro = this.CentrosFiltrados.First((Centro x) => x.get_Id() == centro.get_Id()); } public async Task>> Validate() { List> keyValuePairs = new List>(); bool flag = true; List 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("Descricao", "UM CENTRO COM ESSE NOME JÁ EXISTE.")); } List> keyValuePairs1 = keyValuePairs; keyValuePairs = null; return keyValuePairs1; } } }