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; 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 AdiantamentoViewModel : BaseSegurosViewModel { private readonly AdiantamentoServico _servico; private readonly Vendedor _selectedVendedor; private bool _carregando; private ObservableCollection _adiantamento = new ObservableCollection(); private Gestor.Model.Domain.Seguros.Adiantamento _selectedAdiantamento = new Gestor.Model.Domain.Seguros.Adiantamento(); private bool _logEnabled; public Gestor.Model.Domain.Seguros.Adiantamento CancelAdiantamento; public ObservableCollection Adiantamento { get { return this._adiantamento; } set { this._adiantamento = value; base.OnPropertyChanged("Adiantamento"); } } public bool Carregando { get { return this._carregando; } set { this._carregando = value; base.IsEnabled = !value; base.EnableMenu = !value; base.OnPropertyChanged("Carregando"); } } public bool LogEnabled { get { return this._logEnabled; } set { this._logEnabled = value; base.OnPropertyChanged("LogEnabled"); } } public Gestor.Model.Domain.Seguros.Adiantamento SelectedAdiantamento { get { return this._selectedAdiantamento; } set { long? nullable; this._selectedAdiantamento = value; this.CancelAdiantamento = (value == null || value.get_Id() <= (long)0 ? this.CancelAdiantamento : value); if (value != null) { nullable = new long?(value.get_Id()); } else { nullable = null; } base.VerificarEnables(nullable); base.OnPropertyChanged("SelectedAdiantamento"); } } public AdiantamentoViewModel(Vendedor vendedor) { this._servico = new AdiantamentoServico(); this.Seleciona(vendedor); this._selectedVendedor = vendedor; } public void CancelarAlteracao() { this.Carregando = true; if (this.CancelAdiantamento != null && this.Adiantamento.Any((Gestor.Model.Domain.Seguros.Adiantamento x) => x.get_Id() == this.CancelAdiantamento.get_Id())) { DomainBase.Copy(this.Adiantamento.First((Gestor.Model.Domain.Seguros.Adiantamento x) => x.get_Id() == this.CancelAdiantamento.get_Id()), this.CancelAdiantamento); this.SelectedAdiantamento = this.Adiantamento.First((Gestor.Model.Domain.Seguros.Adiantamento x) => x.get_Id() == this.CancelAdiantamento.get_Id()); } base.Alterar(false); this.Carregando = false; } public async Task CarregarAdiantamento(int index) { this.Carregando = true; this.Adiantamento = new ObservableCollection(await this._servico.BuscarAdiantamentos(this._selectedVendedor.get_Id(), index != 0)); if (this.Adiantamento == null || this.Adiantamento.Count <= 0) { this.LogEnabled = false; } else { this.SelectedAdiantamento = this.Adiantamento.First(); this.LogEnabled = true; } this.Carregando = false; } public async Task Excluir() { bool flag; object obj; object obj1; Gestor.Model.Domain.Seguros.Adiantamento adiantamento; Gestor.Model.Domain.Seguros.Adiantamento adiantamento1; if (this.SelectedAdiantamento == null || this.SelectedAdiantamento.get_Id() == 0) { flag = false; } else if (await base.ShowMessage("DESEJA EXCLUIR?", "SIM", "NÃO", false)) { this.Carregando = true; if (await this._servico.Delete(this.SelectedAdiantamento)) { AdiantamentoViewModel adiantamentoViewModel = this; string str = string.Concat("EXCLUIU O ADIANTAMENTO DO VENDEDOR \"", this.SelectedAdiantamento.get_Vendedor().get_Nome(), "\""); long id = this.SelectedAdiantamento.get_Id(); TipoTela? nullable = new TipoTela?(36); object[] valor = new object[] { this.SelectedAdiantamento.get_Id(), null, null, null, null }; obj = (!this.SelectedAdiantamento.get_Data().HasValue ? "-" : string.Format("{0:d}", this.SelectedAdiantamento.get_Data())); valor[1] = obj; valor[2] = this.SelectedAdiantamento.get_Valor(); valor[3] = Functions.GetDescription(this.SelectedAdiantamento.get_TipoPagamento()); obj1 = (this.SelectedAdiantamento.get_Pago() ? "SIM" : "NÃO"); valor[4] = obj1; adiantamentoViewModel.RegistrarAcao(str, id, nullable, string.Format("ID: {0}\nDATA: {1}\nVALOR: {2:c}\nTIPO PAGAMENTO: {3}\nPAGO: {4}", valor)); int num = this.Adiantamento.IndexOf(this.SelectedAdiantamento); this.Adiantamento.Remove(this.SelectedAdiantamento); this.Adiantamento.Remove(this.SelectedAdiantamento); this.Adiantamento = new ObservableCollection(this.Adiantamento); if (this.Adiantamento.Count <= 0) { this.SelectedAdiantamento = new Gestor.Model.Domain.Seguros.Adiantamento(); base.Alterar(false); base.EnableMenu = false; this.LogEnabled = false; } else { AdiantamentoViewModel adiantamentoViewModel1 = this; adiantamento1 = (num < this.Adiantamento.Count ? this.Adiantamento.ElementAt(num) : this.Adiantamento.Last()); adiantamentoViewModel1.SelectedAdiantamento = adiantamento1; this.LogEnabled = true; } if (this.Adiantamento.Count <= 0) { this.SelectedAdiantamento = new Gestor.Model.Domain.Seguros.Adiantamento(); this.LogEnabled = false; base.Alterar(false); } else { AdiantamentoViewModel adiantamentoViewModel2 = this; adiantamento = (num < this.Adiantamento.Count ? this.Adiantamento.ElementAt(num) : this.Adiantamento.Last()); adiantamentoViewModel2.SelecionaAdiantamento(adiantamento); this.LogEnabled = true; } this.Carregando = false; flag = true; } else { flag = false; } } else { flag = false; } return flag; } public void Incluir() { this.SelectedAdiantamento = new Gestor.Model.Domain.Seguros.Adiantamento(); base.Alterar(true); } public async Task>> Salvar() { List> keyValuePairs; string str; object obj; object obj1; string str1; if (!string.IsNullOrWhiteSpace(this.SelectedAdiantamento.get_Historico())) { this.SelectedAdiantamento.set_Vendedor(this._selectedVendedor); str = (this.SelectedAdiantamento.get_Id() == 0 ? "INCLUIU" : "ALTEROU"); str1 = str; Gestor.Model.Domain.Seguros.Adiantamento adiantamento = await this._servico.Save(this.SelectedAdiantamento); if (this._servico.Sucesso) { AdiantamentoViewModel adiantamentoViewModel = this; string str2 = string.Concat(str1, " ADIANTAMENTO DO VENDEDOR \"", adiantamento.get_Vendedor().get_Nome(), "\""); long id = adiantamento.get_Id(); TipoTela? nullable = new TipoTela?(36); object[] valor = new object[] { adiantamento.get_Id(), null, null, null, null }; obj = (!adiantamento.get_Data().HasValue ? "-" : string.Format("{0:d}", adiantamento.get_Data())); valor[1] = obj; valor[2] = adiantamento.get_Valor(); valor[3] = Functions.GetDescription(adiantamento.get_TipoPagamento()); obj1 = (adiantamento.get_Pago() ? "SIM" : "NÃO"); valor[4] = obj1; adiantamentoViewModel.RegistrarAcao(str2, id, nullable, string.Format("ID: {0}\nDATA: {1}\nVALOR: {2:c}\nTIPO PAGAMENTO: {3}\nPAGO: {4}", valor)); if (!this.Adiantamento.Any((Gestor.Model.Domain.Seguros.Adiantamento x) => x.get_Id() == adiantamento.get_Id())) { this.Adiantamento.Add(adiantamento); } else { DomainBase.Copy(this.Adiantamento.First((Gestor.Model.Domain.Seguros.Adiantamento x) => x.get_Id() == adiantamento.get_Id()), adiantamento); } this.Adiantamento = new ObservableCollection(this.Adiantamento); this.SelectedAdiantamento = this.Adiantamento.First((Gestor.Model.Domain.Seguros.Adiantamento x) => x.get_Id() == adiantamento.get_Id()); this.LogEnabled = true; base.Alterar(false); keyValuePairs = null; } else { keyValuePairs = null; } } else { keyValuePairs = new List>() { new KeyValuePair("Historico|HISTÓRICO", "OBRIGATÓRIO") }; } str1 = null; return keyValuePairs; } private async void Seleciona(Vendedor vendedor) { base.Loading(true); await base.PermissaoTela(15); await this.SelecionaAdiantamento(vendedor); base.Loading(false); } public void SelecionaAdiantamento(Gestor.Model.Domain.Seguros.Adiantamento adiantamento) { this.SelectedAdiantamento = adiantamento; } private async Task SelecionaAdiantamento(Vendedor vendedor) { this.Carregando = true; List adiantamentos = await (new BaseServico()).BuscarAdiantamentoAsync(vendedor); IOrderedEnumerable pago = from x in adiantamentos orderby x.get_Pago() select x; List list = pago.ThenBy((Gestor.Model.Domain.Seguros.Adiantamento x) => x.get_Valor()).ToList(); this.Adiantamento = new ObservableCollection(list); if (this.Adiantamento.Count <= 0) { this.SelectedAdiantamento = new Gestor.Model.Domain.Seguros.Adiantamento(); this.LogEnabled = false; base.Alterar(false); base.VerificarEnables(new long?(this.SelectedAdiantamento.get_Id())); base.EnableMenu = false; } else { this.SelecionaAdiantamento(this.Adiantamento.First()); this.LogEnabled = true; } this.Carregando = false; } } }