From 1f4e14b2e973ee7de337fd4866d9a5ceff5cb6d1 Mon Sep 17 00:00:00 2001 From: Lucas Faria Mendes Date: Mon, 30 Mar 2026 10:38:18 -0300 Subject: chore: location --- .../ViewModels/Financeiro/PlanosViewModel.cs | 330 +++++++++++++++++++++ 1 file changed, 330 insertions(+) create mode 100644 Codemerx/Gestor.Application/ViewModels/Financeiro/PlanosViewModel.cs (limited to 'Codemerx/Gestor.Application/ViewModels/Financeiro/PlanosViewModel.cs') diff --git a/Codemerx/Gestor.Application/ViewModels/Financeiro/PlanosViewModel.cs b/Codemerx/Gestor.Application/ViewModels/Financeiro/PlanosViewModel.cs new file mode 100644 index 0000000..e2251f3 --- /dev/null +++ b/Codemerx/Gestor.Application/ViewModels/Financeiro/PlanosViewModel.cs @@ -0,0 +1,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 _plano = new ObservableCollection(); + + private ObservableCollection _planosFiltrados = new ObservableCollection(); + + 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 Plano + { + get + { + return this._plano; + } + set + { + this._plano = value; + base.OnPropertyChanged("Plano"); + } + } + + public List Planos + { + get; + set; + } + + public ObservableCollection 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 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 x) => x.get_Id() == this._ultimoId)); + base.Loading(false); + } + + public async Task> Filtrar(string value) + { + ObservableCollection observableCollection = await Task.Run>(() => this.FiltrarPlanos(value)); + return observableCollection; + } + + public ObservableCollection FiltrarPlanos(string filter) + { + this.PlanosFiltrados = (string.IsNullOrWhiteSpace(filter) ? new ObservableCollection(this.Planos) : new ObservableCollection( + 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>> Salvar() + { + List> keyValuePairs; + this.SelectedPlanos.set_Plano(this.SelectedPlano); + List> keyValuePairs1 = this.SelectedPlanos.Validate(); + List> 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 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()); + base.Loading(false); + } + + private async Task SelecionaPlanos() + { + List planos = await (new BaseServico()).BuscarPlanosAsync(); + PlanosViewModel list = this; + List planos1 = planos; + IOrderedEnumerable ativo = + from x in planos1 + orderby x.get_Ativo() descending + select x; + IOrderedEnumerable planos2 = ativo.ThenBy((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 x) => x.get_Descricao()).ToList(); + this.PlanosFiltrados = new ObservableCollection(this.Planos); + } + + public async Task SelecionaPlanos(Gestor.Model.Domain.Financeiro.Planos planos) + { + if (planos != null) + { + base.Loading(true); + ObservableCollection observableCollection = await this._planosServico.BuscarPlanos(); + DomainBase.Copy(this.PlanosFiltrados.First((Gestor.Model.Domain.Financeiro.Planos x) => x.get_Id() == planos.get_Id()), observableCollection.First((Gestor.Model.Domain.Financeiro.Planos x) => x.get_Id() == planos.get_Id())); + if (this.Plano == null) + { + List planos1 = await (new BaseServico()).BuscarPlanoAsync(); + PlanosViewModel planosViewModel = this; + List planos2 = planos1; + IOrderedEnumerable ativo = + from x in planos2 + orderby x.get_Ativo() descending + select x; + planosViewModel.Plano = new ObservableCollection(ativo.ThenBy((Gestor.Model.Domain.Financeiro.Plano x) => x.get_Descricao()).ToList()); + } + this.SelectedPlanos = this.PlanosFiltrados.First((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>> Validate() + { + List> keyValuePairs = new List>(); + bool flag = true; + List 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("Descricao", "UM SUBNÍVEL COM ESSE NOME JÁ EXISTE.")); + } + List> keyValuePairs1 = keyValuePairs; + keyValuePairs = null; + return keyValuePairs1; + } + } +} \ No newline at end of file -- cgit v1.2.3