using CsQuery.ExtensionMethods; using Gestor.Application.Helpers; using Gestor.Application.Servicos; using Gestor.Application.Servicos.Ferramentas; using Gestor.Application.ViewModels.Generic; using Gestor.Model.Common; using Gestor.Model.Domain.Ferramentas; using Gestor.Model.Domain.Generic; using Gestor.Model.Domain.Relatorios; using Gestor.Model.Domain.Seguros; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Diagnostics; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using System.Threading.Tasks; using System.Windows; using System.Windows.Data; namespace Gestor.Application.ViewModels.Ferramentas { public class ManutencaoPagamentosViewModel : BaseSegurosViewModel { private Visibility _filtrosPersonalizados = Visibility.Collapsed; private Visibility _visibilityFiltroPersonalizado = Visibility.Collapsed; private List _filtroRelatorioPersonalizado; private Gestor.Model.Domain.Relatorios.FiltroPersonalizado _filtroPersonalizado; private Type _tipoString = typeof(string); private Type _tipoEnum = typeof(Enum); private Type _tipoDateTime = typeof(DateTime); private Type _tipoDecimal = typeof(decimal); private Type _tipoInt = typeof(int); private Type _tipoLong = typeof(long); private string _valorIncial = ""; private string _valorFinal = ""; private bool _semValor; private Visibility _visibilitySemValor = Visibility.Collapsed; private ObservableCollection _filtroPersonalizadoSelecionado; private ObservableCollection _filtroRelatorioSelecionado = new ObservableCollection(); private ObservableCollection _vendedores = new ObservableCollection(); private Vendedor _selectedVendedor; private bool _enableExcluirManutPagamento; private ObservableCollection _pagamentos; private ListCollectionView _view; private bool _allSelected; private string _selectedFiltro; private ObservableCollection _filtros; private DateTime? _inicio; private DateTime? _fim; private List _todosPagamentos; public bool AllSelected { get { return this._allSelected; } set { this._allSelected = value; base.OnPropertyChanged("AllSelected"); this.Selecionar(value); } } public bool EnableExcluirManutPagamento { get { return this._enableExcluirManutPagamento; } set { this._enableExcluirManutPagamento = value; base.OnPropertyChanged("EnableExcluirManutPagamento"); } } public Gestor.Model.Domain.Relatorios.FiltroPersonalizado FiltroPersonalizado { get { return this._filtroPersonalizado; } set { char chr; this._filtroPersonalizado = value; this.VisibilitySemValor = (value != null ? Visibility.Visible : Visibility.Collapsed); base.OnPropertyChanged("FiltroPersonalizado"); if (value == null) { return; } string name = value.get_Tipo().Name; if (name != null) { switch (name.Length) { case 3: { if (name == "int") { break; } return; } case 4: { chr = name[0]; if (chr == 'E') { if (name == "Enum") { this.ValorInicial = null; this.ValorFinal = null; return; } return; } else { if (chr != 'l') { return; } if (name == "long") { break; } return; } } case 5: { chr = name[3]; if (chr == '3') { if (name == "int32") { break; } return; } else { if (chr != '6') { return; } if (name == "int64") { break; } return; } } case 6: { if (name != "String") { return; } this.ValorInicial = ""; this.ValorFinal = ""; return; } case 7: { if (name != "Decimal") { return; } this.ValorInicial = "0,00"; this.ValorFinal = "0,00"; return; } case 8: { if (name == "DateTime") { this.ValorInicial = null; this.ValorFinal = null; return; } return; } default: { return; } } this.ValorInicial = "0"; this.ValorFinal = "0"; } } } public ObservableCollection FiltroPersonalizadoSelecionado { get { return this._filtroPersonalizadoSelecionado; } set { this._filtroPersonalizadoSelecionado = value; base.OnPropertyChanged("FiltroPersonalizadoSelecionado"); } } public ObservableCollection FiltroRelatorioSelecionado { get { return this._filtroRelatorioSelecionado; } set { this._filtroRelatorioSelecionado = value; base.OnPropertyChanged("FiltroRelatorioSelecionado"); } } public ObservableCollection Filtros { get { return this._filtros; } set { this._filtros = value; base.OnPropertyChanged("Filtros"); } } public Visibility FiltrosPersonalizados { get { return this._filtrosPersonalizados; } set { this._filtrosPersonalizados = value; base.OnPropertyChanged("FiltrosPersonalizados"); } } public DateTime? Fim { get { return this._fim; } set { if (value.HasValue && value.Value < new DateTime(1754, 1, 1)) { value = new DateTime?(new DateTime(1754, 1, 1)); } if (value.HasValue && value.Value > new DateTime(9999, 12, 31)) { value = new DateTime?(new DateTime(9999, 12, 31)); } this._fim = value; base.OnPropertyChanged("Fim"); } } public DateTime? Inicio { get { return this._inicio; } set { if (value.HasValue && value.Value < new DateTime(1754, 1, 1)) { value = new DateTime?(new DateTime(1754, 1, 1)); } if (value.HasValue && value.Value > new DateTime(9999, 12, 31)) { value = new DateTime?(new DateTime(9999, 12, 31)); } this._inicio = value; base.OnPropertyChanged("Inicio"); } } public ObservableCollection Pagamentos { get { return this._pagamentos; } set { this._pagamentos = value; this.View = new ListCollectionView(value); this.View.GroupDescriptions.Add(new PropertyGroupDescription("Pagamento")); this.View.GroupDescriptions.Add(new PropertyGroupDescription("Vendedor")); base.OnPropertyChanged("Pagamentos"); } } public List RelatorioFiltroPersonalizado { get { return this._filtroRelatorioPersonalizado; } set { this._filtroRelatorioPersonalizado = value; base.OnPropertyChanged("RelatorioFiltroPersonalizado"); } } public string SelectedFiltro { get { return this._selectedFiltro; } set { this._selectedFiltro = value; base.OnPropertyChanged("SelectedFiltro"); } } public Vendedor SelectedVendedor { get { return this._selectedVendedor; } set { this._selectedVendedor = value; base.OnPropertyChanged("SelectedVendedor"); } } public bool SemValor { get { return this._semValor; } set { this._semValor = value; if (value) { this.ValorInicial = null; this.ValorFinal = null; } base.OnPropertyChanged("SemValor"); } } public Type TipoDateTime { get { return this._tipoDateTime; } set { this._tipoDateTime = value; base.OnPropertyChanged("TipoDateTime"); } } public Type TipoDecimal { get { return this._tipoDecimal; } set { this._tipoDecimal = value; base.OnPropertyChanged("TipoDecimal"); } } public Type TipoEnum { get { return this._tipoEnum; } set { this._tipoEnum = value; base.OnPropertyChanged("TipoEnum"); } } public Type TipoInt { get { return this._tipoInt; } set { this._tipoInt = value; base.OnPropertyChanged("TipoInt"); } } public Type TipoLong { get { return this._tipoLong; } set { this._tipoLong = value; base.OnPropertyChanged("TipoLong"); } } public Type TipoString { get { return this._tipoString; } set { this._tipoString = value; base.OnPropertyChanged("TipoString"); } } private List TodosPagamentos { get { return this._todosPagamentos; } set { this.FiltrosPersonalizados = (value == null || !value.Any() ? Visibility.Collapsed : Visibility.Visible); this._todosPagamentos = value; } } public string ValorFinal { get { return this._valorFinal; } set { this._valorFinal = value; base.OnPropertyChanged("ValorFinal"); } } public string ValorInicial { get { return this._valorIncial; } set { this._valorIncial = value; base.OnPropertyChanged("ValorInicial"); } } public ObservableCollection Vendedores { get { return this._vendedores; } set { this._vendedores = value; this.SelectedVendedor = this.SelectedVendedor ?? value.FirstOrDefault(); base.OnPropertyChanged("Vendedores"); } } public ListCollectionView View { get { return this._view; } set { this._view = value; base.OnPropertyChanged("View"); } } public Visibility VisibilityFiltroPersonalizado { get { return this._visibilityFiltroPersonalizado; } set { this._visibilityFiltroPersonalizado = value; base.OnPropertyChanged("VisibilityFiltroPersonalizado"); } } public Visibility VisibilitySemValor { get { return this._visibilitySemValor; } set { this._visibilitySemValor = value; base.OnPropertyChanged("VisibilitySemValor"); } } public ManutencaoPagamentosViewModel() { this._filtrosPersonalizados = Visibility.Collapsed; this._visibilityFiltroPersonalizado = Visibility.Collapsed; this._tipoString = typeof(string); this._tipoEnum = typeof(Enum); this._tipoDateTime = typeof(DateTime); this._tipoDecimal = typeof(decimal); this._tipoInt = typeof(int); this._tipoLong = typeof(long); this._valorIncial = ""; this._valorFinal = ""; this._visibilitySemValor = Visibility.Collapsed; this._filtroRelatorioSelecionado = new ObservableCollection(); this._vendedores = new ObservableCollection(); Usuario usuario = Recursos.Usuario; if (usuario != null) { id = usuario.get_Id() > (long)0; } else { id = false; } this._enableExcluirManutPagamento = id; this._inicio = new DateTime?(Funcoes.GetNetworkTime()); DateTime networkTime = Funcoes.GetNetworkTime(); this._fim = new DateTime?(networkTime.AddDays(7)); base(); bool id; this.Filtros = new ObservableCollection() { "DATA DE EMISSÃO DO RECEBIMENTO", "DATA DE EMISSÃO DO RECIBO DE PAGAMENTO" }; this.SelectedFiltro = this.Filtros.First(); List list = ( from x in Recursos.Vendedores where x.get_Ativo() orderby x.get_Nome() select x).ToList(); Vendedor vendedor = new Vendedor(); vendedor.set_Id((long)0); vendedor.set_Nome("TODAS OS VENDEDORES"); list.Insert(0, vendedor); this.Vendedores = new ObservableCollection(list); this.RelatorioFiltroPersonalizado = Funcoes.PopularFiltroPersonalizado(); } public async void AdcionarFiltroPersonalizado() { string str; DateTime dateTime; decimal num; int num1; long num2; List filtroPersonalizados; List filtroPersonalizados1; if (this.FiltroPersonalizado != null) { if (this.SemValor) { filtroPersonalizados = (this.FiltroPersonalizadoSelecionado == null ? new List() : ( from x in this.FiltroPersonalizadoSelecionado where x.get_Propriedade() == this.FiltroPersonalizado.get_Propriedade() select x).ToList()); List filtroPersonalizados2 = filtroPersonalizados; if (filtroPersonalizados2.Count > 0) { filtroPersonalizados2.ForEach((Gestor.Model.Domain.Relatorios.FiltroPersonalizado x) => this.FiltroPersonalizadoSelecionado.Remove(x)); } str = string.Concat(this.FiltroPersonalizado.get_Nome(), ": EM BRANCO"); } else { filtroPersonalizados1 = (this.FiltroPersonalizadoSelecionado == null ? new List() : this.FiltroPersonalizadoSelecionado.Where((Gestor.Model.Domain.Relatorios.FiltroPersonalizado x) => { if (x.get_Propriedade() != this.FiltroPersonalizado.get_Propriedade()) { return false; } return x.get_SemValor(); }).ToList()); List filtroPersonalizados3 = filtroPersonalizados1; if (filtroPersonalizados3.Count > 0) { filtroPersonalizados3.ForEach((Gestor.Model.Domain.Relatorios.FiltroPersonalizado x) => this.FiltroPersonalizadoSelecionado.Remove(x)); } string name = this.FiltroPersonalizado.get_Tipo().Name; if (name == "DateTime") { if (!DateTime.TryParse(this.ValorInicial, out dateTime) || !DateTime.TryParse(this.ValorFinal, out dateTime) || DateTime.Parse(this.ValorInicial) > DateTime.Parse(this.ValorFinal)) { await base.ShowMessage("O VALOR DE INTERVALO DEVE SER PREENCHIDO CORRETAMENTE.", "OK", "", false); return; } else { str = string.Format("{0}: {1:d} até {2:d}", this.FiltroPersonalizado.get_Nome(), DateTime.Parse(this.ValorInicial), DateTime.Parse(this.ValorFinal)); } } else if (name == "Decimal") { if (!decimal.TryParse(this.ValorInicial, out num) || !decimal.TryParse(this.ValorFinal, out num) || Math.Abs(decimal.Parse(this.ValorInicial)) > Math.Abs(decimal.Parse(this.ValorFinal))) { await base.ShowMessage("O VALOR DE INTERVALO DEVE SER PREENCHIDO CORRETAMENTE.", "OK", "", false); return; } else { str = string.Format("{0}: {1:n2} até {2:n2}", this.FiltroPersonalizado.get_Nome(), decimal.Parse(this.ValorInicial), decimal.Parse(this.ValorFinal)); } } else if (name == "Int32") { if (!int.TryParse(this.ValorInicial, out num1) || !int.TryParse(this.ValorFinal, out num1) || int.Parse(this.ValorInicial) > int.Parse(this.ValorFinal)) { await base.ShowMessage("O VALOR DE INTERVALO DEVE SER PREENCHIDO CORRETAMENTE.", "OK", "", false); return; } else { str = string.Format("{0}: {1:n0} até {2:n0}", this.FiltroPersonalizado.get_Nome(), int.Parse(this.ValorInicial), int.Parse(this.ValorFinal)); } } else if (name != "Int64") { if (!string.IsNullOrEmpty(this.ValorInicial)) { str = string.Concat(this.FiltroPersonalizado.get_Nome(), " = ", this.ValorInicial); } else { await base.ShowMessage("O VALOR DEVE SER PREENCHIDO CORRETAMENTE.", "OK", "", false); return; } } else if (!long.TryParse(this.ValorInicial, out num2) || !long.TryParse(this.ValorFinal, out num2) || long.Parse(this.ValorInicial) > (long)int.Parse(this.ValorFinal)) { await base.ShowMessage("O VALOR DE INTERVALO DEVE SER PREENCHIDO CORRETAMENTE.", "OK", "", false); return; } else { str = string.Format("{0}: {1:n0} até {2:n0}", this.FiltroPersonalizado.get_Nome(), long.Parse(this.ValorInicial), long.Parse(this.ValorFinal)); } } if (this.FiltroPersonalizadoSelecionado == null) { this.FiltroPersonalizadoSelecionado = new ObservableCollection(); } ObservableCollection filtroPersonalizadoSelecionado = this.FiltroPersonalizadoSelecionado; Gestor.Model.Domain.Relatorios.FiltroPersonalizado filtroPersonalizado = new Gestor.Model.Domain.Relatorios.FiltroPersonalizado(); filtroPersonalizado.set_Nome(this.FiltroPersonalizado.get_Nome()); filtroPersonalizado.set_Propriedade(this.FiltroPersonalizado.get_Propriedade()); filtroPersonalizado.set_Tipo(this.FiltroPersonalizado.get_Tipo()); filtroPersonalizado.set_ValorIncial(this.ValorInicial); filtroPersonalizado.set_ValorFinal(this.ValorFinal); filtroPersonalizado.set_SemValor(this.SemValor); filtroPersonalizado.set_Descricao(str); filtroPersonalizadoSelecionado.Add(filtroPersonalizado); this.FiltroPersonalizado = null; this.ValorInicial = string.Empty; this.ValorFinal = string.Empty; this.PesquisaPersonalizada(); } } public async Task Buscar() { if (this.Inicio.HasValue && this.Fim.HasValue) { this.FiltroPersonalizadoSelecionado = null; this.VisibilityFiltroPersonalizado = Visibility.Visible; Gestor.Model.Domain.Relatorios.Filtros filtro = new Gestor.Model.Domain.Relatorios.Filtros(); filtro.set_Inicio(this.Inicio.Value); filtro.set_Fim(this.Fim.Value); Gestor.Model.Domain.Relatorios.Filtros filtro1 = filtro; if (this.SelectedVendedor.get_Id() > (long)0) { filtro1.set_Vendedores(new List() { this.SelectedVendedor.get_Id() }); } if (this.SelectedFiltro == "DATA DE EMISSÃO DO RECEBIMENTO") { filtro1.set_Referencia("DATA PREVISÃO"); } else { filtro1.set_Referencia("DATA PAGAMENTO"); } this.TodosPagamentos = await (new VendedorServico()).BuscarPagos(filtro1); this.Pagamentos = new ObservableCollection(this.TodosPagamentos); string[] shortDateString = new string[] { "CONSULTOU OS PAGAMENTOS DE \"", null, null, null, null, null }; DateTime value = this.Inicio.Value; shortDateString[1] = value.ToShortDateString(); shortDateString[2] = "\" A \""; value = this.Fim.Value; shortDateString[3] = value.ToShortDateString(); shortDateString[4] = "\" POR "; shortDateString[5] = this.SelectedFiltro; base.RegistrarAcao(string.Concat(shortDateString), (long)0, new TipoTela?(58), null); } } public async Task Excluir() { object obj; string str; ObservableCollection pagamentos = this.Pagamentos; List list = ( from x in pagamentos where x.get_Selecionado() select x).ToList(); if (list.Count != 0) { str = "PAGAMENTOS EXCLUÍDOS:"; foreach (ManutencaoPagamentos manutencaoPagamento in list) { str = string.Concat(str, string.Format("\nID: {0}, NÚMERO: {1}, ID DOCUMENTO: {2}", manutencaoPagamento.get_Id(), manutencaoPagamento.get_Parcela(), manutencaoPagamento.get_IdDocumento())); VendedorParcela vendedorParcela = await (new VendedorServico()).BuscarVendedorParcelaCompleto(manutencaoPagamento.get_Id()); vendedorParcela.set_DataPagamento(null); await (new ParcelaServico()).Save(vendedorParcela); } ManutencaoPagamentosViewModel manutencaoPagamentosViewModel = this; object count = list.Count; obj = (list.Count > 1 ? "S" : ""); manutencaoPagamentosViewModel.RegistrarAcao(string.Format("EXCLUIU {0} PAGAMENTO{1}", count, obj), (long)0, new TipoTela?(58), str); for (int i = this.Pagamentos.Count - 1; i >= 0; i--) { if (this.Pagamentos[i].get_Selecionado()) { this.Pagamentos.RemoveAt(i); } } base.ToggleSnackBar("PAGAMENTOS EXCLUÍDOS COM SUCESSO.", true); } list = null; str = null; } public void PesquisaPersonalizada() { this.Pagamentos = new ObservableCollection(this.TodosPagamentos.CustomWhere(this.FiltroPersonalizadoSelecionado.ToList(), false)); } public void Selecionar(bool value) { if (this.Pagamentos == null || this.Pagamentos.Count == 0) { return; } ExtensionMethods.ForEach(this.Pagamentos, (ManutencaoPagamentos x) => x.set_Selecionado(value)); } } }