using Gestor.Application.Actions; using Gestor.Application.Helpers; using Gestor.Application.Servicos; using Gestor.Model.Common; using Gestor.Model.Domain.Configuracoes; using Gestor.Model.Domain.Generic; using Gestor.Model.Domain.Seguros; using Gestor.Model.Resources; using System; using System.Collections; 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.Generic { public class DialogEditarParcelasViewModel : BaseViewModel { private List _parcelasOriginais; internal Gestor.Application.Servicos.ParcelaServico ParcelaServico; private ObservableCollection _parcelas; private bool _isFatura; private string _status; private Parcela _selectedParcela; private Documento _selectedDocumento; public bool IsFatura { get { return this._isFatura; } set { this._isFatura = value; base.OnPropertyChanged("IsFatura"); } } public ObservableCollection Parcelas { get { return this._parcelas; } set { this._parcelas = value; base.OnPropertyChanged("Parcelas"); } } public Documento SelectedDocumento { get { return this._selectedDocumento; } set { this._selectedDocumento = value; base.OnPropertyChanged("SelectedDocumento"); } } public Parcela SelectedParcela { get { return this._selectedParcela; } set { this._selectedParcela = value; base.OnPropertyChanged("SelectedParcela"); } } public string Status { get { return this._status; } set { this._status = value; base.OnPropertyChanged("Status"); } } public DialogEditarParcelasViewModel(Documento documento) { this.SelectedDocumento = documento; this.ParcelaServico = new Gestor.Application.Servicos.ParcelaServico(); this.Carregar(documento.get_Id()); TipoRecebimento? tipoRecebimento = documento.get_TipoRecebimento(); this.IsFatura = tipoRecebimento.GetValueOrDefault(1) == 2; } public void AlterandoValor() { if (this.SelectedParcela.get_Documento() == null) { this.SelectedParcela.set_Documento(this.SelectedDocumento); } this.SelectedParcela.get_Documento().set_Parcelas(this.Parcelas); if (this.SelectedDocumento.get_TipoRecebimento().GetValueOrDefault() == 1 && this.SelectedParcela.get_SubTipo() == 1 && this.SelectedParcela.get_Comissao() == decimal.Zero) { this.SelectedParcela.set_Comissao(this.SelectedDocumento.get_Comissao()); } this.SelectedParcela.set_Baixando(false); this.AlterarValor(); } public void AlterandoVencimento(Parcela parcela) { DateTime vencimento; ObservableCollection parcelas; if (Recursos.Configuracoes.All((ConfiguracaoSistema y) => y.get_Configuracao() != 11)) { DateTime? vigencia2 = parcela.get_Documento().get_Vigencia2(); if (vigencia2.HasValue) { vencimento = parcela.get_Vencimento(); vigencia2 = parcela.get_Documento().get_Vigencia2(); if ((vigencia2.HasValue ? vencimento > vigencia2.GetValueOrDefault() : false) && parcela.get_Documento().get_Controle().get_Ramo().get_Id() != (long)23) { Action acionarSnackbar = Gestor.Application.Actions.Actions.AcionarSnackbar; if (acionarSnackbar != null) { acionarSnackbar(string.Concat("DATA INVÁLIDA. ", Messages.get_VencimentoMaior())); } else { } parcela.set_Vencimento(this._parcelasOriginais.First((Parcela x) => x.get_Id() == parcela.get_Id()).get_Vencimento()); } } } if (Recursos.Configuracoes.All((ConfiguracaoSistema x) => x.get_Configuracao() != 9)) { if (parcela.get_Documento().get_Vigencia1() > DateTime.MinValue && parcela.get_NumeroParcela() == 1) { DateTime dateTime = parcela.get_Vencimento(); vencimento = parcela.get_Documento().get_Vigencia1(); if (dateTime < vencimento.AddDays(-30)) { goto Label0; } } if (parcela.get_NumeroParcela() == 1 || !(parcela.get_Vencimento() < parcela.get_Documento().get_Vigencia1())) { parcelas = this.Parcelas; this.Parcelas = new ObservableCollection( from x in parcelas orderby x.get_NumeroParcela() select x); return; } Label0: Action action = Gestor.Application.Actions.Actions.AcionarSnackbar; if (action != null) { action(string.Concat("DATA INVÁLIDA. ", Messages.get_VencimentoMenor())); } else { } parcela.set_Vencimento(this._parcelasOriginais.First((Parcela x) => x.get_Id() == parcela.get_Id()).get_Vencimento()); } parcelas = this.Parcelas; this.Parcelas = new ObservableCollection( from x in parcelas orderby x.get_NumeroParcela() select x); } public void AlterarValor() { if (this.SelectedParcela == null) { return; } if (this.SelectedParcela.get_SubTipo() != 1 || this.IsFatura) { this.SelectedParcela.set_Documento(this.SelectedDocumento); return; } decimal premioTotal = this.SelectedDocumento.get_PremioTotal() - this.Parcelas.Where((Parcela x) => { if (x.get_SubTipo() != 1) { return false; } return x.get_Id() != this.SelectedParcela.get_Id(); }).Sum((Parcela x) => x.get_Valor()); decimal valor = this.SelectedDocumento.get_PremioTotal() - this.Parcelas.Where((Parcela x) => { if (x.get_SubTipo() != 1) { return false; } return x.get_Id() < this.SelectedParcela.get_Id(); }).Sum((Parcela x) => x.get_Valor()); valor -= this.SelectedParcela.get_Valor(); decimal numeroParcelas = this.SelectedDocumento.get_NumeroParcelas() - this.SelectedParcela.get_NumeroParcela(); if (numeroParcelas == decimal.Zero) { numeroParcelas = decimal.One; } decimal num = (numeroParcelas > decimal.Zero ? valor / numeroParcelas : decimal.Zero); if (num == decimal.Zero) { this.SelectedParcela.set_Valor(premioTotal); } List parcelas = new List() { this.SelectedParcela }; decimal num1 = Math.Abs(num); decimal? tolerancia = this.SelectedDocumento.get_Controle().get_Seguradora().get_Tolerancia(); if ((num1 > tolerancia.GetValueOrDefault()) & tolerancia.HasValue && !this.Parcelas.Any((Parcela x) => { if (x.get_SubTipo() != 1) { return false; } return x.get_NumeroParcela() > this.SelectedParcela.get_NumeroParcela(); })) { this.SelectedParcela.set_Valor(premioTotal); } this.Parcelas.Where((Parcela x) => { if (x.get_SubTipo() != 1) { return false; } return x.get_NumeroParcela() != this.SelectedParcela.get_NumeroParcela(); }).ToList().ForEach((Parcela x) => { if (x.get_NumeroParcela() > this.SelectedParcela.get_NumeroParcela()) { x.set_Valor((num > decimal.Zero ? num : x.get_Valor())); x.set_Comissao((x.get_Comissao() == decimal.Zero ? this.SelectedDocumento.get_Comissao() : x.get_Comissao())); } parcelas.Add(x); }); foreach (Parcela parcela in from x in parcelas where x.get_NumeroParcela() >= this.SelectedParcela.get_NumeroParcela() select x) { parcela.set_Documento(this.SelectedDocumento); if (parcela.get_Id() == this.SelectedParcela.get_Id()) { continue; } parcela.set_Valor(num); } this.Parcelas = new ObservableCollection( from x in parcelas orderby x.get_NumeroParcela() select x); } public async void Carregar(long id) { await this.CarregarParcelas(id); } public async Task CarregarParcelas(long id) { ObservableCollection observableCollection; ObservableCollection observableCollection1 = await (new Gestor.Application.Servicos.ParcelaServico()).BuscarParcelasAsync(id); this._parcelasOriginais = new List(); foreach (Parcela parcela in observableCollection1) { this._parcelasOriginais.Add((Parcela)parcela.Clone()); } DialogEditarParcelasViewModel dialogEditarParcelasViewModel = this; if (this.IsFatura) { ObservableCollection observableCollection2 = observableCollection1; observableCollection = new ObservableCollection( from x in observableCollection2 orderby x.get_NumeroParcela() select x); } else { observableCollection = observableCollection1; } dialogEditarParcelasViewModel.Parcelas = observableCollection; } public async Task SalvarParcelas() { bool flag; foreach (Parcela parcela in this.Parcelas) { Parcela parcela1 = this._parcelasOriginais.First((Parcela x) => x.get_Id() == parcela.get_Id()); if (!(parcela.get_Vencimento() != parcela1.get_Vencimento()) && !(parcela.get_Valor() != parcela1.get_Valor())) { continue; } parcela1.set_Vencimento(parcela.get_Vencimento()); parcela1.set_Valor(parcela.get_Valor()); Parcela selectedParcela = this.SelectedParcela; List configuracoes = Recursos.Configuracoes; bool flag1 = configuracoes.Any((ConfiguracaoSistema x) => x.get_Configuracao() == 9); List configuracaoSistemas = Recursos.Configuracoes; bool flag2 = configuracaoSistemas.Any((ConfiguracaoSistema y) => y.get_Configuracao() == 11); List configuracoes1 = Recursos.Configuracoes; List> keyValuePairs = selectedParcela.Validate(flag1, flag2, true, configuracoes1.Any((ConfiguracaoSistema x) => x.get_Configuracao() == 16)); if (keyValuePairs.Count <= 0) { await (new Gestor.Application.Servicos.ParcelaServico()).Save(parcela1, this._parcelasOriginais, null, true); } else { List> keyValuePairs1 = keyValuePairs; string str = string.Join(", ", keyValuePairs1.Select, string>((KeyValuePair x) => { string[] strArrays = x.Key.Split(new char[] { '|' }); return string.Concat(((int)strArrays.Length > 1 ? strArrays[1] : x.Key), ": ", x.Value, Environment.NewLine); })); this.Status = string.Concat("NÃO FOI POSSÍVEL SALVAR DEVIDO AOS ERROS ABAIXO:", Environment.NewLine, str); flag = false; return flag; } } base.RegistrarAcao(string.Format("EDITOU VENCIMENTO E/OU VALORES DAS PARCELAS/FATURAS DO DOCUMENTO DE ID {0}", this.Parcelas[0].get_Documento().get_Id()), this.Parcelas[0].get_Documento().get_Id(), new TipoTela?(5), null); flag = true; return flag; } } }