using Gestor.Application.Helpers; using Gestor.Application.Servicos.Ferramentas; using Gestor.Application.Servicos.Generic; using Gestor.Application.ViewModels.Generic; using Gestor.Model.Common; using Gestor.Model.Domain.Configuracoes; using Gestor.Model.Domain.Ferramentas; 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.Drawer { public class ImpostoViewModel : BaseViewModel { private bool _carregando; private bool _apelido; public int Tipo; private List _seguradoras; private List _ramos; private ObservableCollection _impostos = new ObservableCollection(); private Imposto _selectedImposto = new Imposto(); private Ramo _selectedRamo = new Ramo(); private Seguradora _selectedSeguradora = new Seguradora(); public bool Apelido { get { return this._apelido; } set { this._apelido = value; base.OnPropertyChanged("Apelido"); } } public bool? Ativo { get; set; } = new bool?(true); public bool Carregando { get { return this._carregando; } set { this._carregando = value; base.IsEnabled = !value; base.OnPropertyChanged("Carregando"); } } public ObservableCollection Impostos { get { return this._impostos; } set { this._impostos = value; base.OnPropertyChanged("Impostos"); } } public List Ramos { get { return this._ramos; } set { this._ramos = value; base.OnPropertyChanged("Ramos"); } } public List Seguradoras { get { return this._seguradoras; } set { this._seguradoras = value; base.OnPropertyChanged("Seguradoras"); } } public Imposto SelectedImposto { get { return this._selectedImposto; } set { long? nullable; this._selectedImposto = value; if (value != null) { nullable = new long?(value.get_Id()); } else { nullable = null; } base.VerificarEnables(nullable); base.OnPropertyChanged("SelectedImposto"); } } public Ramo SelectedRamo { get { return this._selectedRamo; } set { this._selectedRamo = value; base.OnPropertyChanged("SelectedRamo"); } } public Seguradora SelectedSeguradora { get { return this._selectedSeguradora; } set { this._selectedSeguradora = value; base.OnPropertyChanged("SelectedSeguradora"); } } private ImpostoServico Servico { get; } public ImpostoViewModel() { this.Servico = new ImpostoServico(); this.Apelido = Recursos.Configuracoes.Any((ConfiguracaoSistema x) => x.get_Configuracao() == 6); } public void AlterarImposto() { base.Alterar(true); } public async Task Cancelar() { long id; Imposto selectedImposto = this.SelectedImposto; if (selectedImposto != null) { id = selectedImposto.get_Id(); } else { id = (long)0; } long num = id; base.Alterar(false); await this.Carregar(num); } public async Task Carregar(int tipo, long id) { string str; this.Tipo = tipo; await base.PermissaoTela(56); List list = Recursos.Seguradoras.Where((Seguradora x) => { if (x.get_Ativo()) { return true; } if (tipo != 0) { return false; } return x.get_Id() == id; }).OrderBy((Seguradora x) => { if (!this.Apelido) { return x.get_Nome(); } return x.get_NomeSocial(); }).ToList(); Seguradora seguradora = new Seguradora(); seguradora.set_Nome("TODAS SEGURADORAS"); seguradora.set_NomeSocial("TODAS SEGURADORAS"); seguradora.set_Id((long)0); list.Insert(0, seguradora); this.Seguradoras = list; this.SelectedSeguradora = this.Seguradoras.First(); IEnumerable ramos = Recursos.Ramos.Where((Ramo x) => { if (x.get_Ativo()) { return true; } if (tipo != 1) { return false; } return x.get_Id() == id; }); List list1 = ( from x in ramos orderby x.get_Nome() select x).ToList(); Ramo ramo = new Ramo(); ramo.set_Nome("TODOS OS RAMOS"); ramo.set_Id((long)0); list1.Insert(0, ramo); this.Ramos = list1; this.SelectedRamo = this.Ramos.First(); str = (tipo == 1 ? string.Concat("O RAMO \"", this.Ramos.First((Ramo x) => x.get_Id() == id).get_Nome(), "\"") : string.Concat("A SEGURADORA \"", this.Seguradoras.First((Seguradora x) => x.get_Id() == id).get_Nome(), "\"")); string str1 = str; base.RegistrarAcao(string.Concat("ACESSOU IMPOSTOS D", str1), id, new TipoTela?(56), string.Format("ID: {0}", id)); List impostos = await this.Servico.Buscar(new bool?(true)); if (tipo == 1) { impostos = impostos.Where((Imposto x) => { if (x.get_Ramo() == null || x.get_Ramo().get_Id() != id) { return false; } return x.get_Seguradora() == null; }).ToList(); this.SelectedRamo = this.Ramos.Find((Ramo x) => x.get_Id() == id); } else { impostos = impostos.Where((Imposto x) => { if (x.get_Seguradora() == null || x.get_Seguradora().get_Id() != id) { return false; } return x.get_Ramo() == null; }).ToList(); this.SelectedSeguradora = this.Seguradoras.Find((Seguradora x) => x.get_Id() == id); } ImpostoViewModel observableCollection = this; List impostos1 = impostos; observableCollection.Impostos = new ObservableCollection( from x in impostos1 orderby x.get_Ativo() descending select x); this.SelectedImposto = this.Impostos.FirstOrDefault(); } public async Task Carregar(long id = 0L) { Imposto imposto; List list = await this.Servico.Buscar(this.Ativo); if (this.SelectedSeguradora.get_Id() == 0) { List impostos = list; list = ( from x in impostos where x.get_Seguradora() == null select x).ToList(); } if (this.SelectedSeguradora.get_Id() > (long)0) { list = list.Where((Imposto x) => { if (x.get_Seguradora() == null) { return false; } return x.get_Seguradora().get_Id() == this.SelectedSeguradora.get_Id(); }).ToList(); } if (this.SelectedRamo.get_Id() == 0) { List impostos1 = list; list = ( from x in impostos1 where x.get_Ramo() == null select x).ToList(); } if (this.SelectedRamo.get_Id() > (long)0) { list = list.Where((Imposto x) => { if (x.get_Ramo() == null) { return false; } return x.get_Ramo().get_Id() == this.SelectedRamo.get_Id(); }).ToList(); } this.Impostos = new ObservableCollection(list.Where((Imposto x) => { if (!this.Ativo.HasValue) { return true; } return x.get_Ativo() == this.Ativo.Value; }).ToList()); ImpostoViewModel impostoViewModel = this; imposto = (id == 0 ? this.Impostos.FirstOrDefault() : this.Impostos.FirstOrDefault((Imposto x) => x.get_Id() == id)); impostoViewModel.SelectedImposto = imposto; } public void Incluir() { Seguradora selectedSeguradora; Ramo selectedRamo; Imposto imposto = new Imposto(); if (this.SelectedSeguradora.get_Id() > (long)0) { selectedSeguradora = this.SelectedSeguradora; } else { selectedSeguradora = null; } imposto.set_Seguradora(selectedSeguradora); if (this.SelectedRamo.get_Id() > (long)0) { selectedRamo = this.SelectedRamo; } else { selectedRamo = null; } imposto.set_Ramo(selectedRamo); imposto.set_Ativo(true); this.SelectedImposto = imposto; base.Alterar(true); } public async Task>> Salvar() { List> keyValuePairs; string str; string str1; string str2; object obj; string str3; List> keyValuePairs1 = this.SelectedImposto.Validate(); keyValuePairs1.AddRange(this.Validate(this.SelectedImposto)); if (keyValuePairs1.Count <= 0) { str = (this.SelectedImposto.get_Id() == 0 ? "INCLUIU" : "ALTEROU"); str3 = str; Imposto imposto = await this.Servico.Salvar(this.SelectedImposto); if (this.Servico.Sucesso) { str1 = (imposto.get_Seguradora() == null ? "TODAS AS SEGURADORAS" : string.Concat(" SEGURADORA ", imposto.get_Seguradora().get_Nome())); string str4 = str1; str2 = (imposto.get_Ramo() == null ? "TODOS OS RAMOS" : string.Concat(" RAMO ", imposto.get_Ramo().get_Nome())); string str5 = str2; string str6 = string.Concat(" ", str4, " E ", str5); ImpostoViewModel impostoViewModel = this; string str7 = string.Concat(str3, " IMPOSTO PARA ", str6); long id = imposto.get_Id(); TipoTela? nullable = new TipoTela?(31); object[] ir = new object[] { imposto.get_Id(), str4, str5, null, null, null, null, null }; obj = (imposto.get_Ativo() ? "ATIVO" : "INATIVO"); ir[3] = obj; ir[4] = imposto.get_Ir(); ir[5] = imposto.get_Iss(); ir[6] = imposto.get_Outros(); ir[7] = imposto.get_Desconto(); impostoViewModel.RegistrarAcao(str7, id, nullable, string.Format("ID: {0}{1}{2}\nSTATUS: {3}\nIR: {4:p2}\nISS: {5:p2}\nOUTROS: {6:p2}\nDESCONTO: {7:p2}", ir)); base.Alterar(false); await this.Carregar(imposto.get_Id()); keyValuePairs = null; } else { keyValuePairs = new List>() { new KeyValuePair("GRAVAR", "HOUVE UM ERRO AO SALVAR O IMPOSTO SELECIONADO.") }; } } else { keyValuePairs = keyValuePairs1; } str3 = null; return keyValuePairs; } public List> Validate(Imposto imposto) { List> keyValuePairs = new List>(); if (imposto == null) { return keyValuePairs; } if (imposto.get_Ativo() && this.Impostos.Any((Imposto x) => { long? nullable; long? nullable1; long? nullable2; long? nullable3; long? nullable4; if (x.get_Id() != imposto.get_Id()) { Seguradora seguradora = x.get_Seguradora(); if (seguradora != null) { nullable1 = new long?(seguradora.get_Id()); } else { nullable = null; nullable1 = nullable; } long? nullable5 = nullable1; Seguradora seguradora1 = imposto.get_Seguradora(); if (seguradora1 != null) { nullable2 = new long?(seguradora1.get_Id()); } else { nullable = null; nullable2 = nullable; } long? nullable6 = nullable2; if (nullable5.GetValueOrDefault() == nullable6.GetValueOrDefault() & nullable5.HasValue == nullable6.HasValue) { Ramo ramo = x.get_Ramo(); if (ramo != null) { nullable3 = new long?(ramo.get_Id()); } else { nullable = null; nullable3 = nullable; } nullable6 = nullable3; Ramo ramo1 = imposto.get_Ramo(); if (ramo1 != null) { nullable4 = new long?(ramo1.get_Id()); } else { nullable = null; nullable4 = nullable; } nullable5 = nullable4; if (nullable6.GetValueOrDefault() == nullable5.GetValueOrDefault() & nullable6.HasValue == nullable5.HasValue) { return x.get_Ativo(); } } } return false; })) { keyValuePairs.Add(new KeyValuePair("IMPOSTO DUPLICADO", "NÃO É POSSIVEL INSERIR UM NOVO PARAMETRO DE IMPOSTO, POIS JÁ EXISTEM PARAMETROS PARA A MESMA SEGURADORA E RAMO")); } return keyValuePairs.Distinct>().ToList>(); } } }