using CsQuery.ExtensionMethods.Internal; using Gestor.Application.Helpers; using Gestor.Application.Servicos.Seguros.Itens; using Gestor.Application.ViewModels; using Gestor.Common.Validation; using Gestor.Model.Common; using Gestor.Model.Domain.Generic; using Gestor.Model.Domain.Seguros; using Microsoft.CSharp.RuntimeBinder; using Microsoft.Office.Interop.Excel; using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Globalization; using System.Linq; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Forms; namespace Gestor.Application.ViewModels.Generic { public class ItemViewModel : BaseSegurosViewModel { private bool _alterandoFiltros; public Gestor.Application.ViewModels.CoberturaViewModel CoberturaViewModel; public ItemServico _itemServico; private Visibility _alteracaoTitularesVisibility = Visibility.Collapsed; private bool _enableAlterarExcluirTitulares; private List _excluirTlitulares = new List(); private TitularesVida _titularAlteracao = new TitularesVida(); private bool _codigo; private bool _inicio; private bool _fim; private bool _fatura; private bool _nome; private bool _observacao; private bool _nascimento; private bool _cpf; private bool _matricula; private bool _premio; private bool _capital; private bool _tipo; private ObservableCollection _titulares = new ObservableCollection(); private bool? _allSelected = new bool?(false); private bool _allSelectedChanging; private List _oldTitulares = new List(); public bool? AllSelected { get { return this._allSelected; } set { bool? nullable = value; bool? nullable1 = this._allSelected; if (nullable.GetValueOrDefault() == nullable1.GetValueOrDefault() & nullable.HasValue == nullable1.HasValue) { return; } nullable1 = value; bool flag = false; this.EnableAlterarExcluirTitulares = !(nullable1.GetValueOrDefault() == flag & nullable1.HasValue); nullable1 = value; if (!nullable1.GetValueOrDefault() & nullable1.HasValue) { this.AlteracaoTitularesVisibility = Visibility.Collapsed; } this._allSelected = value; this.AllSelectedChanged(); this.Titulares = new ObservableCollection(this.Titulares); base.OnPropertyChanged("AllSelected"); } } public Visibility AlteracaoTitularesVisibility { get { return this._alteracaoTitularesVisibility; } set { this._alteracaoTitularesVisibility = value; base.OnPropertyChanged("AlteracaoTitularesVisibility"); } } public bool Capital { get { return this._capital; } set { this._capital = value; base.OnPropertyChanged("Capital"); } } public bool Codigo { get { return this._codigo; } set { this._codigo = value; base.OnPropertyChanged("Codigo"); } } public bool Cpf { get { return this._cpf; } set { this._cpf = value; base.OnPropertyChanged("Cpf"); } } public AutoCompleteFilterPredicate DependenteItemFilter { get { AutoCompleteFilterPredicate u003cu003e9_980 = ItemViewModel.u003cu003ec.u003cu003e9__98_0; if (u003cu003e9_980 == null) { u003cu003e9_980 = new AutoCompleteFilterPredicate(ItemViewModel.u003cu003ec.u003cu003e9, (string searchText, object obj) => { if (!((TitularesVida)obj).get_Nome().ToUpper().Contains(searchText.ToUpper())) { return false; } return ((TitularesVida)obj).get_Tipo().GetValueOrDefault() != 2; }); ItemViewModel.u003cu003ec.u003cu003e9__98_0 = u003cu003e9_980; } return u003cu003e9_980; } } public bool EnableAlterarExcluirTitulares { get { return this._enableAlterarExcluirTitulares; } set { this._enableAlterarExcluirTitulares = value; base.OnPropertyChanged("EnableAlterarExcluirTitulares"); } } public List ExcluirTitulares { get { return this._excluirTlitulares; } set { this._excluirTlitulares = value; base.OnPropertyChanged("ExcluirTitulares"); } } public bool Fatura { get { return this._fatura; } set { this._fatura = value; base.OnPropertyChanged("Fatura"); } } public bool Fim { get { return this._fim; } set { this._fim = value; base.OnPropertyChanged("Fim"); } } public bool Inicio { get { return this._inicio; } set { this._inicio = value; base.OnPropertyChanged("Inicio"); } } public Item ItemSubstituido { get; set; } public bool Mantido { get; set; } public bool Matricula { get { return this._matricula; } set { this._matricula = value; base.OnPropertyChanged("Matricula"); } } public bool Nascimento { get { return this._nascimento; } set { this._nascimento = value; base.OnPropertyChanged("Nascimento"); } } public bool Nome { get { return this._nome; } set { this._nome = value; base.OnPropertyChanged("Nome"); } } public bool Observacao { get { return this._observacao; } set { this._observacao = value; base.OnPropertyChanged("Observacao"); } } public List OldTitulares { get { return this._oldTitulares; } set { this._oldTitulares = value; base.OnPropertyChanged("OldTitulares"); } } public bool Premio { get { return this._premio; } set { this._premio = value; base.OnPropertyChanged("Premio"); } } public bool Tipo { get { return this._tipo; } set { this._tipo = value; base.OnPropertyChanged("Tipo"); } } public TitularesVida TitularAlteracao { get { return this._titularAlteracao; } set { this._titularAlteracao = value; base.OnPropertyChanged("TitularAlteracao"); } } public ObservableCollection Titulares { get { return this._titulares; } set { this._titulares = value; foreach (TitularesVida titulare in this.Titulares) { titulare.add_PropertyChanged(new PropertyChangedEventHandler(this.TitularesOnPropertyChanged)); } base.OnPropertyChanged("Titulares"); } } public ItemViewModel() { this.CoberturaViewModel = new Gestor.Application.ViewModels.CoberturaViewModel(null, null); this._itemServico = new ItemServico(); } private void AllSelectedChanged() { if (this._allSelectedChanging) { return; } try { this._alterandoFiltros = true; this._allSelectedChanging = true; if (this.AllSelected.HasValue) { foreach (TitularesVida titulare in this.Titulares) { titulare.set_Selecionado(this.AllSelected.Value); } this._alterandoFiltros = false; } } finally { this._allSelectedChanging = false; } } public void AlterarTitular() { if (this.AlteracaoTitularesVisibility == Visibility.Visible) { this.AlteracaoTitularesVisibility = Visibility.Collapsed; return; } this.TitularAlteracao = new TitularesVida(); this.Codigo = false; this.Inicio = false; this.Fim = false; this.Fatura = false; this.Nome = false; this.Nascimento = false; this.Cpf = false; this.Matricula = false; this.Premio = false; this.Capital = false; this.Tipo = false; this.AlteracaoTitularesVisibility = Visibility.Visible; } public void AplicarTitular() { foreach (TitularesVida list in ( from x in this.Titulares where x.get_Selecionado() select x).ToList()) { if (this.Codigo) { list.set_Codigo(this.TitularAlteracao.get_Codigo()); } if (this.Inicio) { list.set_Inicio(this.TitularAlteracao.get_Inicio()); } if (this.Fim) { list.set_Fim(this.TitularAlteracao.get_Fim()); } if (this.Fatura) { list.set_Fatura(this.TitularAlteracao.get_Fatura()); } if (this.Nome) { list.set_Nome(this.TitularAlteracao.get_Nome()); } if (this.Observacao) { list.set_Observacao(this.TitularAlteracao.get_Observacao()); } if (this.Nascimento) { list.set_Nascimento(this.TitularAlteracao.get_Nascimento()); } if (this.Cpf) { list.set_Cpf(this.TitularAlteracao.get_Cpf()); } if (this.Matricula) { list.set_Matricula(this.TitularAlteracao.get_Matricula()); } if (this.Premio) { list.set_Premio(this.TitularAlteracao.get_Premio()); } if (this.Capital) { list.set_Capital(this.TitularAlteracao.get_Capital()); } if (!this.Tipo) { continue; } list.set_Tipo(this.TitularAlteracao.get_Tipo()); } this.Titulares = new ObservableCollection(this.Titulares); this.AlteracaoTitularesVisibility = Visibility.Collapsed; } internal async Task> BuscarTitulares(string value) { Func func1 = null; List titularesVidas = await Task.Run>(() => { ObservableCollection titulares = this.Titulares; Func u003cu003e9_1 = func1; if (u003cu003e9_1 == null) { Func valueOrDefault = (TitularesVida x) => { if (x.get_Tipo().GetValueOrDefault() == 2 || x.get_Nome() == null) { return false; } return ValidationHelper.RemoveDiacritics(x.get_Nome().Trim()).ToUpper().Contains(value.ToUpper()); }; Func func = valueOrDefault; func1 = valueOrDefault; u003cu003e9_1 = func; } return titulares.Where(u003cu003e9_1).ToList(); }); return titularesVidas; } public async Task CarregarTitulares(long id) { this.Titulares = new ObservableCollection(await this._itemServico.BuscaTitularesVida(id)); ItemViewModel list = this; List titularesVidas = this.Titulares.ToList(); list.OldTitulares = titularesVidas.Select((TitularesVida x) => { TitularesVida titularesVida = new TitularesVida(); titularesVida.set_Id(x.get_Id()); titularesVida.set_IdItem(x.get_IdItem()); titularesVida.set_Codigo(x.get_Codigo()); titularesVida.set_Inicio(x.get_Inicio()); titularesVida.set_Fim(x.get_Fim()); titularesVida.set_Fatura(x.get_Fatura()); titularesVida.set_Nome(x.get_Nome()); titularesVida.set_Observacao(x.get_Observacao()); titularesVida.set_Nascimento(x.get_Nascimento()); titularesVida.set_Cpf(x.get_Cpf()); titularesVida.set_Matricula(x.get_Matricula()); titularesVida.set_Premio(x.get_Premio()); titularesVida.set_Capital(x.get_Capital()); titularesVida.set_Tipo(x.get_Tipo()); titularesVida.set_Dependente(x.get_Dependente()); return titularesVida; }).ToList(); } public void ExcluirTitular() { List list = ( from x in this.Titulares where x.get_Selecionado() select x).ToList(); this.ExcluirTitulares.AddRange( from x in list where x.get_Id() > (long)0 select x); foreach (TitularesVida titularesVida in list) { this.Titulares.Remove(titularesVida); } this.AllSelected = new bool?(false); } public async Task ImportarTitulares() { string fileName; using (OpenFileDialog openFileDialog = new OpenFileDialog()) { openFileDialog.Filter = "Excel|*.xls;*.xlsx"; openFileDialog.InitialDirectory = Environment.SpecialFolder.Desktop.ToString(); if (DialogResult.OK == openFileDialog.ShowDialog()) { fileName = openFileDialog.FileName; } else { return; } } base.Loading(true); int num1 = 0; try { List titularesVidas2 = await Task.Run>(() => { string shortDateString; DateTime dateTime; Microsoft.Office.Interop.Excel.Application variable = (Microsoft.Office.Interop.Excel.Application)Activator.CreateInstance(Marshal.GetTypeFromCLSID(new Guid("00024500-0000-0000-C000-000000000046"))); Workbook variable1 = variable.Workbooks.Open(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); List strs = new List() { "codigo", "inicio", "fim", "fatura", "nome", "nascimento", "cpf", "matricula", "premio", "capital", "tipo", "observacao", "titulardependente" }; List dataTables = new List(); foreach (object worksheet in variable1.Worksheets) { DataTable dataTable = new DataTable(); Microsoft.Office.Interop.Excel.Range usedRange = ((Worksheet)worksheet).UsedRange; if (ItemViewModel.u003cu003eo__93.u003cu003ep__0 == null) { ItemViewModel.u003cu003eo__93.u003cu003ep__0 = CallSite>.Create(Binder.Convert(CSharpBinderFlags.None, typeof(object[,]), typeof(ItemViewModel))); } object[,] target = ItemViewModel.u003cu003eo__93.u003cu003ep__0.Target(ItemViewModel.u003cu003eo__93.u003cu003ep__0, usedRange.Value2); int num = 0; if (target == null) { continue; } for (int i = 0; i <= usedRange.Columns.Count - 1; i++) { if (target[1, i + 1] == null || !strs.Contains(Funcoes.RemoverAcentos(target[1, i + 1].ToString().ToLower().Trim()))) { num--; } else { DataColumn dataColumn = new DataColumn() { DataType = Type.GetType("System.String"), ColumnName = Funcoes.RemoverAcentos(target[1, i + 1].ToString().ToLower().Trim()) }; dataTable.Columns.Add(dataColumn); for (int j = 0; j <= usedRange.Rows.Count - 2; j++) { object obj = target[j + 2, i + 1]; target[1, i + 1].ToString().ToLower(); if (Funcoes.RemoverAcentos(target[1, i + 1].ToString().Trim().ToLower()) != "inicio" && Funcoes.RemoverAcentos(target[1, i + 1].ToString().Trim().ToLower()) != "fim" && Funcoes.RemoverAcentos(target[1, i + 1].ToString().Trim().ToLower()) != "nascimento") { string str = obj as string; shortDateString = (str != null ? Funcoes.RemoverAcentos(str).ToUpper().Trim() : (!(obj is double) ? (!(obj is bool) ? "" : ((bool)obj ? "1" : "0")) : ((double)obj).ToString(CultureInfo.InvariantCulture))); } else if (obj is double) { dateTime = DateTime.FromOADate((double)obj); shortDateString = dateTime.ToShortDateString(); } else if (!DateTime.TryParse((string)obj, out dateTime)) { shortDateString = ""; } else { dateTime = DateTime.Parse((string)obj); shortDateString = dateTime.ToShortDateString(); } if (i + num != 0) { dataTable.Rows[j][i + num] = shortDateString; } else { dataTable.Rows.Add(dataTable.NewRow()); dataTable.Rows[j][i + num] = shortDateString; } } } } dataTables.Add(dataTable); } variable.Quit(); List titularesVidas = new List(); foreach (DataTable dataTable1 in dataTables) { List titularesVidas1 = titularesVidas; EnumerableRowCollection dataRows = dataTable1.AsEnumerable(); Func u003cu003e9_931 = ItemViewModel.u003cu003ec.u003cu003e9__93_1; if (u003cu003e9_931 == null) { u003cu003e9_931 = (DataRow x) => { if (x.Field("nome") == null) { return false; } return !string.IsNullOrEmpty(x.Field("nome").ToString()); }; ItemViewModel.u003cu003ec.u003cu003e9__93_1 = u003cu003e9_931; } titularesVidas1.AddRange(dataRows.Where(u003cu003e9_931).ToList().Select((DataRow x) => { DateTime? nullable; decimal? nullable1; TipoTitular? nullable2; DateTime? nullable3; DateTime? nullable4; DateTime? nullable5; decimal? nullable6; decimal? nullable7; TipoTitular? nullable8; TitularesVida titularesVida = new TitularesVida(); titularesVida.set_Codigo((!dataTable1.Columns.Contains("codigo") || string.IsNullOrWhiteSpace(x.Field("codigo").ToString()) ? null : x.Field("codigo").ToString().ToUpper())); if (!dataTable1.Columns.Contains("inicio") || string.IsNullOrWhiteSpace(x.Field("inicio").ToString())) { nullable = null; nullable3 = nullable; } else { nullable3 = new DateTime?(DateTime.Parse(x.Field("inicio").ToString())); } titularesVida.set_Inicio(nullable3); if (!dataTable1.Columns.Contains("fim") | string.IsNullOrWhiteSpace(x.Field("fim").ToString())) { nullable = null; nullable4 = nullable; } else { nullable4 = new DateTime?(DateTime.Parse(x.Field("fim").ToString())); } titularesVida.set_Fim(nullable4); titularesVida.set_Fatura((!dataTable1.Columns.Contains("fatura") || string.IsNullOrWhiteSpace(x.Field("fatura").ToString()) ? null : x.Field("fatura").ToString().ToUpper())); titularesVida.set_Nome((!dataTable1.Columns.Contains("nome") || string.IsNullOrWhiteSpace(x.Field("nome").ToString()) ? null : x.Field("nome").ToString().ToUpper())); titularesVida.set_Observacao((!dataTable1.Columns.Contains("observacao") || string.IsNullOrWhiteSpace(x.Field("observacao").ToString()) ? null : x.Field("observacao").ToString().ToUpper())); if (!dataTable1.Columns.Contains("nascimento") || string.IsNullOrWhiteSpace(x.Field("nascimento").ToString())) { nullable = null; nullable5 = nullable; } else { nullable5 = new DateTime?(DateTime.Parse(x.Field("nascimento").ToString())); } titularesVida.set_Nascimento(nullable5); titularesVida.set_Cpf((!dataTable1.Columns.Contains("cpf") || string.IsNullOrWhiteSpace(x.Field("cpf").ToString()) ? null : x.Field("cpf").ToString())); titularesVida.set_Matricula((!dataTable1.Columns.Contains("matricula") || string.IsNullOrWhiteSpace(x.Field("matricula").ToString()) ? null : x.Field("matricula").ToString().ToUpper())); if (!dataTable1.Columns.Contains("premio") || string.IsNullOrWhiteSpace(x.Field("premio").ToString())) { nullable1 = null; nullable6 = nullable1; } else { nullable6 = new decimal?(decimal.Parse(x.Field("premio").ToString(), NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands, CultureInfo.InvariantCulture)); } titularesVida.set_Premio(nullable6); if (!dataTable1.Columns.Contains("capital") || string.IsNullOrWhiteSpace(x.Field("capital").ToString())) { nullable1 = null; nullable7 = nullable1; } else { nullable7 = new decimal?(decimal.Parse(x.Field("capital").ToString(), NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands, CultureInfo.InvariantCulture)); } titularesVida.set_Capital(nullable7); if (!dataTable1.Columns.Contains("tipo") || string.IsNullOrWhiteSpace(x.Field("tipo").ToString())) { nullable2 = null; nullable8 = nullable2; } else if (x.Field("tipo").ToString() == "SOCIO") { nullable8 = new TipoTitular?(0); } else if (x.Field("tipo").ToString() == "FUNCIONARIO") { nullable8 = new TipoTitular?(1); } else { nullable2 = null; nullable8 = nullable2; } titularesVida.set_Tipo(nullable8); return titularesVida; }).ToList()); } return titularesVidas; }); base.Loading(false); if (titularesVidas2.Count != 0) { ExtensionMethods.AddRange(this.Titulares, titularesVidas2); } else { await base.ShowMessage("NENHUMA COLUNA FOI ENCONTRADA", "OK", "", false); return; } } catch { num1 = 1; } if (num1 == 1) { base.Loading(false); await base.ShowMessage("FALHA AO IMPORTAR O EXCEL, VERIFIQUE SE O ARQUIVO POSSUI ALGUM ERRO.", "OK", "", false); } } public void IncluirTitular() { this.Titulares.Insert(0, new TitularesVida()); this.Titulares = new ObservableCollection(this.Titulares); } public async Task QuantidadeDeItens(long id) { return await (new ItemServico()).BuscarProximoItem(id); } public void RecheckAllSelected() { if (this._allSelectedChanging) { return; } try { this._allSelectedChanging = true; if (this.Titulares.All((TitularesVida e) => e.get_Selecionado())) { this.AllSelected = new bool?(true); } else if (!this.Titulares.All((TitularesVida e) => !e.get_Selecionado())) { this.AllSelected = null; } else { this.AllSelected = new bool?(false); } } finally { this._allSelectedChanging = false; } } private void TitularesOnPropertyChanged(object sender, PropertyChangedEventArgs args) { if (this._alterandoFiltros) { return; } this.RecheckAllSelected(); } } }