using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Diagnostics; using System.Globalization; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using Gestor.Application.Helpers; using Gestor.Application.Servicos.Seguros; using Gestor.Application.Servicos.Seguros.Itens; using Gestor.Application.ViewModels.Generic; using Gestor.Common.Validation; using Gestor.Model.Common; using Gestor.Model.Domain.Generic; using Gestor.Model.Domain.Seguros; namespace Gestor.Application.ViewModels.Ferramentas; public class EtiquetaViewModel : BaseSegurosViewModel { private bool _allSelectedChanging; private List _tiposEtiqueta = new List { "CLIENTE", "APÓLICE" }; private string _tipoEtiqueta = "CLIENTE"; private bool? _allSelected; private ObservableCollection _apolicesFiltrados = new ObservableCollection(); private bool _duasColunas = true; private bool _tresColunas; private bool _umaPagina; private bool _mostrarNascimento; private int _pular; private object _visibilityColunas; private bool _mostrarApolice = true; private bool _mostrarVendedor = true; private bool _mostrarItem = true; public List TiposEtiqueta { get { return _tiposEtiqueta; } set { _tiposEtiqueta = value; OnPropertyChanged("TiposEtiqueta"); } } public string TipoEtiqueta { get { return _tipoEtiqueta; } set { _tipoEtiqueta = value; OnPropertyChanged("TipoEtiqueta"); OnPropertyChanged("ExibirControlesEtiquetaApolice"); OnPropertyChanged("ExibirControlesEtiquetaCliente"); } } public Visibility ExibirControlesEtiquetaApolice { get { if (TipoEtiqueta == "APÓLICE") { return (Visibility)0; } return (Visibility)2; } } public Visibility ExibirControlesEtiquetaCliente { get { if (TipoEtiqueta == "CLIENTE") { return (Visibility)0; } return (Visibility)2; } } public bool? AllSelected { get { return _allSelected; } set { if (value != _allSelected) { _allSelected = value; AllSelectedChanged(); Apolices = new List(Apolices); OnPropertyChanged("AllSelected"); } } } public List Apolices { get; set; } public ObservableCollection ApolicesFiltrados { get { return _apolicesFiltrados; } set { _apolicesFiltrados = value; foreach (Documento apolicesFiltrado in ApolicesFiltrados) { apolicesFiltrado.PropertyChanged += EntryOnPropertyChanged; } OnPropertyChanged("ApolicesFiltrados"); } } public bool DuasColunas { get { return _duasColunas; } set { _duasColunas = value; OnPropertyChanged("DuasColunas"); } } public bool TresColunas { get { return _tresColunas; } set { _tresColunas = value; OnPropertyChanged("TresColunas"); } } public bool UmaPagina { get { return _umaPagina; } set { _umaPagina = value; VisibilityColunas = (object)(Visibility)(value ? 2 : 0); OnPropertyChanged("UmaPagina"); } } public bool MostrarNascimento { get { return _mostrarNascimento; } set { _mostrarNascimento = value; OnPropertyChanged("MostrarNascimento"); } } public int Pular { get { return _pular; } set { _pular = value; OnPropertyChanged("Pular"); } } public object VisibilityColunas { get { return _visibilityColunas; } set { _visibilityColunas = value; OnPropertyChanged("VisibilityColunas"); } } public bool MostrarApolice { get { return _mostrarApolice; } set { _mostrarApolice = value; OnPropertyChanged("MostrarApolice"); } } public bool MostrarVendedor { get { return _mostrarVendedor; } set { _mostrarVendedor = value; OnPropertyChanged("MostrarVendedor"); } } public bool MostrarItem { get { return _mostrarItem; } set { _mostrarItem = value; OnPropertyChanged("MostrarItem"); } } private void EntryOnPropertyChanged(object sender, PropertyChangedEventArgs args) { if (args.PropertyName == "Selecionado") { RecheckAllSelected(); } } private void RecheckAllSelected() { if (_allSelectedChanging) { return; } try { _allSelectedChanging = true; if (Apolices.All((Documento e) => e.Selecionado)) { AllSelected = true; } else if (Apolices.All((Documento e) => !e.Selecionado)) { AllSelected = false; } else { AllSelected = null; } } finally { _allSelectedChanging = false; } } private void AllSelectedChanged() { if (_allSelectedChanging) { return; } try { _allSelectedChanging = true; if (!AllSelected.HasValue) { return; } foreach (Documento apolice in Apolices) { apolice.Selecionado = AllSelected.Value; } } finally { _allSelectedChanging = false; } } public async Task CarregarDados(List list, bool apenasCliente) { List source = ((IEnumerable)list.ToList()).Select((Func)((Documento x) => new Cliente { Id = ((DomainBase)x.Controle.Cliente).Id })).ToList(); ClienteServico clienteServico = new ClienteServico(); ItemServico itemServico = new ItemServico(); List enderecos = await clienteServico.BuscarEnderecosPorCliente(source.Where((Cliente x) => ((DomainBase)x).Id > 0).ToList()); ObservableCollection observableCollection = ((!apenasCliente) ? (await itemServico.BuscarItens(list.ToList())) : null); ObservableCollection source2 = observableCollection; foreach (Documento x2 in list) { if (enderecos.Where((ClienteEndereco y) => ((DomainBase)y.Cliente).Id == ((DomainBase)x2.Controle.Cliente).Id).Count() == 0) { continue; } if (((DomainBase)x2.Controle.Cliente).Id > 0) { x2.Controle.Cliente.Enderecos = new ObservableCollection(from y in enderecos.Where((ClienteEndereco y) => ((DomainBase)y.Cliente).Id == ((DomainBase)x2.Controle.Cliente).Id).ToList() orderby y.Ordem select y); } if (!apenasCliente) { x2.ItensAtivo = source2.Where((Item y) => ((DomainBase)y.Documento).Id == ((DomainBase)x2).Id).ToList(); } } TipoEtiqueta = (apenasCliente ? "CLIENTE" : "APÓLICE"); Apolices = (from x in list.Where(delegate(Documento x) { Cliente cliente = x.Controle.Cliente; if (((cliente != null) ? cliente.Enderecos : null) != null) { Cliente cliente2 = x.Controle.Cliente; if (cliente2 == null) { return false; } return cliente2.Enderecos.Count() > 0; } return false; }) orderby x.Controle.Cliente.Nome select x).ToList(); ApolicesFiltrados = new ObservableCollection(Apolices); AllSelected = true; RecheckAllSelected(); } public void EmitirEtiquetas() { string text = ""; List list = Apolices.Where((Documento x) => x.Selecionado).ToList(); string tipoEtiqueta = TipoEtiqueta; if (!(tipoEtiqueta == "CLIENTE")) { if (tipoEtiqueta == "APÓLICE") { if (UmaPagina) { text = "ETIQUETAS"; for (int i = 0; i < list.Count; i++) { text += "
" : "30%'>"); text += "
"; text += ""; text += ""; text = text + ""; text += ""; text += "
"; Documento val = list[i]; string[] obj = new string[7] { text, val.Controle.Cliente.Nome.Trim(), "
", null, null, null, null }; object obj2; if (!MostrarItem) { obj2 = null; } else { if (val.ItensAtivo.Count <= 1) { Item? obj3 = val.ItensAtivo.FirstOrDefault(); obj2 = ((obj3 != null) ? obj3.Descricao.ToUpper() : null) + "
"; } else { obj2 = "APÓLICE COLETIVA
"; } if (obj2 == null) { obj2 = ""; } } obj[3] = (string)obj2; obj[4] = (MostrarApolice ? (((val.Apolice == "PROSPECÇÃO") ? $"{val.Vigencia2:d} - APÓLICE: {val.Apolice}
" : ($"{val.Vigencia1:d} - {val.Vigencia2:d} - APÓLICE: {val.Apolice}" + (string.IsNullOrEmpty(val.Endosso) ? "
" : (" /" + val.Endosso + "
")))) ?? "") : null); obj[5] = ((val.Apolice == "PROSPECÇÃO") ? "" : (val.Controle.Seguradora.Nome + " - " + val.Controle.Ramo.Nome + "
")); obj[6] = (MostrarVendedor ? (((val.VendedorPrincipal == null) ? "" : ("VENDEDOR: " + val.VendedorPrincipal.Nome)) ?? "") : null); text = string.Concat(obj); text += "
"; text += "
"; text += "
"; } } else { int num = 0; int num2 = 0; double num3 = 0.0; if (DuasColunas) { num = 10; num2 = 2; num3 = 10.52; } else if (TresColunas) { num = 10; num2 = 3; num3 = 6.87; } Pular %= num * num2; int pular = Pular; text = "ETIQUETAS
"; int num4 = 0; while (num4 < list.Count) { text += "
"; text = text + ""; for (int j = 0; j < num * num2; j += num2) { text += ""; for (int k = 0; k < num2; k++) { text = text + ""; } } text += "
"; if (j + k >= Pular) { if (num4 < list.Count) { Documento val2 = list[num4]; string[] obj4 = new string[7] { text, val2.Controle.Cliente.Nome.Trim(), "
", null, null, null, null }; object obj5; if (!MostrarItem) { obj5 = null; } else { if (val2.ItensAtivo.Count <= 1) { Item? obj6 = val2.ItensAtivo.FirstOrDefault(); obj5 = ((obj6 != null) ? obj6.Descricao.ToUpper() : null) + "
"; } else { obj5 = "APÓLICE COLETIVA
"; } if (obj5 == null) { obj5 = ""; } } obj4[3] = (string)obj5; obj4[4] = ((!MostrarApolice) ? null : (((val2.Apolice == "PROSPECÇÃO") ? $"{val2.Vigencia2:d} - APÓLICE: {val2.Apolice}
" : ($"{val2.Vigencia1:d} - {val2.Vigencia2:d} - APÓLICE: {val2.Apolice}" + (string.IsNullOrEmpty(val2.Endosso) ? "
" : (" /" + val2.Endosso + "
")))) ?? "")); obj4[5] = ((val2.Apolice == "PROSPECÇÃO") ? "" : (val2.Controle.Seguradora.Nome + " - " + val2.Controle.Ramo.Nome + "
")); obj4[6] = ((!MostrarVendedor) ? null : (((val2.VendedorPrincipal == null) ? "" : ("VENDEDOR: " + val2.VendedorPrincipal.Nome)) ?? "")); text = string.Concat(obj4); num4++; } Pular = 0; } text += "
"; text += "
"; } Pular = (pular + list.Count) % (num * num2); } RegistrarAcao(string.Format("EMITIU ETIQUETA DE {0} DOCUMENTO{1}", list.Count, (list.Count == 1) ? "" : "S"), 0L, (TipoTela)59, "IDS DOS DOCUMENTOS:\n" + string.Join(", ", list.Select((Documento x) => ((DomainBase)x).Id))); } } else { list = Apolices.Where((Documento x) => x.Selecionado && x.Controle.Cliente.Enderecos != null).ToList(); if (UmaPagina) { text = "ETIQUETAS"; for (int l = 0; l < list.Count; l++) { text += "
" : "30%'>"); text += "
"; text += ""; text += ""; text = text + ""; text += ""; text += "
"; Documento val3 = list[l]; if (MostrarNascimento) { val3.Controle.Cliente.Nascimento = new ClienteServico().BuscarNascimento(((DomainBase)val3.Controle.Cliente).Id); } string[] obj7 = new string[9] { text, val3.Controle.Cliente.Nome.Trim(), " ", (!val3.Controle.Cliente.Nascimento.HasValue || !MostrarNascimento) ? "" : val3.Controle.Cliente.Nascimento?.ToString("dd/MM"), "
", null, null, null, null }; object obj16; if (((DomainBase)val3.Controle.Cliente).Id != 0L) { string[] array = new string[10]; ClienteEndereco? obj8 = val3.Controle.Cliente.Enderecos.OrderBy((ClienteEndereco x) => x.Ordem).FirstOrDefault(); array[0] = ((obj8 == null) ? null : ((EnderecoBase)obj8).Endereco?.Trim()); array[1] = ", "; ClienteEndereco? obj9 = val3.Controle.Cliente.Enderecos.FirstOrDefault(); array[2] = ((obj9 == null) ? null : ((EnderecoBase)obj9).Numero?.Trim()); ClienteEndereco? obj10 = val3.Controle.Cliente.Enderecos.FirstOrDefault(); object obj11; if (string.IsNullOrWhiteSpace((obj10 != null) ? ((EnderecoBase)obj10).Complemento : null)) { obj11 = ""; } else { ClienteEndereco? obj12 = val3.Controle.Cliente.Enderecos.FirstOrDefault(); obj11 = "
" + ((obj12 == null) ? null : ((EnderecoBase)obj12).Complemento?.Trim()); } array[3] = (string)obj11; array[4] = "
"; ClienteEndereco? obj13 = val3.Controle.Cliente.Enderecos.FirstOrDefault(); array[5] = ((obj13 == null) ? null : ((EnderecoBase)obj13).Bairro?.Trim()); array[6] = " - "; ClienteEndereco? obj14 = val3.Controle.Cliente.Enderecos.FirstOrDefault(); array[7] = ((obj14 == null) ? null : ((EnderecoBase)obj14).Cidade?.Trim()); array[8] = "/"; ClienteEndereco? obj15 = val3.Controle.Cliente.Enderecos.FirstOrDefault(); array[9] = ((obj15 == null) ? null : ((EnderecoBase)obj15).Estado?.Trim()); obj16 = string.Concat(array); } else { obj16 = ""; } obj7[5] = (string)obj16; obj7[6] = "
"; object obj18; if (((DomainBase)val3.Controle.Cliente).Id != 0L) { ClienteEndereco? obj17 = val3.Controle.Cliente.Enderecos.OrderBy((ClienteEndereco x) => x.Ordem).FirstOrDefault(); obj18 = ((obj17 != null) ? ((EnderecoBase)obj17).Cep.Trim() : null) ?? ""; } else { obj18 = ""; } obj7[7] = (string)obj18; obj7[8] = "
"; text = string.Concat(obj7); text += "
"; text += "
"; text += "
"; } } else { int num5 = 0; int num6 = 0; double num7 = 0.0; if (DuasColunas) { num5 = 10; num6 = 2; num7 = 10.52; } else if (TresColunas) { num5 = 10; num6 = 3; num7 = 6.82; } Pular %= num5 * num6; int pular2 = Pular; text = "ETIQUETAS
"; int num8 = 0; while (num8 < list.Count) { text += "
"; bool flag = num8 == 0 || num8 == num5 * num6; text = text + ""; for (int m = 0; m < num5 * num6; m += num6) { text += ""; if (DuasColunas) { text += ""; for (int n = 0; n < num6; n++) { text = text + ""; if (n < num6 - 1) { if (DuasColunas) { text += ""; } } text += "
"; } else if (TresColunas) { text += ""; } text += ""; if (m + n >= Pular) { if (num8 < list.Count) { Documento val4 = list[num8]; if (MostrarNascimento) { val4.Controle.Cliente.Nascimento = new ClienteServico().BuscarNascimento(((DomainBase)val4.Controle.Cliente).Id); } if (val4.Controle.Cliente.Enderecos.Count() > 0) { string[] obj19 = new string[8] { text, val4.Controle.Cliente.Nome.Trim(), " ", (!val4.Controle.Cliente.Nascimento.HasValue || !MostrarNascimento) ? "" : val4.Controle.Cliente.Nascimento?.ToString("dd/MM"), "
", null, null, null }; object obj32; if (((DomainBase)val4.Controle.Cliente).Id != 0L) { string[] array2 = new string[10]; ClienteEndereco? obj20 = val4.Controle.Cliente.Enderecos.FirstOrDefault(); array2[0] = ((obj20 == null) ? null : ((EnderecoBase)obj20).Endereco?.Trim()); array2[1] = ", "; ClienteEndereco? obj21 = val4.Controle.Cliente.Enderecos.FirstOrDefault(); array2[2] = ((obj21 == null) ? null : ((EnderecoBase)obj21).Numero?.Trim()); ClienteEndereco? obj22 = val4.Controle.Cliente.Enderecos.FirstOrDefault(); object obj23; if (string.IsNullOrWhiteSpace((obj22 != null) ? ((EnderecoBase)obj22).Complemento : null)) { obj23 = ""; } else { ClienteEndereco? obj24 = val4.Controle.Cliente.Enderecos.FirstOrDefault(); obj23 = "
" + ((obj24 != null) ? ((EnderecoBase)obj24).Complemento.Trim() : null); } array2[3] = (string)obj23; array2[4] = "
"; ClienteEndereco? obj25 = val4.Controle.Cliente.Enderecos.FirstOrDefault(); array2[5] = ((obj25 == null) ? null : ((EnderecoBase)obj25).Bairro?.Trim()); array2[6] = " - "; ClienteEndereco? obj26 = val4.Controle.Cliente.Enderecos.FirstOrDefault(); object obj27; if (string.IsNullOrEmpty((obj26 != null) ? ((EnderecoBase)obj26).Cidade : null)) { obj27 = ""; } else { ClienteEndereco? obj28 = val4.Controle.Cliente.Enderecos.FirstOrDefault(); obj27 = ((obj28 != null) ? ((EnderecoBase)obj28).Cidade.Trim() : null); } array2[7] = (string)obj27; array2[8] = "/"; ClienteEndereco? obj29 = val4.Controle.Cliente.Enderecos.FirstOrDefault(); object obj30; if (string.IsNullOrEmpty((obj29 != null) ? ((EnderecoBase)obj29).Estado : null)) { obj30 = ""; } else { ClienteEndereco? obj31 = val4.Controle.Cliente.Enderecos.FirstOrDefault(); obj30 = ((obj31 != null) ? ((EnderecoBase)obj31).Estado.Trim() : null); } array2[9] = (string)obj30; obj32 = string.Concat(array2); } else { obj32 = ""; } obj19[5] = (string)obj32; obj19[6] = "
"; object obj34; if (((DomainBase)val4.Controle.Cliente).Id != 0L) { ClienteEndereco? obj33 = val4.Controle.Cliente.Enderecos.FirstOrDefault(); obj34 = ((obj33 != null) ? ((EnderecoBase)obj33).Cep.Trim() : null) ?? ""; } else { obj34 = ""; } obj19[7] = (string)obj34; text = string.Concat(obj19); } num8++; } Pular = 0; } text += "
"; } else if (TresColunas) { text += ""; } } text += "
"; text += "
"; } Pular = (pular2 + list.Count) % (num5 * num6); } RegistrarAcao(string.Format("EMITIU ETIQUETA DE {0} CLIENTE{1}", list.Count, (list.Count == 1) ? "" : "S"), 0L, (TipoTela)59, "IDS E NOMES DOS CLIENTES:\n" + string.Join("\n", list.Select((Documento x) => ((DomainBase)x.Controle.Cliente).Id + ": \"" + x.Controle.Cliente.Nome + "\""))); } text += ""; string tempPath = Path.GetTempPath(); string text2 = $"{tempPath}{(object)(TipoExtrato)0}_{Funcoes.GetNetworkTime():ddMMyyyyhhmmss}.html"; StreamWriter streamWriter = new StreamWriter(text2, append: true, Encoding.UTF8); streamWriter.Write(text); streamWriter.Close(); Process.Start(text2); } internal async Task> Filtrar(string value) { return await Task.Run(() => FiltrarApolice(value)); } public List FiltrarApolice(string filter) { ApolicesFiltrados = (string.IsNullOrWhiteSpace(filter) ? new ObservableCollection(Apolices) : new ObservableCollection(from x in Apolices where ValidationHelper.RemoveDiacritics(x.Controle.Cliente.Nome.Trim()).ToUpper().Contains(ValidationHelper.RemoveDiacritics(filter)) orderby x.Controle.Cliente.Nome descending, ((DomainBase)x).Id select x)); return ApolicesFiltrados.ToList(); } }