summaryrefslogtreecommitdiff
path: root/Codemerx/Gestor.Application/ViewModels/Ferramentas/NotaFiscalViewModel.cs
diff options
context:
space:
mode:
authorLucas Faria Mendes <lucas.fariamo08@gmail.com>2026-03-30 13:38:18 +0000
committerLucas Faria Mendes <lucas.fariamo08@gmail.com>2026-03-30 13:38:18 +0000
commit1f4e14b2e973ee7de337fd4866d9a5ceff5cb6d1 (patch)
treee1c3b20ea08f0cf71122a1e73f0d395f8fd83874 /Codemerx/Gestor.Application/ViewModels/Ferramentas/NotaFiscalViewModel.cs
parent674ca83ba9243a9e95a7568c797668dab6aee26a (diff)
downloadgestor-1f4e14b2e973ee7de337fd4866d9a5ceff5cb6d1.tar.gz
gestor-1f4e14b2e973ee7de337fd4866d9a5ceff5cb6d1.zip
chore: location
Diffstat (limited to 'Codemerx/Gestor.Application/ViewModels/Ferramentas/NotaFiscalViewModel.cs')
-rw-r--r--Codemerx/Gestor.Application/ViewModels/Ferramentas/NotaFiscalViewModel.cs482
1 files changed, 482 insertions, 0 deletions
diff --git a/Codemerx/Gestor.Application/ViewModels/Ferramentas/NotaFiscalViewModel.cs b/Codemerx/Gestor.Application/ViewModels/Ferramentas/NotaFiscalViewModel.cs
new file mode 100644
index 0000000..61ee285
--- /dev/null
+++ b/Codemerx/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<Estipulante> _estipulantes;
+
+ private List<Seguradora> _seguradoras;
+
+ private ObservableCollection<NotaFiscal> _notasFiscaisFiltrados = new ObservableCollection<NotaFiscal>();
+
+ 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<Estipulante> 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<NotaFiscal> NotasFiscais
+ {
+ get;
+ set;
+ }
+
+ public ObservableCollection<NotaFiscal> 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<Seguradora> 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<Seguradora>();
+ this.Estipulantes = (
+ from e in Recursos.Estipulantes
+ where e.get_Ativo()
+ select e).ToList<Estipulante>();
+ this.Apelido = Recursos.Configuracoes.Any<ConfiguracaoSistema>((ConfiguracaoSistema x) => x.get_Configuracao() == 6);
+ base.EnableMenu = true;
+ this.Seleciona();
+ }
+
+ public async Task<decimal> BuscarImposto()
+ {
+ decimal iss;
+ List<Imposto> impostos = await (new ImpostoServico()).Buscar(new bool?(true));
+ Imposto imposto = impostos.FirstOrDefault<Imposto>((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<Imposto> impostos1 = impostos;
+ imposto = impostos1.FirstOrDefault<Imposto>((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>((NotaFiscal x) => x.get_Id() == this.CancelNotaFiscal.get_Id()))
+ {
+ this.Incluir();
+ }
+ else
+ {
+ DomainBase.Copy<NotaFiscal, NotaFiscal>(this.NotasFiscais.First<NotaFiscal>((NotaFiscal x) => x.get_Id() == this.CancelNotaFiscal.get_Id()), this.CancelNotaFiscal);
+ if (this.NotasFiscaisFiltrados.Count <= 0 || !this.NotasFiscaisFiltrados.Any<NotaFiscal>((NotaFiscal x) => x.get_Id() == this.CancelNotaFiscal.get_Id()))
+ {
+ this.NotasFiscaisFiltrados.Add(this.CancelNotaFiscal);
+ }
+ else
+ {
+ DomainBase.Copy<NotaFiscal, NotaFiscal>(this.NotasFiscaisFiltrados.First<NotaFiscal>((NotaFiscal x) => x.get_Id() == this.CancelNotaFiscal.get_Id()), this.CancelNotaFiscal);
+ }
+ this.NotasFiscaisFiltrados = new ObservableCollection<NotaFiscal>(this.NotasFiscaisFiltrados);
+ this.SelectedNotaFiscal = this.NotasFiscaisFiltrados.First<NotaFiscal>((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<List<NotaFiscal>> Filtrar(string value)
+ {
+ List<NotaFiscal> notaFiscals = await Task.Run<List<NotaFiscal>>(() => this.FiltrarNotaFiscal(value));
+ return notaFiscals;
+ }
+
+ public List<NotaFiscal> FiltrarNotaFiscal(string filter)
+ {
+ this.NotasFiscaisFiltrados = (string.IsNullOrWhiteSpace(filter) ? new ObservableCollection<NotaFiscal>(this.NotasFiscais) : new ObservableCollection<NotaFiscal>(this.NotasFiscais.Where<NotaFiscal>((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, string>((NotaFiscal x) => x.get_Seguradora().get_Nome())));
+ return this.NotasFiscaisFiltrados.ToList<NotaFiscal>();
+ }
+
+ 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<List<KeyValuePair<string, string>>> Salvar()
+ {
+ List<KeyValuePair<string, string>> keyValuePairs;
+ string str;
+ long? nullable;
+ bool valueOrDefault;
+ object obj;
+ string str1;
+ List<KeyValuePair<string, string>> 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>((NotaFiscal x) => x.get_Id() == notaFiscal.get_Id()))
+ {
+ this.NotasFiscais.Add(notaFiscal);
+ }
+ else
+ {
+ DomainBase.Copy<NotaFiscal, NotaFiscal>(this.NotasFiscais.First<NotaFiscal>((NotaFiscal x) => x.get_Id() == notaFiscal.get_Id()), notaFiscal);
+ }
+ if (!this.NotasFiscaisFiltrados.Any<NotaFiscal>((NotaFiscal x) => x.get_Id() == notaFiscal.get_Id()))
+ {
+ this.NotasFiscaisFiltrados.Add(notaFiscal);
+ }
+ else
+ {
+ DomainBase.Copy<NotaFiscal, NotaFiscal>(this.NotasFiscaisFiltrados.First<NotaFiscal>((NotaFiscal x) => x.get_Id() == notaFiscal.get_Id()), notaFiscal);
+ }
+ this.NotasFiscaisFiltrados = new ObservableCollection<NotaFiscal>(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<NotaFiscal> 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<NotaFiscal> notaFiscals = await this._servico.BuscarNotasFiscais();
+ NotaFiscalViewModel list = this;
+ List<NotaFiscal> notaFiscals1 = notaFiscals;
+ list.NotasFiscais = (
+ from x in notaFiscals1
+ orderby x.get_Seguradora().get_Nome()
+ select x).ToList<NotaFiscal>();
+ this.NotasFiscaisFiltrados = new ObservableCollection<NotaFiscal>(this.NotasFiscais);
+ this.SelectedNotaFiscal = this.NotasFiscaisFiltrados.FirstOrDefault<NotaFiscal>();
+ 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>((NotaFiscal x) => x.get_Id() == value.get_Id());
+ }
+ }
+ }
+ }
+} \ No newline at end of file