using Gestor.Application.Servicos; using Gestor.Application.Servicos.Generic; using Gestor.Application.ViewModels.Generic; using Gestor.Common.Validation; using Gestor.Model.Common; using Gestor.Model.Domain.Aggilizador; 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; using System.Windows; namespace Gestor.Application.ViewModels.Drawer { public class ValoresParcelaViewModel : BaseSegurosViewModel { private ParcelaPendente _pendecia; private Visibility _isVisiblePendencia = Visibility.Collapsed; private Visibility _isVisibleFatura; private Visibility _isVisibleVendedores; private decimal _prevista; private decimal _ir; private decimal _iss; private string _repassePago; private decimal _recebidaLiquida; private decimal _recebida; private decimal _recebidaEspecial; private decimal _pendente; private decimal _repasse; private decimal _repasseEspecial; private decimal _paga; private decimal _pagaEspecial; private string _apoliceLabel = "DESCRIÇÃO DE VALORES"; private string _apoliceDescricao = "VALORES BASEADOS NO DOCUMENTO SELECIONADO ATÉ O MOMENTO, OS VALORES PODEM SER DIFERENTES EM CASO DE ALTERAÇÕES POR OUTROS USUÁRIOS."; private Parcela _selectedParcela; private string _statusBaixa; private string _statusParc; private Visibility _isVisiblestatusparc = Visibility.Collapsed; private ObservableCollection _pagamentos; public string ApoliceDescricao { get { return this._apoliceDescricao; } set { this._apoliceDescricao = value; base.OnPropertyChanged("ApoliceDescricao"); } } public string ApoliceLabel { get { return this._apoliceLabel; } set { this._apoliceLabel = value; base.OnPropertyChanged("ApoliceLabel"); } } public decimal Ir { get { return this._ir; } set { this._ir = value; base.OnPropertyChanged("Ir"); } } public decimal Iss { get { return this._iss; } set { this._iss = value; base.OnPropertyChanged("Iss"); } } public Visibility IsVisibleFatura { get { return this._isVisibleFatura; } set { this._isVisibleFatura = value; base.OnPropertyChanged("IsVisibleFatura"); } } public Visibility IsVisiblePendencia { get { return this._isVisiblePendencia; } set { this._isVisiblePendencia = value; base.OnPropertyChanged("IsVisiblePendencia"); } } public Visibility IsVisibleStatusParc { get { return this._isVisiblestatusparc; } set { this._isVisiblestatusparc = value; base.OnPropertyChanged("IsVisibleStatusParc"); } } public Visibility IsVisibleVendedores { get { return this._isVisibleVendedores; } set { this._isVisibleVendedores = value; base.OnPropertyChanged("IsVisibleVendedores"); } } public decimal Paga { get { return this._paga; } set { this._paga = value; base.OnPropertyChanged("Paga"); } } public decimal PagaEspecial { get { return this._pagaEspecial; } set { this._pagaEspecial = value; base.OnPropertyChanged("PagaEspecial"); } } public ObservableCollection Pagamentos { get { return this._pagamentos; } set { this._pagamentos = value; base.OnPropertyChanged("Pagamentos"); } } public ParcelaPendente Pendecia { get { return this._pendecia; } set { this._pendecia = value; this.IsVisiblePendencia = (value == null ? Visibility.Collapsed : Visibility.Visible); base.OnPropertyChanged("Pendecia"); } } public decimal Pendente { get { return this._pendente; } set { if (value >= new decimal(1, 0, 0, false, 2)) { this._pendente = value; } else { this._pendente = new decimal(); } base.OnPropertyChanged("Pendente"); } } public decimal Prevista { get { return this._prevista; } set { this._prevista = value; base.OnPropertyChanged("Prevista"); } } public decimal Recebida { get { return this._recebida; } set { this._recebida = value; base.OnPropertyChanged("Recebida"); } } public decimal RecebidaEspecial { get { return this._recebidaEspecial; } set { this._recebidaEspecial = value; base.OnPropertyChanged("RecebidaEspecial"); } } public decimal RecebidaLiquida { get { return this._recebidaLiquida; } set { this._recebidaLiquida = value; base.OnPropertyChanged("RecebidaLiquida"); } } public decimal Repasse { get { return this._repasse; } set { this._repasse = value; base.OnPropertyChanged("Repasse"); } } public decimal RepasseEspecial { get { return this._repasseEspecial; } set { this._repasseEspecial = value; base.OnPropertyChanged("RepasseEspecial"); } } public string RepassePago { get { return this._repassePago; } set { this._repassePago = value; base.OnPropertyChanged("RepassePago"); } } public Parcela SelectedParcela { get { return this._selectedParcela; } set { this._selectedParcela = value; base.OnPropertyChanged("SelectedParcela"); } } public string StatusBaixa { get { return this._statusBaixa; } set { this._statusBaixa = value; base.OnPropertyChanged("StatusBaixa"); } } public string StatusParc { get { return this._statusParc; } set { this._statusParc = value; base.OnPropertyChanged("StatusParc"); } } public ValoresParcelaViewModel(Parcela parcela, Documento documento) { this.Seleciona(parcela, documento); } private async void BuscarPendencia() { this.Pendecia = await (new BaseServico()).BuscarParcelaPendente(this.SelectedParcela.get_IdParcelaPendente()); } private void CalculaComissao(Documento documento) { decimal valor; if (this.SelectedParcela.get_SubTipo() != 1) { valor = this.SelectedParcela.get_Valor(); } else if (documento.get_NumeroParcelas() == decimal.Zero) { valor = decimal.Zero; } else { valor = (documento.get_AdicionalComiss() ? (documento.get_PremioLiquido() + documento.get_PremioAdicional()) / documento.get_NumeroParcelas() : documento.get_PremioLiquido() / documento.get_NumeroParcelas()); } decimal num = valor; this.Prevista = (this.SelectedParcela.get_ValorLiquidoFatura() != decimal.Zero ? (this.SelectedParcela.get_ValorLiquidoFatura() * this.SelectedParcela.get_Comissao()) * new decimal(1, 0, 0, false, 2) : (num * this.SelectedParcela.get_Comissao()) * new decimal(1, 0, 0, false, 2)); this.Recebida = this.SelectedParcela.get_ValorComissao(); this.Pendente = this.Prevista - this.Recebida; this.Repasse = this.SelectedParcela.get_Vendedores().Where((VendedorParcela x) => { if (this.SelectedParcela.get_SubTipo() == 1) { return x.get_Parcela().get_SubTipo() == 1; } return x.get_Parcela().get_SubTipo() != 1; }).Sum((VendedorParcela x) => x.get_ValorRepasse().GetValueOrDefault()); this.Paga = this.SelectedParcela.get_Vendedores().Where((VendedorParcela x) => { if (this.SelectedParcela.get_SubTipo() == 1) { return x.get_Parcela().get_SubTipo() == 1; } if (x.get_Parcela().get_SubTipo() == 1) { return false; } return x.get_DataPrePagamento().HasValue; }).Sum((VendedorParcela x) => x.get_ValorRepasse().GetValueOrDefault()); this.Ir = this.SelectedParcela.get_Irr(); this.Iss = this.SelectedParcela.get_Iss(); this.RecebidaLiquida = this.SelectedParcela.get_ValorComDesconto(); } private void CalculaRepasse() { if (this.SelectedParcela.get_Vendedores().All((VendedorParcela x) => x.get_Vendedor().get_Corretora())) { this.IsVisibleVendedores = Visibility.Collapsed; return; } List list = ( from x in this.SelectedParcela.get_Vendedores() where !x.get_Vendedor().get_Corretora() group x by x.get_Vendedor().get_Id()).Select, VendedorParcela>((IGrouping x) => { VendedorParcela vendedorParcela = new VendedorParcela(); vendedorParcela.set_Repasse(x.First().get_Repasse()); vendedorParcela.set_Documento(this.SelectedParcela.get_Documento()); vendedorParcela.set_ValorTotal(x.First().get_ValorTotal()); vendedorParcela.set_Vendedor(x.First().get_Vendedor()); vendedorParcela.set_ValorTotalPago(x.Where((VendedorParcela y) => { if (y.get_Parcela().get_SubTipo() != 1) { return false; } return y.get_DataPrePagamento().HasValue; }).Sum((VendedorParcela y) => y.get_ValorRepasse())); vendedorParcela.set_ValorRepasseB(( from y in x where y.get_Parcela().get_SubTipo() != 1 select y).Sum((VendedorParcela y) => y.get_ValorRepasse())); vendedorParcela.set_ValorRepasse(x.Where((VendedorParcela y) => { if (y.get_Parcela().get_SubTipo() == 1) { return false; } return y.get_DataPrePagamento().HasValue; }).Sum((VendedorParcela y) => y.get_ValorRepasse())); decimal? porcentagemRepasse = x.First().get_PorcentagemRepasse(); vendedorParcela.set_PorcentagemRepasse((porcentagemRepasse.HasValue ? new decimal?(porcentagemRepasse.GetValueOrDefault() / 100) : null)); vendedorParcela.set_TipoVendedor(x.First().get_TipoVendedor()); return vendedorParcela; }).ToList(); this.Pagamentos = new ObservableCollection(list); } public async void Seleciona(Parcela parcela, Documento documento) { StatusPagamento? statusPagamento; Visibility visibility; string str; string str1; this.SelectedParcela = parcela; ValoresParcelaViewModel valoresParcelaViewModel = this; visibility = (this.SelectedParcela.get_Documento().get_TipoRecebimento().GetValueOrDefault() == 2 ? Visibility.Visible : Visibility.Collapsed); valoresParcelaViewModel.IsVisibleFatura = visibility; this.ApoliceLabel = string.Format("VALORES DA PARCELA {0} VENCIMENTO {1:d}", this.SelectedParcela.get_NumeroParcela(), parcela.get_Vencimento()); this.ApoliceDescricao = string.Format("VALORES BASEADOS NA PARCELA {0} ATÉ O MOMENTO, OS VALORES PODEM SER DIFERENTES EM CASO DE ALTERAÇÕES POR OUTROS USUÁRIOS.", this.SelectedParcela.get_NumeroParcela()); this.StatusBaixa = "PENDENTE"; this.RepassePago = "REPASSE A SER PAGO"; if (this.SelectedParcela.get_DataRecebimento().HasValue) { ValoresParcelaViewModel valoresParcelaViewModel1 = this; if (this.SelectedParcela.get_Documento().get_TipoRecebimento().GetValueOrDefault() == 2) { str = "BAIXA MANUAL"; } else { str = (this.SelectedParcela.get_ValorRealizado() == decimal.Zero ? "BAIXA ESGOTAMENTO" : "BAIXA MANUAL"); } valoresParcelaViewModel1.StatusBaixa = str; DetalheExtrato detalheExtrato = await (new ServicoExtrato()).FindByParcelaId(this.SelectedParcela.get_Id()); if (detalheExtrato != null) { StatusParcela? status = detalheExtrato.get_Status(); if (status.HasValue) { StatusParcela valueOrDefault = status.GetValueOrDefault(); if (valueOrDefault != 1) { if (valueOrDefault != 5) { switch (valueOrDefault) { case 10: case 13: { goto Label1; } case 11: { this.CalculaComissao(documento); this.CalculaRepasse(); if (this.SelectedParcela.get_IdParcelaPendente() > (long)0) { statusPagamento = this.SelectedParcela.get_StatusPagamento(); if (statusPagamento.GetValueOrDefault() != 0 | !statusPagamento.HasValue) { this.BuscarPendencia(); } } return; } case 12: { break; } default: { this.CalculaComissao(documento); this.CalculaRepasse(); if (this.SelectedParcela.get_IdParcelaPendente() > (long)0) { statusPagamento = this.SelectedParcela.get_StatusPagamento(); if (statusPagamento.GetValueOrDefault() != 0 | !statusPagamento.HasValue) { this.BuscarPendencia(); } } return; } } } this.RepassePago = "REPASSE PAGO"; this.StatusBaixa = "BAIXA MANUAL"; this.CalculaComissao(documento); this.CalculaRepasse(); if (this.SelectedParcela.get_IdParcelaPendente() > (long)0) { statusPagamento = this.SelectedParcela.get_StatusPagamento(); if (statusPagamento.GetValueOrDefault() != 0 | !statusPagamento.HasValue) { this.BuscarPendencia(); } } return; } Label1: this.IsVisibleStatusParc = Visibility.Visible; this.StatusParc = ValidationHelper.GetDescription(detalheExtrato.get_Status()); this.RepassePago = "REPASSE PAGO"; ValoresParcelaViewModel valoresParcelaViewModel2 = this; str1 = (this.SelectedParcela.get_ValorRealizado() == decimal.Zero ? "BAIXA AUTOMATICA ESGOTAMENTO" : "BAIXA AUTOMATICA"); valoresParcelaViewModel2.StatusBaixa = str1; } } } this.CalculaComissao(documento); this.CalculaRepasse(); if (this.SelectedParcela.get_IdParcelaPendente() > (long)0) { statusPagamento = this.SelectedParcela.get_StatusPagamento(); if (statusPagamento.GetValueOrDefault() != 0 | !statusPagamento.HasValue) { this.BuscarPendencia(); } } } } }