summaryrefslogtreecommitdiff
path: root/Codemerx/Gestor.Application/ViewModels/AnaliticoViewModel.cs
blob: bdf394b7e411695ea1e7aeea1ab6afa9f7a1ee85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using Gestor.Application.Helpers;
using Gestor.Application.Servicos;
using Gestor.Application.Servicos.Generic;
using Gestor.Application.ViewModels.Generic;
using Gestor.Model.Common;
using Gestor.Model.Domain.BI;
using Gestor.Model.Domain.Ferramentas;
using Gestor.Model.Domain.Generic;
using Gestor.Model.Domain.Relatorios;
using Gestor.Model.Domain.Relatorios.ClientesAtivosInativos;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace Gestor.Application.ViewModels
{
	internal class AnaliticoViewModel : BaseSegurosViewModel
	{
		private string _head;

		public string Head
		{
			get
			{
				return this._head;
			}
			set
			{
				this._head = value;
				base.OnPropertyChanged("Head");
			}
		}

		public TipoTela Tela { get; set; } = 1;

		public string Tipo
		{
			get;
			set;
		}

		public AnaliticoViewModel(string tipo)
		{
			this.Tipo = tipo;
			string str = this.Tipo;
			if (str == "PARCELAS")
			{
				this.Tela = 5;
				return;
			}
			if (str != "APOLICES")
			{
				this.Tela = 1;
				return;
			}
			this.Tela = 2;
		}

		private async Task<string> GerarHtml(DataGrid grid)
		{
			PropertyInfo[] properties = typeof(Analitico).GetProperties();
			List<string> list = (
				from x in (IEnumerable<PropertyInfo>)properties
				select x.Name).ToList<string>();
			List<string> strs = new List<string>();
			for (int i = 0; i < grid.Columns.Count; i++)
			{
				if (!(grid.Columns[i] is DataGridTemplateColumn))
				{
					Binding binding = (grid.Columns[i] as DataGridBoundColumn).Binding as Binding;
					strs.Add(binding.Path.Path);
				}
			}
			List<string> list1 = list.Except<string>(strs).ToList<string>();
			IEnumerable<Analitico> analiticos = grid.Items.Cast<Analitico>();
			string str = await Funcoes.GenerateTable<Analitico>((
				from x in analiticos
				where x.get_Selecionado()
				select x).ToList<Analitico>(), list1, false, true, "", null);
			string str1 = Funcoes.CreateCard("", str, false);
			TipoRelatorio tipoRelatorio = new TipoRelatorio();
			tipoRelatorio.set_Nome(string.Concat("ANALÍTICO - ", this.Head));
			string str2 = str1;
			if (str2 == null)
			{
				str2 = "";
			}
			string str3 = Funcoes.ExportarHtml(tipoRelatorio, str2, "60", "landscape", false, "");
			return str3;
		}

		public async Task Print(DataGrid grid)
		{
			IEnumerable<Analitico> analiticos = grid.Items.Cast<Analitico>();
			if (analiticos.Any<Analitico>((Analitico x) => x.get_Selecionado()))
			{
				string str = await this.GerarHtml(grid);
				if (!string.IsNullOrEmpty(str))
				{
					string tempPath = Path.GetTempPath();
					string str1 = string.Format("{0}{1}_{2:ddMMyyyyhhmmss}.html", tempPath, (new Regex(string.Concat("[", Regex.Escape(string.Concat(new string(Path.GetInvalidFileNameChars()), new string(Path.GetInvalidPathChars()))), "]"))).Replace(this.Head, ""), Funcoes.GetNetworkTime());
					StreamWriter streamWriter = new StreamWriter(str1, true, Encoding.UTF8);
					streamWriter.Write(str);
					streamWriter.Close();
					Process.Start(str1);
				}
			}
			else
			{
				await base.ShowMessage("É NECESSÁRIO SELECIONAR AO MENOS UM DOCUMENTO", "OK", "", false);
			}
		}

		public async Task<Trilha> Salvar(Trilha destino)
		{
			Trilha trilha;
			List<Fase> fases = destino.get_Fases();
			TarefaServico tarefaServico = new TarefaServico();
			Trilha trilha1 = await tarefaServico.Salvar(destino);
			if (tarefaServico.Sucesso)
			{
				fases.ForEach((Fase x) => x.set_Trilha(trilha1));
				fases = await tarefaServico.Salvar(fases);
				if (tarefaServico.Sucesso)
				{
					trilha1.set_Fases(fases);
					trilha = trilha1;
				}
				else
				{
					trilha = null;
				}
			}
			else
			{
				trilha = null;
			}
			fases = null;
			tarefaServico = null;
			return trilha;
		}

		public async Task Save(List<Tarefa> tarefas)
		{
			await (new TarefaServico()).Salvar(tarefas);
		}

		public async Task<List<Analitico>> Validar(List<Analitico> analitico, Trilha trilha)
		{
			List<long> list;
			List<Analitico> analiticos;
			TarefaServico tarefaServico = new TarefaServico();
			string tipo = this.Tipo;
			if (tipo == "CLIENTES")
			{
				List<Analitico> analiticos1 = analitico;
				list = (
					from x in analiticos1
					select x.get_Cliente().get_Id()).ToList<long>();
			}
			else if (tipo == "PARCELAS" || tipo == "COMISSÃO")
			{
				List<Analitico> analiticos2 = analitico;
				list = (
					from x in analiticos2
					select x.get_Parcela().get_Id()).ToList<long>();
			}
			else
			{
				List<Analitico> analiticos3 = analitico;
				list = (
					from x in analiticos3
					select x.get_Documento().get_Id()).ToList<long>();
			}
			List<long> nums = await tarefaServico.Validar(list, trilha.get_Id());
			List<long> nums1 = nums;
			tipo = this.Head;
			if (tipo == "CLIENTES")
			{
				analiticos = (
					from x in analitico
					where nums1.Contains(x.get_Cliente().get_Id())
					select x).ToList<Analitico>();
			}
			else
			{
				analiticos = (tipo == "PARCELAS" || tipo == "COMISSÃO" ? (
					from x in analitico
					where nums1.Contains(x.get_Parcela().get_Id())
					select x).ToList<Analitico>() : (
					from x in analitico
					where nums1.Contains(x.get_Documento().get_Id())
					select x).ToList<Analitico>());
			}
			List<Analitico> list1 = analitico.Except<Analitico>(analiticos).ToList<Analitico>();
			return list1;
		}
	}
}