From 674ca83ba9243a9e95a7568c797668dab6aee26a Mon Sep 17 00:00:00 2001 From: Lucas Faria Mendes Date: Mon, 30 Mar 2026 10:35:25 -0300 Subject: feat: upload files --- .../ViewModels/Ferramentas/NotaFiscalViewModel.cs | 482 +++++++++++++++++++++ 1 file changed, 482 insertions(+) create mode 100644 Gestor.Application/ViewModels/Ferramentas/NotaFiscalViewModel.cs (limited to 'Gestor.Application/ViewModels/Ferramentas/NotaFiscalViewModel.cs') diff --git a/Gestor.Application/ViewModels/Ferramentas/NotaFiscalViewModel.cs b/Gestor.Application/ViewModels/Ferramentas/NotaFiscalViewModel.cs new file mode 100644 index 0000000..61ee285 --- /dev/null +++ b/Gestor.Application/ViewModels/Ferramentas/NotaFiscalViewModel.cs @@ -0,0 +1,482 @@ +using Gestor.Application.Helpers; +using Gestor.Application.Servicos; +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.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.Globalization; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Threading.Tasks; + +namespace Gestor.Application.ViewModels.Ferramentas +{ + public class NotaFiscalViewModel : BaseSegurosViewModel + { + private readonly NotaFiscalServico _servico; + + private readonly ServicoExtrato _servicoExtrato; + + private bool _apelido; + + private List _estipulantes; + + private List _seguradoras; + + private ObservableCollection _notasFiscaisFiltrados = new ObservableCollection(); + + private bool _isExpanded; + + private string _numExtrato; + + private NotaFiscal _selectedNotaFiscal; + + public NotaFiscal CancelNotaFiscal; + + public bool Apelido + { + get + { + return this._apelido; + } + set + { + this._apelido = value; + base.OnPropertyChanged("Apelido"); + } + } + + public List Estipulantes + { + get + { + return this._estipulantes; + } + set + { + this._estipulantes = value; + base.OnPropertyChanged("Estipulantes"); + } + } + + public bool IsExpanded + { + get + { + return this._isExpanded; + } + set + { + this._isExpanded = value; + base.OnPropertyChanged("IsExpanded"); + } + } + + public List NotasFiscais + { + get; + set; + } + + public ObservableCollection NotasFiscaisFiltrados + { + get + { + return this._notasFiscaisFiltrados; + } + set + { + this._notasFiscaisFiltrados = value; + this.IsExpanded = (value != null ? value.Count > 0 : false); + base.OnPropertyChanged("NotasFiscaisFiltrados"); + } + } + + public string NumExtrato + { + get + { + return this._numExtrato; + } + set + { + this._numExtrato = value; + base.OnPropertyChanged("NumExtrato"); + } + } + + public List Seguradoras + { + get + { + return this._seguradoras; + } + set + { + this._seguradoras = value; + base.OnPropertyChanged("Seguradoras"); + } + } + + public NotaFiscal SelectedNotaFiscal + { + get + { + return this._selectedNotaFiscal; + } + set + { + long? nullable; + this._selectedNotaFiscal = value; + this.WorkOnSelectedNotaFiscal(value, true); + if (value != null) + { + nullable = new long?(value.get_Id()); + } + else + { + nullable = null; + } + base.VerificarEnables(nullable); + base.OnPropertyChanged("SelectedNotaFiscal"); + } + } + + public NotaFiscalViewModel() + { + this._servicoExtrato = new ServicoExtrato(); + this._servico = new NotaFiscalServico(); + this.Seguradoras = ( + from x in Recursos.Seguradoras + where x.get_Ativo() + select x).ToList(); + this.Estipulantes = ( + from e in Recursos.Estipulantes + where e.get_Ativo() + select e).ToList(); + this.Apelido = Recursos.Configuracoes.Any((ConfiguracaoSistema x) => x.get_Configuracao() == 6); + base.EnableMenu = true; + this.Seleciona(); + } + + public async Task BuscarImposto() + { + decimal iss; + List impostos = await (new ImpostoServico()).Buscar(new bool?(true)); + Imposto imposto = impostos.FirstOrDefault((Imposto x) => { + if (x.get_Seguradora() == null || x.get_Seguradora().get_Id() != this.SelectedNotaFiscal.get_Seguradora().get_Id()) + { + return false; + } + return x.get_Ramo() == null; + }); + if (imposto == null) + { + List impostos1 = impostos; + imposto = impostos1.FirstOrDefault((Imposto x) => { + if (x.get_Seguradora() != null) + { + return false; + } + return x.get_Ramo() == null; + }); + } + if (imposto != null) + { + iss = imposto.get_Iss(); + } + else + { + iss = decimal.Zero; + } + return iss; + } + + public void CancelarAlteracao() + { + if (this.CancelNotaFiscal == null || !this.NotasFiscais.Any((NotaFiscal x) => x.get_Id() == this.CancelNotaFiscal.get_Id())) + { + this.Incluir(); + } + else + { + DomainBase.Copy(this.NotasFiscais.First((NotaFiscal x) => x.get_Id() == this.CancelNotaFiscal.get_Id()), this.CancelNotaFiscal); + if (this.NotasFiscaisFiltrados.Count <= 0 || !this.NotasFiscaisFiltrados.Any((NotaFiscal x) => x.get_Id() == this.CancelNotaFiscal.get_Id())) + { + this.NotasFiscaisFiltrados.Add(this.CancelNotaFiscal); + } + else + { + DomainBase.Copy(this.NotasFiscaisFiltrados.First((NotaFiscal x) => x.get_Id() == this.CancelNotaFiscal.get_Id()), this.CancelNotaFiscal); + } + this.NotasFiscaisFiltrados = new ObservableCollection(this.NotasFiscaisFiltrados); + this.SelectedNotaFiscal = this.NotasFiscaisFiltrados.First((NotaFiscal x) => x.get_Id() == this.CancelNotaFiscal.get_Id()); + } + base.Alterar(false); + } + + public async void Excluir() + { + object obj; + if (this.SelectedNotaFiscal != null && this.SelectedNotaFiscal.get_Id() != 0) + { + if (await base.ShowMessage(string.Concat("DESEJA REALMENTE EXCLUIR A NOTA FISCAL DA ", this.SelectedNotaFiscal.get_Seguradora().get_Nome(), "?"), "SIM", "NÃO", false)) + { + base.Loading(true); + if (await this._servico.Delete(this.SelectedNotaFiscal)) + { + NotaFiscalViewModel notaFiscalViewModel = this; + string str = string.Format("EXCLUIU NOTA FISCAL DE ID \"{0}\"", this.SelectedNotaFiscal.get_Id()); + long id = this.SelectedNotaFiscal.get_Id(); + TipoTela? nullable = new TipoTela?(55); + object[] nome = new object[] { this.SelectedNotaFiscal.get_Seguradora().get_Nome(), null, null, null, null }; + obj = (!this.SelectedNotaFiscal.get_Data().HasValue ? "-" : string.Format("{0:d}", this.SelectedNotaFiscal.get_Data())); + nome[1] = obj; + nome[2] = this.SelectedNotaFiscal.get_ValorBruto(); + nome[3] = this.SelectedNotaFiscal.get_Iss(); + nome[4] = this.SelectedNotaFiscal.get_ValorLiquido(); + notaFiscalViewModel.RegistrarAcao(str, id, nullable, string.Format("SEGURADORA: {0}\nDATA: {1}\nBRUTO: {2:c}\nISS: {3:c}\nLÍQUIDO: {4:c}", nome)); + await this.SelecionaNotaFiscal(); + base.Loading(false); + base.ToggleSnackBar("RECIBO EXCLUÍDO COM SUCESSO", true); + } + else + { + base.Loading(false); + } + } + } + } + + public async Task> Filtrar(string value) + { + List notaFiscals = await Task.Run>(() => this.FiltrarNotaFiscal(value)); + return notaFiscals; + } + + public List FiltrarNotaFiscal(string filter) + { + this.NotasFiscaisFiltrados = (string.IsNullOrWhiteSpace(filter) ? new ObservableCollection(this.NotasFiscais) : new ObservableCollection(this.NotasFiscais.Where((NotaFiscal x) => { + if (ValidationHelper.RemoveDiacritics(x.get_Seguradora().get_Nome().Trim()).ToUpper().Contains(ValidationHelper.RemoveDiacritics(filter)) || ValidationHelper.RemoveDiacritics(x.get_ValorBruto().ToString(CultureInfo.InvariantCulture).Trim()).Contains(ValidationHelper.RemoveDiacritics(filter))) + { + return true; + } + return ValidationHelper.RemoveDiacritics(x.get_Data().ToString().Trim()).Contains(ValidationHelper.RemoveDiacritics(filter)); + }).OrderBy((NotaFiscal x) => x.get_Seguradora().get_Nome()))); + return this.NotasFiscaisFiltrados.ToList(); + } + + public void Incluir() + { + NotaFiscal notaFiscal = new NotaFiscal(); + notaFiscal.set_Seguradora(new Seguradora()); + notaFiscal.set_Estipulante(new Estipulante()); + notaFiscal.set_Iss(decimal.Zero); + notaFiscal.set_ValorLiquido(decimal.Zero); + notaFiscal.set_ValorBruto(decimal.Zero); + notaFiscal.set_Extrato(""); + this.SelectedNotaFiscal = notaFiscal; + base.Alterar(true); + } + + public async Task>> Salvar() + { + List> keyValuePairs; + string str; + long? nullable; + bool valueOrDefault; + object obj; + string str1; + List> keyValuePairs1 = this.SelectedNotaFiscal.Validate(); + if (keyValuePairs1.Count <= 0) + { + str = (this.SelectedNotaFiscal.get_Id() == 0 ? "INCLUIU" : "ALTEROU"); + str1 = str; + NotaFiscal selectedNotaFiscal = this.SelectedNotaFiscal; + if (selectedNotaFiscal != null) + { + Estipulante estipulante = selectedNotaFiscal.get_Estipulante(); + if (estipulante != null) + { + nullable = new long?(estipulante.get_Id()); + } + else + { + nullable = null; + } + long? nullable1 = nullable; + long num = (long)0; + valueOrDefault = nullable1.GetValueOrDefault() <= num & nullable1.HasValue; + } + else + { + valueOrDefault = false; + } + if (valueOrDefault) + { + this.SelectedNotaFiscal.set_Estipulante(null); + } + NotaFiscal notaFiscal = await this._servico.Save(this.SelectedNotaFiscal); + if (this._servico.Sucesso) + { + NotaFiscalViewModel notaFiscalViewModel = this; + string str2 = string.Format("{0} NOTA FISCAL DE ID \"{1}\"", str1, notaFiscal.get_Id()); + long id = notaFiscal.get_Id(); + TipoTela? nullable2 = new TipoTela?(55); + object[] nome = new object[] { notaFiscal.get_Seguradora().get_Nome(), null, null, null, null }; + obj = (!notaFiscal.get_Data().HasValue ? "-" : string.Format("{0:d}", notaFiscal.get_Data())); + nome[1] = obj; + nome[2] = notaFiscal.get_ValorBruto(); + nome[3] = notaFiscal.get_Iss(); + nome[4] = notaFiscal.get_ValorLiquido(); + notaFiscalViewModel.RegistrarAcao(str2, id, nullable2, string.Format("SEGURADORA: {0}\nDATA: {1}\nBRUTO: {2:c}\nISS: {3:c}\nLÍQUIDO: {4:c}", nome)); + if (!this.NotasFiscais.Any((NotaFiscal x) => x.get_Id() == notaFiscal.get_Id())) + { + this.NotasFiscais.Add(notaFiscal); + } + else + { + DomainBase.Copy(this.NotasFiscais.First((NotaFiscal x) => x.get_Id() == notaFiscal.get_Id()), notaFiscal); + } + if (!this.NotasFiscaisFiltrados.Any((NotaFiscal x) => x.get_Id() == notaFiscal.get_Id())) + { + this.NotasFiscaisFiltrados.Add(notaFiscal); + } + else + { + DomainBase.Copy(this.NotasFiscaisFiltrados.First((NotaFiscal x) => x.get_Id() == notaFiscal.get_Id()), notaFiscal); + } + this.NotasFiscaisFiltrados = new ObservableCollection(this.NotasFiscaisFiltrados); + this.WorkOnSelectedNotaFiscal(notaFiscal, false); + base.Alterar(false); + base.ToggleSnackBar("NOTA FISCAL SALVA COM SUCESSO", true); + keyValuePairs = null; + } + else + { + keyValuePairs = null; + } + } + else + { + keyValuePairs = keyValuePairs1; + } + str1 = null; + return keyValuePairs; + } + + public async Task SalvarLote(List notas) + { + foreach (NotaFiscal nota in notas) + { + bool hasValue = nota.get_IdExtrato().HasValue; + if (hasValue) + { + NotaFiscalServico notaFiscalServico = this._servico; + long? idExtrato = nota.get_IdExtrato(); + hasValue = await notaFiscalServico.Cadatrada(idExtrato.Value); + } + if (hasValue) + { + continue; + } + this.SelectedNotaFiscal = nota; + await this.Salvar(); + } + base.ToggleSnackBar("NOTAS FISCAIS SALVAS COM SUCESSO", true); + } + + private async void Seleciona() + { + base.Loading(true); + await base.PermissaoTela(55); + await this.SelecionaNotaFiscal(); + base.Loading(false); + } + + public async Task SelecionaNotaFiscal() + { + base.Loading(true); + List notaFiscals = await this._servico.BuscarNotasFiscais(); + NotaFiscalViewModel list = this; + List notaFiscals1 = notaFiscals; + list.NotasFiscais = ( + from x in notaFiscals1 + orderby x.get_Seguradora().get_Nome() + select x).ToList(); + this.NotasFiscaisFiltrados = new ObservableCollection(this.NotasFiscais); + this.SelectedNotaFiscal = this.NotasFiscaisFiltrados.FirstOrDefault(); + base.Loading(false); + } + + private async Task WorkOnSelectedNotaFiscal(NotaFiscal value, bool registrar = true) + { + string str; + long? idExtrato; + NotaFiscal notaFiscal; + long? nullable; + object obj; + NotaFiscalViewModel notaFiscalViewModel = this; + notaFiscal = (value == null || value.get_Id() == 0 ? this.CancelNotaFiscal : (NotaFiscal)value.Clone()); + notaFiscalViewModel.CancelNotaFiscal = notaFiscal; + if (value != null && value.get_Id() != 0 && (this.LastAccessId != value.get_Id() || this.LastAccessTela != 55)) + { + if (registrar) + { + NotaFiscalViewModel notaFiscalViewModel1 = this; + string str1 = string.Format("ACESSOU NOTA FISCAL DE ID \"{0}\"", value.get_Id()); + long id = value.get_Id(); + TipoTela? nullable1 = new TipoTela?(55); + object[] nome = new object[] { value.get_Seguradora().get_Nome(), null, null, null, null }; + obj = (!value.get_Data().HasValue ? "-" : string.Format("{0:d}", value.get_Data())); + nome[1] = obj; + nome[2] = value.get_ValorBruto(); + nome[3] = value.get_Iss(); + nome[4] = value.get_ValorLiquido(); + notaFiscalViewModel1.RegistrarAcao(str1, id, nullable1, string.Format("SEGURADORA: {0}\nDATA: {1}\nBRUTO: {2:c}\nISS: {3:c}\nLÍQUIDO: {4:c}", nome)); + } + this.LastAccessId = value.get_Id(); + this.LastAccessTela = 55; + if (string.IsNullOrEmpty(this.SelectedNotaFiscal.get_Extrato())) + { + NotaFiscal selectedNotaFiscal = this.SelectedNotaFiscal; + idExtrato = this.SelectedNotaFiscal.get_IdExtrato(); + if (!idExtrato.HasValue) + { + str = ""; + } + else + { + str = await this._servicoExtrato.BuscarNumExtrato(this.SelectedNotaFiscal.get_IdExtrato()); + } + selectedNotaFiscal.set_Extrato(str); + selectedNotaFiscal = null; + } + NotaFiscal selectedNotaFiscal1 = this.SelectedNotaFiscal; + if (selectedNotaFiscal1 != null) + { + nullable = new long?(selectedNotaFiscal1.get_Id()); + } + else + { + nullable = null; + } + idExtrato = nullable; + long num = value.get_Id(); + if (idExtrato.GetValueOrDefault() != num | !idExtrato.HasValue) + { + this.SelectedNotaFiscal = this.NotasFiscaisFiltrados.FirstOrDefault((NotaFiscal x) => x.get_Id() == value.get_Id()); + } + } + } + } +} \ No newline at end of file -- cgit v1.2.3