using Gestor.Application.Helpers; using Gestor.Application.Servicos.Ferramentas; using Gestor.Application.Servicos.Generic; using Gestor.Application.ViewModels.Generic; using Gestor.Common.Validation; using Gestor.Model.Common; using Gestor.Model.Domain.Generic; using Gestor.Model.Domain.Seguros; 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.Ferramentas { public class EstipulanteViewModel : BaseSegurosViewModel { private readonly EstipulanteServico _servico; public Estipulante CancelEstipulante; private Estipulante _selectedEstipulante = new Estipulante(); private ObservableCollection _estipulantesFiltrados = new ObservableCollection(); private bool _isExpanded; public List Estipulantes { get; set; } public ObservableCollection EstipulantesFiltrados { get { return this._estipulantesFiltrados; } set { this._estipulantesFiltrados = value; this.IsExpanded = (value != null ? value.Count > 0 : false); base.OnPropertyChanged("EstipulantesFiltrados"); } } public bool IsExpanded { get { return this._isExpanded; } set { this._isExpanded = value; base.OnPropertyChanged("IsExpanded"); } } public Estipulante SelectedEstipulante { get { return this._selectedEstipulante; } set { long? nullable; this._selectedEstipulante = value; this.WorkOnSelectedEstipulante(value, true); if (value != null) { nullable = new long?(value.get_Id()); } else { nullable = null; } base.VerificarEnables(nullable); base.OnPropertyChanged("SelectedEstipulante"); if (value == null) { return; } this.SelectedEstipulante.Initialize(); } } public EstipulanteViewModel() { this._servico = new EstipulanteServico(); base.EnableMenu = true; this.Seleciona(); } public void CancelarAlteracao() { if (this.CancelEstipulante == null || !this.Estipulantes.Any((Estipulante x) => x.get_Id() == this.CancelEstipulante.get_Id())) { this.Incluir(); } else { DomainBase.Copy(this.Estipulantes.First((Estipulante x) => x.get_Id() == this.CancelEstipulante.get_Id()), this.CancelEstipulante); if (this.EstipulantesFiltrados.Count <= 0 || !this.EstipulantesFiltrados.Any((Estipulante x) => x.get_Id() == this.CancelEstipulante.get_Id())) { this.EstipulantesFiltrados.Add(this.CancelEstipulante); } else { DomainBase.Copy(this.EstipulantesFiltrados.First((Estipulante x) => x.get_Id() == this.CancelEstipulante.get_Id()), this.CancelEstipulante); } this.EstipulantesFiltrados = new ObservableCollection(this.EstipulantesFiltrados); this.SelectedEstipulante = this.EstipulantesFiltrados.First((Estipulante x) => x.get_Id() == this.CancelEstipulante.get_Id()); } base.Alterar(false); } public async void Excluir() { Estipulante estipulante; if (this.SelectedEstipulante != null && this.SelectedEstipulante.get_Id() != 0) { base.Loading(true); List documentos = await (new BaseServico()).BuscarDocumentosPorEstipulante(this.SelectedEstipulante.get_Id()); base.Loading(false); if (documentos.Count > 0) { string str = "ESTIPULANTE NÃO PODE SER EXCLUÍDO POIS ESTÁ VINCULADO AOS SEGUINTES DOCUMENTOS:"; foreach (Documento documento in documentos) { if (!string.IsNullOrWhiteSpace(documento.get_Apolice())) { object[] newLine = new object[] { Environment.NewLine, documentos.IndexOf(documento) + 1, documento.get_Proposta(), documento.get_Apolice() }; str = string.Concat(str, string.Format("{0}DOCUMENTO {1} (NÚMERO DA PROPOSTA: {2}, NÚMERO DA APÓLICE: {3})", newLine)); } else { str = string.Concat(str, string.Format("{0}DOCUMENTO {1} (NÚMERO DA PROPOSTA: {2})", Environment.NewLine, documentos.IndexOf(documento) + 1, documento.get_Proposta())); } } await base.ShowMessage(str, "OK", "", false); } else if (await base.ShowMessage("DESEJA EXCLUIR?", "SIM", "NÃO", false)) { base.Loading(true); if (await this._servico.Delete(this.SelectedEstipulante)) { base.RegistrarAcao(string.Concat("EXCLUIU ESTIPULANTE \"", this.SelectedEstipulante.get_Nome(), "\""), this.SelectedEstipulante.get_Id(), new TipoTela?(9), string.Format("ESTIPULANTE \"{0}\", ID: {1}", this.SelectedEstipulante.get_Nome(), this.SelectedEstipulante.get_Id())); int num = this.EstipulantesFiltrados.IndexOf(this.SelectedEstipulante); this.Estipulantes.Remove(this.SelectedEstipulante); this.EstipulantesFiltrados.Remove(this.SelectedEstipulante); this.EstipulantesFiltrados = new ObservableCollection(this.EstipulantesFiltrados); if (this.EstipulantesFiltrados.Count <= 0) { this.SelectedEstipulante = new Estipulante(); base.Alterar(false); base.EnableMenu = false; } else { EstipulanteViewModel estipulanteViewModel = this; estipulante = (num < this.EstipulantesFiltrados.Count ? this.EstipulantesFiltrados.ElementAt(num) : this.EstipulantesFiltrados.Last()); estipulanteViewModel.SelectedEstipulante = estipulante; } Recursos.Estipulantes = await (new BaseServico()).BuscarEstipulantesAsync(); base.Loading(false); base.ToggleSnackBar("ESTIPULANTE EXCLUÍDO COM SUCESSO", true); } else { base.Loading(false); } } } } internal async Task> Filtrar(string value) { List estipulantes = await Task.Run>(() => this.FiltrarEstipulante(value)); return estipulantes; } public List FiltrarEstipulante(string filter) { this.EstipulantesFiltrados = (string.IsNullOrWhiteSpace(filter) ? new ObservableCollection(this.Estipulantes) : new ObservableCollection( from x in this.Estipulantes where ValidationHelper.RemoveDiacritics(x.get_Nome().Trim()).ToUpper().Contains(ValidationHelper.RemoveDiacritics(filter)) orderby x.get_Ativo() descending, x.get_Nome() select x)); this.SelectedEstipulante = this.EstipulantesFiltrados.FirstOrDefault(); return this.EstipulantesFiltrados.ToList(); } public void Incluir() { Estipulante estipulante = new Estipulante(); estipulante.set_IdEmpresa(Recursos.Usuario.get_IdEmpresa()); estipulante.set_Ativo(true); this.SelectedEstipulante = estipulante; base.Alterar(true); } public async Task>> Salvar() { List> keyValuePairs; string str; string str1; List> keyValuePairs1 = this.SelectedEstipulante.Validate(); List> keyValuePairs2 = keyValuePairs1; keyValuePairs2.AddRange(await this.Validate()); keyValuePairs2 = null; if (keyValuePairs1.Count <= 0) { str = (this.SelectedEstipulante.get_Id() == 0 ? "INCLUIU" : "ALTEROU"); str1 = str; Estipulante estipulante = await this._servico.Save(this.SelectedEstipulante); if (this._servico.Sucesso) { base.RegistrarAcao(string.Concat(str1, " ESTIPULANTE \"", estipulante.get_Nome(), "\""), estipulante.get_Id(), new TipoTela?(9), string.Format("ESTIPULANTE \"{0}\", ID: {1}", estipulante.get_Nome(), estipulante.get_Id())); if (!this.Estipulantes.Any((Estipulante x) => x.get_Id() == estipulante.get_Id())) { this.Estipulantes.Add(estipulante); } else { DomainBase.Copy(this.Estipulantes.First((Estipulante x) => x.get_Id() == estipulante.get_Id()), estipulante); } if (!this.EstipulantesFiltrados.Any((Estipulante x) => x.get_Id() == estipulante.get_Id())) { this.EstipulantesFiltrados.Add(estipulante); } else { DomainBase.Copy(this.EstipulantesFiltrados.First((Estipulante x) => x.get_Id() == estipulante.get_Id()), estipulante); } this.EstipulantesFiltrados = new ObservableCollection(this.EstipulantesFiltrados); Recursos.Estipulantes = this.Estipulantes; this.WorkOnSelectedEstipulante(estipulante, false); base.Alterar(false); this.SelectedEstipulante.Initialize(); base.ToggleSnackBar("ESTIPULANTE SALVO COM SUCESSO", true); keyValuePairs = null; } else { keyValuePairs = null; } } else { keyValuePairs = keyValuePairs1; } keyValuePairs1 = null; str1 = null; return keyValuePairs; } private async void Seleciona() { base.Loading(true); await base.PermissaoTela(9); await this.SelecionaEstipulantes(); base.Loading(false); } public async void SelecionaEstipulante(Estipulante estipulante) { Estipulante estipulante1 = await this._servico.BuscarEstipulantePorId(estipulante.get_Id()); DomainBase.Copy(this.EstipulantesFiltrados.First((Estipulante x) => x.get_Id() == estipulante.get_Id()), estipulante1); this.SelectedEstipulante = this.EstipulantesFiltrados.First((Estipulante x) => x.get_Id() == estipulante.get_Id()); } private async Task SelecionaEstipulantes() { base.Loading(true); List estipulantes = await (new BaseServico()).BuscarEstipulantesAsync(); EstipulanteViewModel list = this; List estipulantes1 = estipulantes; IEnumerable estipulantes2 = estipulantes1.Where((Estipulante x) => { if (Recursos.Usuario.get_IdEmpresa() == (long)1) { return true; } return Recursos.Usuario.get_IdEmpresa() == x.get_IdEmpresa(); }); IOrderedEnumerable ativo = from x in estipulantes2 orderby x.get_Ativo() descending select x; list.Estipulantes = ativo.ThenBy((Estipulante x) => x.get_Nome()).ToList(); this.EstipulantesFiltrados = new ObservableCollection(this.Estipulantes); if (this.EstipulantesFiltrados.Count <= 0) { this.SelectedEstipulante = new Estipulante(); base.Alterar(false); base.EnableMenu = false; } else { this.SelecionaEstipulante(this.EstipulantesFiltrados.First()); } Recursos.Estipulantes = estipulantes; base.Loading(false); } public async Task>> Validate() { List estipulantes; List> keyValuePairs = new List>(); bool flag = !string.IsNullOrEmpty(this.SelectedEstipulante.get_Nome()); if (!ValidationHelper.ValidateDocument(this.SelectedEstipulante.get_Documento())) { estipulantes = new List(); } else { estipulantes = await (new BaseServico()).BuscarEstipulante(this.SelectedEstipulante.get_Documento()); } List estipulantes1 = estipulantes; string nome = ""; if (estipulantes1.Count > 0) { estipulantes1.ForEach((Estipulante x) => { if (x.get_Id() == this.SelectedEstipulante.get_Id()) { return; } if (!string.IsNullOrEmpty(this.SelectedEstipulante.get_Documento()) && x.get_Documento() == this.SelectedEstipulante.get_Documento()) { flag = false; nome = x.get_Nome(); } }); } if (!flag) { keyValuePairs.Add(new KeyValuePair("Documento", string.Concat("O DOCUMENTO ESTÁ CADASTRADO PARA O ESTIPULANTE ", nome, "."))); } List> keyValuePairs1 = keyValuePairs; keyValuePairs = null; return keyValuePairs1; } private void WorkOnSelectedEstipulante(Estipulante value, bool registrar = true) { long? nullable; this.CancelEstipulante = (value == null || value.get_Id() == 0 ? this.CancelEstipulante : (Estipulante)value.Clone()); if (value == null || value.get_Id() == 0 || this.LastAccessId == value.get_Id() && this.LastAccessTela == 9) { return; } if (registrar) { base.RegistrarAcao(string.Concat("ACESSOU ESTIPULANTE \"", value.get_Nome(), "\""), value.get_Id(), new TipoTela?(9), string.Format("ID ESTIPULANTE: {0}", value.get_Id())); } this.LastAccessId = value.get_Id(); this.LastAccessTela = 9; Estipulante selectedEstipulante = this.SelectedEstipulante; if (selectedEstipulante != null) { nullable = new long?(selectedEstipulante.get_Id()); } else { nullable = null; } long? nullable1 = nullable; long id = value.get_Id(); if (nullable1.GetValueOrDefault() != id | !nullable1.HasValue) { this.SelectedEstipulante = this.EstipulantesFiltrados.FirstOrDefault((Estipulante x) => x.get_Id() == value.get_Id()); } } } }