summaryrefslogtreecommitdiff
path: root/Codemerx/Gestor.Application/ViewModels/Ferramentas/CadastroParceiroViewModel.cs
blob: 9b8f08ce28466ba57e0bc9100dbf2b9846c90a38 (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
using Gestor.Application.Helpers;
using Gestor.Application.Servicos;
using Gestor.Application.Servicos.Generic;
using Gestor.Application.ViewModels.Generic;
using Gestor.Common.Validation;
using Gestor.Model.Common;
using Gestor.Model.Domain.Generic;
using Gestor.Model.Domain.Seguros;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

namespace Gestor.Application.ViewModels.Ferramentas
{
	public class CadastroParceiroViewModel : BaseSegurosViewModel
	{
		private readonly ParceiroServico _servico;

		private Parceiro _selectedParceiro;

		private ObservableCollection<Parceiro> _parceirosFiltrados = new ObservableCollection<Parceiro>();

		private bool _isExpanded;

		public Parceiro CancelParceiro;

		private Item _selectedItem = new Item();

		public bool IsExpanded
		{
			get
			{
				return this._isExpanded;
			}
			set
			{
				this._isExpanded = value;
				base.OnPropertyChanged("IsExpanded");
			}
		}

		public List<Parceiro> Parceiros
		{
			get;
			set;
		}

		public ObservableCollection<Parceiro> ParceirosFiltrados
		{
			get
			{
				return this._parceirosFiltrados;
			}
			set
			{
				this._parceirosFiltrados = value;
				this.IsExpanded = (value != null ? value.Count > 0 : false);
				base.OnPropertyChanged("ParceirosFiltrados");
			}
		}

		public Item SelectedItem
		{
			get
			{
				return this._selectedItem;
			}
			set
			{
				this._selectedItem = value;
				base.OnPropertyChanged("SelectedItem");
			}
		}

		public Parceiro SelectedParceiro
		{
			get
			{
				return this._selectedParceiro;
			}
			set
			{
				long? nullable;
				this._selectedParceiro = value;
				this.WorkOnSelectedParceiro(value, true);
				if (value != null)
				{
					nullable = new long?(value.get_Id());
				}
				else
				{
					nullable = null;
				}
				base.VerificarEnables(nullable);
				base.OnPropertyChanged("SelectedParceiro");
			}
		}

		public CadastroParceiroViewModel(Parceiro parceiro)
		{
			this._servico = new ParceiroServico();
			base.EnableMenu = true;
			this.Seleciona(parceiro);
		}

		public void CancelarAlteracao()
		{
			if (this.CancelParceiro == null || !this.Parceiros.Any<Parceiro>((Parceiro x) => x.get_Id() == this.CancelParceiro.get_Id()))
			{
				this.Incluir();
			}
			else
			{
				DomainBase.Copy<Parceiro, Parceiro>(this.Parceiros.First<Parceiro>((Parceiro x) => x.get_Id() == this.CancelParceiro.get_Id()), this.CancelParceiro);
				if (this.ParceirosFiltrados.Count <= 0 || !this.ParceirosFiltrados.Any<Parceiro>((Parceiro x) => x.get_Id() == this.CancelParceiro.get_Id()))
				{
					this.ParceirosFiltrados.Add(this.CancelParceiro);
				}
				else
				{
					DomainBase.Copy<Parceiro, Parceiro>(this.ParceirosFiltrados.First<Parceiro>((Parceiro x) => x.get_Id() == this.CancelParceiro.get_Id()), this.CancelParceiro);
				}
				this.ParceirosFiltrados = new ObservableCollection<Parceiro>(this.ParceirosFiltrados);
				this.SelecionarParceiro(this.ParceirosFiltrados.First<Parceiro>((Parceiro x) => x.get_Id() == this.CancelParceiro.get_Id()));
			}
			base.Alterar(false);
		}

		public async void Excluir()
		{
			string complemento;
			Parceiro parceiro;
			if (this.SelectedParceiro != null && this.SelectedParceiro.get_Id() != 0)
			{
				if (await (new BaseServico()).ParceiroUtilizado(this.SelectedParceiro.get_Id()))
				{
					await base.ShowMessage("PARCEIRO NÃO PODE SER EXCLUÍDO POIS ESTÁ SENDO UTILIZADO.", "OK", "", false);
				}
				else if (await base.ShowMessage("DESEJA EXCLUIR?", "SIM", "NÃO", false))
				{
					base.Loading(true);
					if (await this._servico.Delete(this.SelectedParceiro))
					{
						CadastroParceiroViewModel cadastroParceiroViewModel = this;
						string str = string.Concat("EXCLUIU O PARCEIRO \"", this.SelectedParceiro.get_Nome(), "\"");
						long id = this.SelectedParceiro.get_Id();
						TipoTela? nullable = new TipoTela?(22);
						string[] cgccpf = new string[] { "DOCUMENTO: ", this.SelectedParceiro.get_Cgccpf(), "\nTELEFONE 1: (", this.SelectedParceiro.get_Ddd1(), ") ", this.SelectedParceiro.get_Telefone1(), "\nTELEFONE 2: (", this.SelectedParceiro.get_Ddd2(), ") ", this.SelectedParceiro.get_Telefone2(), "\nTELEFONE 3: (", this.SelectedParceiro.get_Ddd3(), ") ", this.SelectedParceiro.get_Telefone3(), "\nE-MAIL: ", this.SelectedParceiro.get_Email(), "\n\nENDEREÇO: ", this.SelectedParceiro.get_Endereco(), ", ", this.SelectedParceiro.get_Numero(), ", ", null, null, null, null, null, null, null, null, null };
						if (string.IsNullOrWhiteSpace(this.SelectedParceiro.get_Complemento()))
						{
							complemento = "-";
						}
						else
						{
							complemento = this.SelectedParceiro.get_Complemento();
							if (complemento == null)
							{
								complemento = "";
							}
						}
						cgccpf[21] = complemento;
						cgccpf[22] = ", ";
						cgccpf[23] = this.SelectedParceiro.get_Bairro();
						cgccpf[24] = ", ";
						cgccpf[25] = this.SelectedParceiro.get_Cidade();
						cgccpf[26] = "/";
						cgccpf[27] = this.SelectedParceiro.get_Uf();
						cgccpf[28] = " - ";
						cgccpf[29] = this.SelectedParceiro.get_Cep();
						cadastroParceiroViewModel.RegistrarAcao(str, id, nullable, string.Concat(cgccpf));
						int num = this.ParceirosFiltrados.IndexOf(this.SelectedParceiro);
						this.Parceiros.Remove(this.SelectedParceiro);
						this.ParceirosFiltrados.Remove(this.SelectedParceiro);
						this.ParceirosFiltrados = new ObservableCollection<Parceiro>(this.ParceirosFiltrados);
						if (this.ParceirosFiltrados.Count <= 0)
						{
							this.Incluir();
							base.Alterar(false);
							base.EnableMenu = false;
						}
						else
						{
							CadastroParceiroViewModel cadastroParceiroViewModel1 = this;
							parceiro = (num < this.ParceirosFiltrados.Count ? this.ParceirosFiltrados.ElementAt<Parceiro>(num) : this.ParceirosFiltrados.Last<Parceiro>());
							cadastroParceiroViewModel1.SelecionarParceiro(parceiro);
						}
						base.Loading(false);
						base.ToggleSnackBar("PARCEIRO EXCLUÍDO COM SUCESSO", true);
					}
					else
					{
						base.Loading(false);
					}
				}
			}
		}

		internal async Task<List<Parceiro>> Filtrar(string value)
		{
			List<Parceiro> parceiros = await Task.Run<List<Parceiro>>(() => this.FiltrarParceiro(value));
			return parceiros;
		}

		public List<Parceiro> FiltrarParceiro(string filter)
		{
			this.ParceirosFiltrados = (string.IsNullOrWhiteSpace(filter) ? new ObservableCollection<Parceiro>(this.Parceiros) : new ObservableCollection<Parceiro>(
				from x in this.Parceiros
				where ValidationHelper.RemoveDiacritics(x.get_Nome().Trim()).ToUpper().Contains(ValidationHelper.RemoveDiacritics(filter))
				select x));
			return this.ParceirosFiltrados.ToList<Parceiro>();
		}

		public void Incluir()
		{
			this.SelectedParceiro = new Parceiro();
			base.Alterar(true);
		}

		public async Task<List<KeyValuePair<string, string>>> Salvar()
		{
			List<KeyValuePair<string, string>> keyValuePairs;
			string str;
			string complemento;
			string str1;
			List<KeyValuePair<string, string>> keyValuePairs1 = this.SelectedParceiro.Validate();
			if (keyValuePairs1.Count <= 0)
			{
				str = (this.SelectedParceiro.get_Id() == 0 ? "INCLUIU" : "ALTEROU");
				str1 = str;
				Parceiro parceiro = await this._servico.Save(this.SelectedParceiro);
				if (this._servico.Sucesso)
				{
					CadastroParceiroViewModel cadastroParceiroViewModel = this;
					string str2 = string.Concat(str1, " PARCEIRO \"", parceiro.get_Nome(), "\"");
					long id = parceiro.get_Id();
					TipoTela? nullable = new TipoTela?(22);
					string[] cgccpf = new string[] { "DOCUMENTO: ", parceiro.get_Cgccpf(), "\nTELEFONE 1: (", parceiro.get_Ddd1(), ") ", parceiro.get_Telefone1(), "\nTELEFONE 2: (", parceiro.get_Ddd2(), ") ", parceiro.get_Telefone2(), "\nTELEFONE 3: (", parceiro.get_Ddd3(), ") ", parceiro.get_Telefone3(), "\nE-MAIL: ", parceiro.get_Email(), "\n\nENDEREÇO: ", parceiro.get_Endereco(), ", ", parceiro.get_Numero(), ", ", null, null, null, null, null, null, null, null, null };
					if (string.IsNullOrWhiteSpace(parceiro.get_Complemento()))
					{
						complemento = "-";
					}
					else
					{
						complemento = parceiro.get_Complemento();
						if (complemento == null)
						{
							complemento = "";
						}
					}
					cgccpf[21] = complemento;
					cgccpf[22] = ", ";
					cgccpf[23] = parceiro.get_Bairro();
					cgccpf[24] = ", ";
					cgccpf[25] = parceiro.get_Cidade();
					cgccpf[26] = "/";
					cgccpf[27] = parceiro.get_Uf();
					cgccpf[28] = " - ";
					cgccpf[29] = parceiro.get_Cep();
					cadastroParceiroViewModel.RegistrarAcao(str2, id, nullable, string.Concat(cgccpf));
					if (!this.Parceiros.Any<Parceiro>((Parceiro x) => x.get_Id() == parceiro.get_Id()))
					{
						this.Parceiros.Add(parceiro);
					}
					else
					{
						DomainBase.Copy<Parceiro, Parceiro>(this.Parceiros.First<Parceiro>((Parceiro x) => x.get_Id() == parceiro.get_Id()), parceiro);
					}
					if (!this.ParceirosFiltrados.Any<Parceiro>((Parceiro x) => x.get_Id() == parceiro.get_Id()))
					{
						this.ParceirosFiltrados.Add(parceiro);
					}
					else
					{
						DomainBase.Copy<Parceiro, Parceiro>(this.ParceirosFiltrados.First<Parceiro>((Parceiro x) => x.get_Id() == parceiro.get_Id()), parceiro);
					}
					this.ParceirosFiltrados = new ObservableCollection<Parceiro>(this.ParceirosFiltrados);
					Recursos.Parceiros = this.Parceiros;
					this.WorkOnSelectedParceiro(parceiro, false);
					base.Alterar(false);
					base.ToggleSnackBar("PARCEIRO SALVO COM SUCESSO", true);
					keyValuePairs = null;
				}
				else
				{
					keyValuePairs = null;
				}
			}
			else
			{
				keyValuePairs = keyValuePairs1;
			}
			str1 = null;
			return keyValuePairs;
		}

		private async void Seleciona(Parceiro parceiro)
		{
			base.Loading(true);
			await base.PermissaoTela(22);
			await this.SelecionaParceiro(parceiro);
			base.Loading(false);
		}

		private async Task SelecionaParceiro(Parceiro parceiro = null)
		{
			base.Loading(true);
			List<Parceiro> parceiros = await (new BaseServico()).BuscarParceirosAsync();
			CadastroParceiroViewModel list = this;
			List<Parceiro> parceiros1 = parceiros;
			list.Parceiros = (
				from x in parceiros1
				orderby x.get_Nome()
				select x).ToList<Parceiro>();
			this.ParceirosFiltrados = new ObservableCollection<Parceiro>(this.Parceiros);
			CadastroParceiroViewModel cadastroParceiroViewModel = this;
			Parceiro parceiro1 = parceiro;
			if (parceiro1 == null)
			{
				parceiro1 = this.ParceirosFiltrados.FirstOrDefault<Parceiro>();
			}
			cadastroParceiroViewModel.SelecionarParceiro(parceiro1);
			base.Loading(false);
		}

		public async void SelecionarParceiro(Parceiro parceiro)
		{
			if (parceiro != null)
			{
				Parceiro parceiro1 = await this._servico.BuscarParceiro(parceiro.get_Id());
				DomainBase.Copy<Parceiro, Parceiro>(this.ParceirosFiltrados.First<Parceiro>((Parceiro x) => x.get_Id() == parceiro.get_Id()), parceiro1);
				this.SelectedParceiro = this.ParceirosFiltrados.First<Parceiro>((Parceiro x) => x.get_Id() == parceiro.get_Id());
			}
			else
			{
				this.SelectedParceiro = null;
			}
		}

		private async void WorkOnSelectedParceiro(Parceiro value, bool registrar = true)
		{
			Parceiro parceiro;
			long? nullable;
			string complemento;
			CadastroParceiroViewModel cadastroParceiroViewModel = this;
			parceiro = (value == null || value.get_Id() == 0 ? this.CancelParceiro : (Parceiro)value.Clone());
			cadastroParceiroViewModel.CancelParceiro = parceiro;
			if (value != null && value.get_Id() != 0 && (this.LastAccessId != value.get_Id() || this.LastAccessTela != 22))
			{
				if (registrar)
				{
					CadastroParceiroViewModel cadastroParceiroViewModel1 = this;
					string str = string.Concat("ACESSOU PARCEIRO \"", value.get_Nome(), "\"");
					long id = value.get_Id();
					TipoTela? nullable1 = new TipoTela?(22);
					string[] cgccpf = new string[] { "DOCUMENTO: ", value.get_Cgccpf(), "\nTELEFONE 1: (", value.get_Ddd1(), ") ", value.get_Telefone1(), "\nTELEFONE 2: (", value.get_Ddd2(), ") ", value.get_Telefone2(), "\nTELEFONE 3: (", value.get_Ddd3(), ") ", value.get_Telefone3(), "\nE-MAIL: ", value.get_Email(), "\n\nENDEREÇO: ", value.get_Endereco(), ", ", value.get_Numero(), ", ", null, null, null, null, null, null, null, null, null };
					if (string.IsNullOrWhiteSpace(value.get_Complemento()))
					{
						complemento = "-";
					}
					else
					{
						complemento = value.get_Complemento();
						if (complemento == null)
						{
							complemento = "";
						}
					}
					cgccpf[21] = complemento;
					cgccpf[22] = ", ";
					cgccpf[23] = value.get_Bairro();
					cgccpf[24] = ", ";
					cgccpf[25] = value.get_Cidade();
					cgccpf[26] = "/";
					cgccpf[27] = value.get_Uf();
					cgccpf[28] = " - ";
					cgccpf[29] = value.get_Cep();
					cadastroParceiroViewModel1.RegistrarAcao(str, id, nullable1, string.Concat(cgccpf));
				}
				this.LastAccessId = value.get_Id();
				this.LastAccessTela = 22;
				Parceiro selectedParceiro = this.SelectedParceiro;
				if (selectedParceiro != null)
				{
					nullable = new long?(selectedParceiro.get_Id());
				}
				else
				{
					nullable = null;
				}
				long? nullable2 = nullable;
				long num = value.get_Id();
				if (nullable2.GetValueOrDefault() != num | !nullable2.HasValue)
				{
					Parceiro parceiro1 = await this._servico.BuscarParceiro(value.get_Id());
					DomainBase.Copy<Parceiro, Parceiro>(this.SelectedParceiro, parceiro1);
					Parceiro selectedParceiro1 = this.SelectedParceiro;
					if (selectedParceiro1 != null)
					{
						selectedParceiro1.Initialize();
					}
					else
					{
					}
					this.Initialized = true;
				}
			}
		}
	}
}