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.Reflection; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows; using System.Windows.Forms; using System.Windows.Media; using ClosedXML.Excel; using Gestor.Application.Componentes; using Gestor.Application.Helpers; using Gestor.Application.Model; using Gestor.Application.ViewModels.Generic; using Gestor.Common.Helpers; using Gestor.Model.Attributes; using Gestor.Model.Common; using Gestor.Model.Domain.Configuracoes; using Gestor.Model.Domain.Relatorios; using LiveCharts; using LiveCharts.Definitions.Series; using LiveCharts.Helpers; using LiveCharts.Wpf; using NReco.PdfGenerator; namespace Gestor.Application.ViewModels.Relatorios; public class SinteticoViewModel : BaseSegurosViewModel { private Visibility _unicaPaginaVisibility = (Visibility)2; private Geometry _maximizeRestore = Geometry.Parse((string)Application.Current.Resources[(object)"Restore"]); private ObservableCollection _series; private string _titulotela = $"RELATÓRIO SINTÉTICO | VERSÃO GESTOR {ApplicationHelper.Versao}"; public Visibility UnicaPaginaVisibility { get { //IL_0001: Unknown result type (might be due to invalid IL or missing references) return _unicaPaginaVisibility; } set { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0002: Unknown result type (might be due to invalid IL or missing references) _unicaPaginaVisibility = value; OnPropertyChanged("UnicaPaginaVisibility"); } } public string Relatorio { get; set; } public Geometry MaximizeRestore { get { return _maximizeRestore; } set { _maximizeRestore = value; OnPropertyChanged("MaximizeRestore"); } } public ObservableCollection Series { get { return _series; } set { _series = value; OnPropertyChanged("Series"); } } public string Titulotela { get { return _titulotela; } set { _titulotela = value; OnPropertyChanged("Titulotela"); } } public SinteticoViewModel(List sintetico, string relatorio) { //IL_0002: Unknown result type (might be due to invalid IL or missing references) Relatorio = relatorio; GerarSintetico(sintetico); } private void GerarSintetico(List sintetico) { //IL_00d6: Unknown result type (might be due to invalid IL or missing references) //IL_00e0: Expected O, but got Unknown //IL_0117: Unknown result type (might be due to invalid IL or missing references) //IL_0169: Unknown result type (might be due to invalid IL or missing references) //IL_016e: Unknown result type (might be due to invalid IL or missing references) //IL_0180: Unknown result type (might be due to invalid IL or missing references) //IL_0189: Expected O, but got Unknown //IL_02f2: Unknown result type (might be due to invalid IL or missing references) //IL_02f7: Unknown result type (might be due to invalid IL or missing references) //IL_03f4: Unknown result type (might be due to invalid IL or missing references) //IL_03f9: Unknown result type (might be due to invalid IL or missing references) //IL_0203: Unknown result type (might be due to invalid IL or missing references) //IL_0208: Unknown result type (might be due to invalid IL or missing references) //IL_031e: Unknown result type (might be due to invalid IL or missing references) //IL_0356: Unknown result type (might be due to invalid IL or missing references) //IL_0361: Unknown result type (might be due to invalid IL or missing references) //IL_0420: Unknown result type (might be due to invalid IL or missing references) //IL_0449: Unknown result type (might be due to invalid IL or missing references) //IL_0451: Unknown result type (might be due to invalid IL or missing references) //IL_045c: Unknown result type (might be due to invalid IL or missing references) //IL_022f: Unknown result type (might be due to invalid IL or missing references) //IL_0257: Unknown result type (might be due to invalid IL or missing references) //IL_025f: Unknown result type (might be due to invalid IL or missing references) //IL_036f: Unknown result type (might be due to invalid IL or missing references) //IL_037c: Expected O, but got Unknown //IL_046a: Unknown result type (might be due to invalid IL or missing references) //IL_0477: Expected O, but got Unknown //IL_026d: Unknown result type (might be due to invalid IL or missing references) //IL_027a: Expected O, but got Unknown //IL_071b: Unknown result type (might be due to invalid IL or missing references) //IL_0720: Unknown result type (might be due to invalid IL or missing references) //IL_072b: Unknown result type (might be due to invalid IL or missing references) //IL_0733: Unknown result type (might be due to invalid IL or missing references) //IL_073e: Unknown result type (might be due to invalid IL or missing references) //IL_0746: Unknown result type (might be due to invalid IL or missing references) //IL_0589: Unknown result type (might be due to invalid IL or missing references) //IL_058e: Unknown result type (might be due to invalid IL or missing references) //IL_0599: Unknown result type (might be due to invalid IL or missing references) //IL_05a6: Unknown result type (might be due to invalid IL or missing references) //IL_05ae: Unknown result type (might be due to invalid IL or missing references) //IL_076e: Unknown result type (might be due to invalid IL or missing references) //IL_077e: Expected O, but got Unknown //IL_05cd: Unknown result type (might be due to invalid IL or missing references) //IL_05dd: Expected O, but got Unknown UnicaPaginaVisibility = (Visibility)((!(Relatorio == "RELATÓRIO DE FECHAMENTO")) ? 2 : 0); Series = new ObservableCollection(); PropertyInfo[] properties = ((object)sintetico.First()).GetType().GetProperties(); foreach (PropertyInfo parentProperty in properties) { if (parentProperty.Name == "ValidationEvent" || parentProperty.Name == "Agrupamento" || sintetico.All((Sintetico s) => ((object)s).GetType().GetProperty(parentProperty.Name)?.GetValue(s, null) == null)) { continue; } object obj = parentProperty.GetCustomAttributes(typeof(DescriptionAttribute), inherit: true).FirstOrDefault(); if (obj == null) { continue; } string description = ((DescriptionAttribute)obj).Description; SinteticoSource sinteticoSource = new SinteticoSource { Titulo = description, Colecao = new SeriesCollection(), Lista = new ObservableCollection() }; object obj2 = parentProperty.GetCustomAttributes(typeof(TipoAttribute), inherit: true).FirstOrDefault(); string text = ""; if (obj2 != null) { text = ((TipoAttribute)obj2).Description; } sintetico = sintetico.OrderByDescending((Sintetico x) => parentProperty.GetValue(x, null)).ToList(); foreach (Sintetico x2 in sintetico) { bool flag = false; PieSeries val = new PieSeries { Title = x2.Agrupamento, DataLabels = false }; IChartValues values; if (!(text == "PERCENTUAL")) { if (!(text == "VALOR")) { flag = (int)parentProperty.GetValue(x2) < 0; ChartValues obj3 = new ChartValues(); ((NoisyCollection)(object)obj3).Add(Math.Abs((int)parentProperty.GetValue(x2))); values = (IChartValues)(object)obj3; sinteticoSource.Lista.Add(new ValorSintetico { Indice = x2.Agrupamento + (flag ? " (-)" : ""), Valor = decimal.Parse(parentProperty.GetValue(x2).ToString()), Formato = text, Sinal = (Sinal)(flag ? 1 : 0), NomeRelatorio = description }); ((Series)val).LabelPoint = (ChartPoint chartPoint) => ((int)parentProperty.GetValue(x2)).ToString("n"); } else { flag = (decimal)parentProperty.GetValue(x2) < 0m; ChartValues obj4 = new ChartValues(); ((NoisyCollection)(object)obj4).Add(Math.Abs((decimal)parentProperty.GetValue(x2))); values = (IChartValues)(object)obj4; sinteticoSource.Lista.Add(new ValorSintetico { Indice = x2.Agrupamento + (flag ? " (-)" : ""), Valor = Math.Round((decimal)parentProperty.GetValue(x2), 2), Formato = text, Unidate = "R$", Sinal = (Sinal)(flag ? 1 : 0), NomeRelatorio = description }); ((Series)val).LabelPoint = (ChartPoint chartPoint) => ((decimal)parentProperty.GetValue(x2)).ToString("n2"); } } else { flag = (decimal)parentProperty.GetValue(x2) < 0m; ChartValues obj5 = new ChartValues(); ((NoisyCollection)(object)obj5).Add(Math.Abs((decimal)parentProperty.GetValue(x2))); values = (IChartValues)(object)obj5; sinteticoSource.Lista.Add(new ValorSintetico { Indice = x2.Agrupamento + (flag ? " (-)" : ""), Valor = Math.Round((decimal)parentProperty.GetValue(x2) * 0.01m, 2), Unidate = "%", Sinal = (Sinal)(flag ? 1 : 0), NomeRelatorio = description }); ((Series)val).LabelPoint = (ChartPoint chartPoint) => ((decimal)parentProperty.GetValue(x2)).ToString("n2"); } ((Series)val).Values = values; ((NoisyCollection)(object)sinteticoSource.Colecao).Add((ISeriesView)(object)val); } if (obj2 != null && !(text == "PERCENTUAL")) { if (!(text == "VALOR")) { if (!Relatorio.Equals("RELATÓRIO DE FECHAMENTO")) { int num = sintetico.Sum((Sintetico x) => (int)parentProperty.GetValue(x, null)); if (num != 0) { foreach (ValorSintetico listum in sinteticoSource.Lista) { listum.Porcentagem = (100m * listum.Valor / (decimal)num).ToString("F") + "%"; } } sinteticoSource.Lista.Add(new ValorSintetico { Indice = "TOTAL", Valor = num, Formato = text, Sinal = (Sinal)(sintetico.Sum((Sintetico x) => (int)parentProperty.GetValue(x, null)) < 0), Porcentagem = "100%" }); } else { int num2 = sintetico.Sum((Sintetico x) => (int)parentProperty.GetValue(x, null)); if (num2 != 0) { foreach (ValorSintetico listum2 in sinteticoSource.Lista) { listum2.Porcentagem = (100m * listum2.Valor / (decimal)num2).ToString("F") + "%"; } } } } else if (!Relatorio.Equals("RELATÓRIO DE FECHAMENTO")) { decimal num3 = Math.Round(sintetico.Sum((Sintetico x) => (decimal)parentProperty.GetValue(x, null)), 2); if (num3 != 0m) { foreach (ValorSintetico listum3 in sinteticoSource.Lista) { listum3.Porcentagem = (100m * listum3.Valor / num3).ToString("F") + "%"; } } sinteticoSource.Lista.Add(new ValorSintetico { Indice = "TOTAL", Valor = num3, Unidate = "R$", Formato = text, Sinal = (Sinal)((sintetico.Sum((Sintetico x) => (decimal)parentProperty.GetValue(x, null)) < 0m) ? 1 : 0), Porcentagem = "100%" }); } else { decimal num4 = Math.Round(sintetico.Sum((Sintetico x) => (decimal)parentProperty.GetValue(x, null)), 2); if (num4 != 0m) { foreach (ValorSintetico listum4 in sinteticoSource.Lista) { listum4.Porcentagem = (100m * listum4.Valor / num4).ToString("F") + "%"; } } } } Series.Add(sinteticoSource); } } public async Task Print(SinteticoSource sintetico) { string grafico = Funcoes.GerarGrafico(sintetico.Lista.ToList()); string dados = await Funcoes.GenerateTable(GerarRelacao(sintetico.Lista.ToList()), new List(), grafico: true); string value = Funcoes.ExportarHtml(new TipoRelatorio { Nome = sintetico.Titulo, Inicio = (sintetico.DateStart ?? DateTime.MinValue), Fim = (sintetico.DateFinal ?? DateTime.MinValue) }, dados, "60", "landscape", search: false, grafico); string tempPath = Path.GetTempPath(); string text = string.Format("{0}{1}_{2:ddMMyyyyhhmmss}.html", tempPath, new Regex("[" + Regex.Escape(new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars())) + "]").Replace(Relatorio, ""), Funcoes.GetNetworkTime()); StreamWriter streamWriter = new StreamWriter(text, append: true, Encoding.UTF8); streamWriter.Write(value); streamWriter.Close(); Process.Start(text); } public async Task PrintUnica(SinteticoSource sintetico) { List> list = new List>(); foreach (SinteticoSource item in Series) { list.Add(item.Lista.ToList()); } string grafico = Funcoes.GerarGraficoUnico(list); string dados = await Funcoes.GenerateMultipleTable(GerarRelacao(list), new List(), grafico: true); string value = Funcoes.ExportarMultipleHtml(new TipoRelatorio { Nome = "RELATÓRIOS", Inicio = (sintetico.DateStart ?? DateTime.MinValue), Fim = (sintetico.DateFinal ?? DateTime.MinValue) }, dados, "60", "landscape", search: false, grafico); string tempPath = Path.GetTempPath(); string text = string.Format("{0}{1}_{2:ddMMyyyyhhmmss}.html", tempPath, new Regex("[" + Regex.Escape(new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars())) + "]").Replace(Relatorio, ""), Funcoes.GetNetworkTime()); StreamWriter streamWriter = new StreamWriter(text, append: true, Encoding.UTF8); streamWriter.Write(value); streamWriter.Close(); Process.Start(text); } private static List GerarRelacao(List lista) { return ((IEnumerable)lista).Select((Func)((ValorSintetico x) => new Listagem { Agrupamento = x.Indice, Valor = ((x.Formato == "VALOR") ? x.Valor.ToString("c2") : ((x.Formato == "PERCENTUAL") ? x.Valor.ToString("p2") : x.Valor.ToString(new CultureInfo("pt-BR")))), NomeRelatorio = x.NomeRelatorio })).ToList(); } private static List> GerarRelacao(List> listas) { List> list = new List>(); foreach (List lista in listas) { list.Add(GerarRelacao(lista)); } return list; } public async Task GerarPdf(SinteticoSource sintetico) { string dados = await Funcoes.GenerateTable(GerarRelacao(sintetico.Lista.ToList()), new List()); string text = Funcoes.ExportarHtml(new TipoRelatorio { Nome = sintetico.Titulo, Inicio = DateTime.MinValue, Fim = DateTime.MinValue }, dados); NRecoHtmlToPdfConverter nRecoHtmlToPdfConverter = new NRecoHtmlToPdfConverter(); ((HtmlToPdfConverter)nRecoHtmlToPdfConverter).Orientation = (PageOrientation)1; ((HtmlToPdfConverter)nRecoHtmlToPdfConverter).Zoom = 0.5f; byte[] bytes = ((HtmlToPdfConverter)nRecoHtmlToPdfConverter).GeneratePdf(text); SaveFileDialog sfd = new SaveFileDialog(); try { ((FileDialog)sfd).Filter = "All Files|*.*"; ((FileDialog)sfd).FileName = $"RELATORIO FINANCEIRO_{Funcoes.GetNetworkTime():ddMMyyyyhhmmss}"; if (1 != (int)((CommonDialog)sfd).ShowDialog()) { return; } if (File.Exists(((FileDialog)sfd).FileName + ".pdf")) { await ShowMessage("JÁ EXISTE UM ARQUIVO COM O NOME DE " + ((FileDialog)sfd).FileName + ".pdf NA PASTA SELECIONADA. " + Environment.NewLine + "TENTE NOVAMENTE EM OUTRA PASTA."); return; } File.WriteAllBytes(((FileDialog)sfd).FileName + ".pdf", bytes); Process.Start(((FileDialog)sfd).FileName + ".pdf"); } finally { ((IDisposable)sfd)?.Dispose(); } } public async Task GerarExcel(SinteticoSource sintetico) { List analitico = GerarRelacao(sintetico.Lista.ToList()); string text = ""; string fileName; if (Recursos.Configuracoes.Any((ConfiguracaoSistema x) => (int)x.Configuracao == 41)) { FolderBrowserDialog val = new FolderBrowserDialog(); try { if (1 != (int)((CommonDialog)val).ShowDialog()) { return; } text = val.SelectedPath + "\\"; Directory.CreateDirectory(text); } finally { ((IDisposable)val)?.Dispose(); } fileName = text + "SINTETICO " + Functions.GetNetworkTime().Date.ToShortDateString().Replace("/", "") + ".xlsx"; } else { text = Path.GetTempPath(); fileName = $"{text}{Guid.NewGuid()}.xlsx"; } (await Funcoes.GerarXls(new XLWorkbook(), "SINTÉTICO", analitico)).SaveAs(fileName); Process.Start(fileName); } }