using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Threading.Tasks; using Assinador.Infrastructure.Helpers; using Gestor.Application.Servicos.Ferramentas; using Gestor.Application.Servicos.Generic; using Gestor.Application.ViewModels.Generic; using Gestor.Model.Common; using Gestor.Model.Domain.Generic; using Gestor.Model.Domain.Seguros; namespace Gestor.Application.ViewModels.Drawer; public class AdiantamentoViewModel : BaseSegurosViewModel { private readonly AdiantamentoServico _servico; private readonly Vendedor _selectedVendedor; private bool _carregando; private ObservableCollection _adiantamento = new ObservableCollection(); private Adiantamento _selectedAdiantamento = new Adiantamento(); private bool _logEnabled; public Adiantamento CancelAdiantamento; public bool Carregando { get { return _carregando; } set { _carregando = value; base.IsEnabled = !value; base.EnableMenu = !value; OnPropertyChanged("Carregando"); } } public ObservableCollection Adiantamento { get { return _adiantamento; } set { _adiantamento = value; OnPropertyChanged("Adiantamento"); } } public Adiantamento SelectedAdiantamento { get { return _selectedAdiantamento; } set { _selectedAdiantamento = value; CancelAdiantamento = ((value != null && ((DomainBase)value).Id > 0) ? value : CancelAdiantamento); VerificarEnables((value != null) ? new long?(((DomainBase)value).Id) : null); OnPropertyChanged("SelectedAdiantamento"); } } public bool LogEnabled { get { return _logEnabled; } set { _logEnabled = value; OnPropertyChanged("LogEnabled"); } } public AdiantamentoViewModel(Vendedor vendedor) { //IL_000c: Unknown result type (might be due to invalid IL or missing references) //IL_0016: Expected O, but got Unknown _servico = new AdiantamentoServico(); Seleciona(vendedor); _selectedVendedor = vendedor; } private async void Seleciona(Vendedor vendedor) { Loading(isLoading: true); await PermissaoTela((TipoTela)15); await SelecionaAdiantamento(vendedor); Loading(isLoading: false); } public void SelecionaAdiantamento(Adiantamento adiantamento) { SelectedAdiantamento = adiantamento; } private async Task SelecionaAdiantamento(Vendedor vendedor) { Carregando = true; List list = (from x in await new BaseServico().BuscarAdiantamentoAsync(vendedor) orderby x.Pago, x.Valor select x).ToList(); Adiantamento = new ObservableCollection(list); if (Adiantamento.Count > 0) { SelecionaAdiantamento(Adiantamento.First()); LogEnabled = true; } else { SelectedAdiantamento = new Adiantamento(); LogEnabled = false; Alterar(alterar: false); VerificarEnables(((DomainBase)SelectedAdiantamento).Id); base.EnableMenu = false; } Carregando = false; } public void Incluir() { //IL_0000: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Expected O, but got Unknown Adiantamento selectedAdiantamento = new Adiantamento(); SelectedAdiantamento = selectedAdiantamento; Alterar(alterar: true); } public async Task>> Salvar() { if (string.IsNullOrWhiteSpace(SelectedAdiantamento.Historico)) { return new List> { new KeyValuePair("Historico|HISTÓRICO", "OBRIGATÓRIO") }; } SelectedAdiantamento.Vendedor = _selectedVendedor; string acao = ((((DomainBase)SelectedAdiantamento).Id == 0L) ? "INCLUIU" : "ALTEROU"); Adiantamento value = await _servico.Save(SelectedAdiantamento); if (!_servico.Sucesso) { return null; } RegistrarAcao(acao + " ADIANTAMENTO DO VENDEDOR \"" + value.Vendedor.Nome + "\"", ((DomainBase)value).Id, (TipoTela)36, string.Format("ID: {0}\nDATA: {1}\nVALOR: {2:c}\nTIPO PAGAMENTO: {3}\nPAGO: {4}", ((DomainBase)value).Id, (!value.Data.HasValue) ? "-" : $"{value.Data:d}", value.Valor, Functions.GetDescription((Enum)(object)value.TipoPagamento), value.Pago ? "SIM" : "NÃO")); if (Adiantamento.Any((Adiantamento x) => ((DomainBase)x).Id == ((DomainBase)value).Id)) { DomainBase.Copy(Adiantamento.First((Adiantamento x) => ((DomainBase)x).Id == ((DomainBase)value).Id), value); } else { Adiantamento.Add(value); } Adiantamento = new ObservableCollection(Adiantamento); SelectedAdiantamento = Adiantamento.First((Adiantamento x) => ((DomainBase)x).Id == ((DomainBase)value).Id); LogEnabled = true; Alterar(alterar: false); return null; } public void CancelarAlteracao() { Carregando = true; if (CancelAdiantamento != null && Adiantamento.Any((Adiantamento x) => ((DomainBase)x).Id == ((DomainBase)CancelAdiantamento).Id)) { DomainBase.Copy(Adiantamento.First((Adiantamento x) => ((DomainBase)x).Id == ((DomainBase)CancelAdiantamento).Id), CancelAdiantamento); SelectedAdiantamento = Adiantamento.First((Adiantamento x) => ((DomainBase)x).Id == ((DomainBase)CancelAdiantamento).Id); } Alterar(alterar: false); Carregando = false; } public async Task Excluir() { if (SelectedAdiantamento == null || ((DomainBase)SelectedAdiantamento).Id == 0L) { return false; } if (!(await ShowMessage("DESEJA EXCLUIR?", "SIM", "NÃO"))) { return false; } Carregando = true; if (!(await _servico.Delete(SelectedAdiantamento))) { return false; } RegistrarAcao("EXCLUIU O ADIANTAMENTO DO VENDEDOR \"" + SelectedAdiantamento.Vendedor.Nome + "\"", ((DomainBase)SelectedAdiantamento).Id, (TipoTela)36, string.Format("ID: {0}\nDATA: {1}\nVALOR: {2:c}\nTIPO PAGAMENTO: {3}\nPAGO: {4}", ((DomainBase)SelectedAdiantamento).Id, (!SelectedAdiantamento.Data.HasValue) ? "-" : $"{SelectedAdiantamento.Data:d}", SelectedAdiantamento.Valor, Functions.GetDescription((Enum)(object)SelectedAdiantamento.TipoPagamento), SelectedAdiantamento.Pago ? "SIM" : "NÃO")); int num = Adiantamento.IndexOf(SelectedAdiantamento); Adiantamento.Remove(SelectedAdiantamento); Adiantamento.Remove(SelectedAdiantamento); Adiantamento = new ObservableCollection(Adiantamento); if (Adiantamento.Count > 0) { SelectedAdiantamento = ((num < Adiantamento.Count) ? Adiantamento.ElementAt(num) : Adiantamento.Last()); LogEnabled = true; } else { SelectedAdiantamento = new Adiantamento(); Alterar(alterar: false); base.EnableMenu = false; LogEnabled = false; } if (Adiantamento.Count > 0) { SelecionaAdiantamento((num < Adiantamento.Count) ? Adiantamento.ElementAt(num) : Adiantamento.Last()); LogEnabled = true; } else { SelectedAdiantamento = new Adiantamento(); LogEnabled = false; Alterar(alterar: false); } Carregando = false; return true; } public async Task CarregarAdiantamento(int index) { Carregando = true; Adiantamento = new ObservableCollection(await _servico.BuscarAdiantamentos(((DomainBase)_selectedVendedor).Id, index != 0)); if (Adiantamento != null && Adiantamento.Count > 0) { SelectedAdiantamento = Adiantamento.First(); LogEnabled = true; } else { LogEnabled = false; } Carregando = false; } }