summaryrefslogtreecommitdiff
path: root/Codemerx/Gestor.Application/ViewModels/Ferramentas/NotaFiscalViewModel.cs
blob: 61ee2854150e2d0c74c4bc92e617ecd3fbc7bfe2 (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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
using Gestor.Application.Helpers;
using Gestor.Application.Servicos;
using Gestor.Application.Servicos.Ferramentas;
using Gestor.Application.Servicos.Generic;
using Gestor.Application.ViewModels.Generic;
using Gestor.Common.Validation;
using Gestor.Model.Common;
using Gestor.Model.Domain.Configuracoes;
using Gestor.Model.Domain.Ferramentas;
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.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

namespace Gestor.Application.ViewModels.Ferramentas
{
	public class NotaFiscalViewModel : BaseSegurosViewModel
	{
		private readonly NotaFiscalServico _servico;

		private readonly ServicoExtrato _servicoExtrato;

		private bool _apelido;

		private List<Estipulante> _estipulantes;

		private List<Seguradora> _seguradoras;

		private ObservableCollection<NotaFiscal> _notasFiscaisFiltrados = new ObservableCollection<NotaFiscal>();

		private bool _isExpanded;

		private string _numExtrato;

		private NotaFiscal _selectedNotaFiscal;

		public NotaFiscal CancelNotaFiscal;

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

		public List<Estipulante> Estipulantes
		{
			get
			{
				return this._estipulantes;
			}
			set
			{
				this._estipulantes = value;
				base.OnPropertyChanged("Estipulantes");
			}
		}

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

		public List<NotaFiscal> NotasFiscais
		{
			get;
			set;
		}

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

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

		public List<Seguradora> Seguradoras
		{
			get
			{
				return this._seguradoras;
			}
			set
			{
				this._seguradoras = value;
				base.OnPropertyChanged("Seguradoras");
			}
		}

		public NotaFiscal SelectedNotaFiscal
		{
			get
			{
				return this._selectedNotaFiscal;
			}
			set
			{
				long? nullable;
				this._selectedNotaFiscal = value;
				this.WorkOnSelectedNotaFiscal(value, true);
				if (value != null)
				{
					nullable = new long?(value.get_Id());
				}
				else
				{
					nullable = null;
				}
				base.VerificarEnables(nullable);
				base.OnPropertyChanged("SelectedNotaFiscal");
			}
		}

		public NotaFiscalViewModel()
		{
			this._servicoExtrato = new ServicoExtrato();
			this._servico = new NotaFiscalServico();
			this.Seguradoras = (
				from x in Recursos.Seguradoras
				where x.get_Ativo()
				select x).ToList<Seguradora>();
			this.Estipulantes = (
				from e in Recursos.Estipulantes
				where e.get_Ativo()
				select e).ToList<Estipulante>();
			this.Apelido = Recursos.Configuracoes.Any<ConfiguracaoSistema>((ConfiguracaoSistema x) => x.get_Configuracao() == 6);
			base.EnableMenu = true;
			this.Seleciona();
		}

		public async Task<decimal> BuscarImposto()
		{
			decimal iss;
			List<Imposto> impostos = await (new ImpostoServico()).Buscar(new bool?(true));
			Imposto imposto = impostos.FirstOrDefault<Imposto>((Imposto x) => {
				if (x.get_Seguradora() == null || x.get_Seguradora().get_Id() != this.SelectedNotaFiscal.get_Seguradora().get_Id())
				{
					return false;
				}
				return x.get_Ramo() == null;
			});
			if (imposto == null)
			{
				List<Imposto> impostos1 = impostos;
				imposto = impostos1.FirstOrDefault<Imposto>((Imposto x) => {
					if (x.get_Seguradora() != null)
					{
						return false;
					}
					return x.get_Ramo() == null;
				});
			}
			if (imposto != null)
			{
				iss = imposto.get_Iss();
			}
			else
			{
				iss = decimal.Zero;
			}
			return iss;
		}

		public void CancelarAlteracao()
		{
			if (this.CancelNotaFiscal == null || !this.NotasFiscais.Any<NotaFiscal>((NotaFiscal x) => x.get_Id() == this.CancelNotaFiscal.get_Id()))
			{
				this.Incluir();
			}
			else
			{
				DomainBase.Copy<NotaFiscal, NotaFiscal>(this.NotasFiscais.First<NotaFiscal>((NotaFiscal x) => x.get_Id() == this.CancelNotaFiscal.get_Id()), this.CancelNotaFiscal);
				if (this.NotasFiscaisFiltrados.Count <= 0 || !this.NotasFiscaisFiltrados.Any<NotaFiscal>((NotaFiscal x) => x.get_Id() == this.CancelNotaFiscal.get_Id()))
				{
					this.NotasFiscaisFiltrados.Add(this.CancelNotaFiscal);
				}
				else
				{
					DomainBase.Copy<NotaFiscal, NotaFiscal>(this.NotasFiscaisFiltrados.First<NotaFiscal>((NotaFiscal x) => x.get_Id() == this.CancelNotaFiscal.get_Id()), this.CancelNotaFiscal);
				}
				this.NotasFiscaisFiltrados = new ObservableCollection<NotaFiscal>(this.NotasFiscaisFiltrados);
				this.SelectedNotaFiscal = this.NotasFiscaisFiltrados.First<NotaFiscal>((NotaFiscal x) => x.get_Id() == this.CancelNotaFiscal.get_Id());
			}
			base.Alterar(false);
		}

		public async void Excluir()
		{
			object obj;
			if (this.SelectedNotaFiscal != null && this.SelectedNotaFiscal.get_Id() != 0)
			{
				if (await base.ShowMessage(string.Concat("DESEJA REALMENTE EXCLUIR A NOTA FISCAL DA ", this.SelectedNotaFiscal.get_Seguradora().get_Nome(), "?"), "SIM", "NÃO", false))
				{
					base.Loading(true);
					if (await this._servico.Delete(this.SelectedNotaFiscal))
					{
						NotaFiscalViewModel notaFiscalViewModel = this;
						string str = string.Format("EXCLUIU NOTA FISCAL DE ID \"{0}\"", this.SelectedNotaFiscal.get_Id());
						long id = this.SelectedNotaFiscal.get_Id();
						TipoTela? nullable = new TipoTela?(55);
						object[] nome = new object[] { this.SelectedNotaFiscal.get_Seguradora().get_Nome(), null, null, null, null };
						obj = (!this.SelectedNotaFiscal.get_Data().HasValue ? "-" : string.Format("{0:d}", this.SelectedNotaFiscal.get_Data()));
						nome[1] = obj;
						nome[2] = this.SelectedNotaFiscal.get_ValorBruto();
						nome[3] = this.SelectedNotaFiscal.get_Iss();
						nome[4] = this.SelectedNotaFiscal.get_ValorLiquido();
						notaFiscalViewModel.RegistrarAcao(str, id, nullable, string.Format("SEGURADORA: {0}\nDATA: {1}\nBRUTO: {2:c}\nISS: {3:c}\nLÍQUIDO: {4:c}", nome));
						await this.SelecionaNotaFiscal();
						base.Loading(false);
						base.ToggleSnackBar("RECIBO EXCLUÍDO COM SUCESSO", true);
					}
					else
					{
						base.Loading(false);
					}
				}
			}
		}

		public async Task<List<NotaFiscal>> Filtrar(string value)
		{
			List<NotaFiscal> notaFiscals = await Task.Run<List<NotaFiscal>>(() => this.FiltrarNotaFiscal(value));
			return notaFiscals;
		}

		public List<NotaFiscal> FiltrarNotaFiscal(string filter)
		{
			this.NotasFiscaisFiltrados = (string.IsNullOrWhiteSpace(filter) ? new ObservableCollection<NotaFiscal>(this.NotasFiscais) : new ObservableCollection<NotaFiscal>(this.NotasFiscais.Where<NotaFiscal>((NotaFiscal x) => {
				if (ValidationHelper.RemoveDiacritics(x.get_Seguradora().get_Nome().Trim()).ToUpper().Contains(ValidationHelper.RemoveDiacritics(filter)) || ValidationHelper.RemoveDiacritics(x.get_ValorBruto().ToString(CultureInfo.InvariantCulture).Trim()).Contains(ValidationHelper.RemoveDiacritics(filter)))
				{
					return true;
				}
				return ValidationHelper.RemoveDiacritics(x.get_Data().ToString().Trim()).Contains(ValidationHelper.RemoveDiacritics(filter));
			}).OrderBy<NotaFiscal, string>((NotaFiscal x) => x.get_Seguradora().get_Nome())));
			return this.NotasFiscaisFiltrados.ToList<NotaFiscal>();
		}

		public void Incluir()
		{
			NotaFiscal notaFiscal = new NotaFiscal();
			notaFiscal.set_Seguradora(new Seguradora());
			notaFiscal.set_Estipulante(new Estipulante());
			notaFiscal.set_Iss(decimal.Zero);
			notaFiscal.set_ValorLiquido(decimal.Zero);
			notaFiscal.set_ValorBruto(decimal.Zero);
			notaFiscal.set_Extrato("");
			this.SelectedNotaFiscal = notaFiscal;
			base.Alterar(true);
		}

		public async Task<List<KeyValuePair<string, string>>> Salvar()
		{
			List<KeyValuePair<string, string>> keyValuePairs;
			string str;
			long? nullable;
			bool valueOrDefault;
			object obj;
			string str1;
			List<KeyValuePair<string, string>> keyValuePairs1 = this.SelectedNotaFiscal.Validate();
			if (keyValuePairs1.Count <= 0)
			{
				str = (this.SelectedNotaFiscal.get_Id() == 0 ? "INCLUIU" : "ALTEROU");
				str1 = str;
				NotaFiscal selectedNotaFiscal = this.SelectedNotaFiscal;
				if (selectedNotaFiscal != null)
				{
					Estipulante estipulante = selectedNotaFiscal.get_Estipulante();
					if (estipulante != null)
					{
						nullable = new long?(estipulante.get_Id());
					}
					else
					{
						nullable = null;
					}
					long? nullable1 = nullable;
					long num = (long)0;
					valueOrDefault = nullable1.GetValueOrDefault() <= num & nullable1.HasValue;
				}
				else
				{
					valueOrDefault = false;
				}
				if (valueOrDefault)
				{
					this.SelectedNotaFiscal.set_Estipulante(null);
				}
				NotaFiscal notaFiscal = await this._servico.Save(this.SelectedNotaFiscal);
				if (this._servico.Sucesso)
				{
					NotaFiscalViewModel notaFiscalViewModel = this;
					string str2 = string.Format("{0} NOTA FISCAL DE ID \"{1}\"", str1, notaFiscal.get_Id());
					long id = notaFiscal.get_Id();
					TipoTela? nullable2 = new TipoTela?(55);
					object[] nome = new object[] { notaFiscal.get_Seguradora().get_Nome(), null, null, null, null };
					obj = (!notaFiscal.get_Data().HasValue ? "-" : string.Format("{0:d}", notaFiscal.get_Data()));
					nome[1] = obj;
					nome[2] = notaFiscal.get_ValorBruto();
					nome[3] = notaFiscal.get_Iss();
					nome[4] = notaFiscal.get_ValorLiquido();
					notaFiscalViewModel.RegistrarAcao(str2, id, nullable2, string.Format("SEGURADORA: {0}\nDATA: {1}\nBRUTO: {2:c}\nISS: {3:c}\nLÍQUIDO: {4:c}", nome));
					if (!this.NotasFiscais.Any<NotaFiscal>((NotaFiscal x) => x.get_Id() == notaFiscal.get_Id()))
					{
						this.NotasFiscais.Add(notaFiscal);
					}
					else
					{
						DomainBase.Copy<NotaFiscal, NotaFiscal>(this.NotasFiscais.First<NotaFiscal>((NotaFiscal x) => x.get_Id() == notaFiscal.get_Id()), notaFiscal);
					}
					if (!this.NotasFiscaisFiltrados.Any<NotaFiscal>((NotaFiscal x) => x.get_Id() == notaFiscal.get_Id()))
					{
						this.NotasFiscaisFiltrados.Add(notaFiscal);
					}
					else
					{
						DomainBase.Copy<NotaFiscal, NotaFiscal>(this.NotasFiscaisFiltrados.First<NotaFiscal>((NotaFiscal x) => x.get_Id() == notaFiscal.get_Id()), notaFiscal);
					}
					this.NotasFiscaisFiltrados = new ObservableCollection<NotaFiscal>(this.NotasFiscaisFiltrados);
					this.WorkOnSelectedNotaFiscal(notaFiscal, false);
					base.Alterar(false);
					base.ToggleSnackBar("NOTA FISCAL SALVA COM SUCESSO", true);
					keyValuePairs = null;
				}
				else
				{
					keyValuePairs = null;
				}
			}
			else
			{
				keyValuePairs = keyValuePairs1;
			}
			str1 = null;
			return keyValuePairs;
		}

		public async Task SalvarLote(List<NotaFiscal> notas)
		{
			foreach (NotaFiscal nota in notas)
			{
				bool hasValue = nota.get_IdExtrato().HasValue;
				if (hasValue)
				{
					NotaFiscalServico notaFiscalServico = this._servico;
					long? idExtrato = nota.get_IdExtrato();
					hasValue = await notaFiscalServico.Cadatrada(idExtrato.Value);
				}
				if (hasValue)
				{
					continue;
				}
				this.SelectedNotaFiscal = nota;
				await this.Salvar();
			}
			base.ToggleSnackBar("NOTAS FISCAIS SALVAS COM SUCESSO", true);
		}

		private async void Seleciona()
		{
			base.Loading(true);
			await base.PermissaoTela(55);
			await this.SelecionaNotaFiscal();
			base.Loading(false);
		}

		public async Task SelecionaNotaFiscal()
		{
			base.Loading(true);
			List<NotaFiscal> notaFiscals = await this._servico.BuscarNotasFiscais();
			NotaFiscalViewModel list = this;
			List<NotaFiscal> notaFiscals1 = notaFiscals;
			list.NotasFiscais = (
				from x in notaFiscals1
				orderby x.get_Seguradora().get_Nome()
				select x).ToList<NotaFiscal>();
			this.NotasFiscaisFiltrados = new ObservableCollection<NotaFiscal>(this.NotasFiscais);
			this.SelectedNotaFiscal = this.NotasFiscaisFiltrados.FirstOrDefault<NotaFiscal>();
			base.Loading(false);
		}

		private async Task WorkOnSelectedNotaFiscal(NotaFiscal value, bool registrar = true)
		{
			string str;
			long? idExtrato;
			NotaFiscal notaFiscal;
			long? nullable;
			object obj;
			NotaFiscalViewModel notaFiscalViewModel = this;
			notaFiscal = (value == null || value.get_Id() == 0 ? this.CancelNotaFiscal : (NotaFiscal)value.Clone());
			notaFiscalViewModel.CancelNotaFiscal = notaFiscal;
			if (value != null && value.get_Id() != 0 && (this.LastAccessId != value.get_Id() || this.LastAccessTela != 55))
			{
				if (registrar)
				{
					NotaFiscalViewModel notaFiscalViewModel1 = this;
					string str1 = string.Format("ACESSOU NOTA FISCAL DE ID \"{0}\"", value.get_Id());
					long id = value.get_Id();
					TipoTela? nullable1 = new TipoTela?(55);
					object[] nome = new object[] { value.get_Seguradora().get_Nome(), null, null, null, null };
					obj = (!value.get_Data().HasValue ? "-" : string.Format("{0:d}", value.get_Data()));
					nome[1] = obj;
					nome[2] = value.get_ValorBruto();
					nome[3] = value.get_Iss();
					nome[4] = value.get_ValorLiquido();
					notaFiscalViewModel1.RegistrarAcao(str1, id, nullable1, string.Format("SEGURADORA: {0}\nDATA: {1}\nBRUTO: {2:c}\nISS: {3:c}\nLÍQUIDO: {4:c}", nome));
				}
				this.LastAccessId = value.get_Id();
				this.LastAccessTela = 55;
				if (string.IsNullOrEmpty(this.SelectedNotaFiscal.get_Extrato()))
				{
					NotaFiscal selectedNotaFiscal = this.SelectedNotaFiscal;
					idExtrato = this.SelectedNotaFiscal.get_IdExtrato();
					if (!idExtrato.HasValue)
					{
						str = "";
					}
					else
					{
						str = await this._servicoExtrato.BuscarNumExtrato(this.SelectedNotaFiscal.get_IdExtrato());
					}
					selectedNotaFiscal.set_Extrato(str);
					selectedNotaFiscal = null;
				}
				NotaFiscal selectedNotaFiscal1 = this.SelectedNotaFiscal;
				if (selectedNotaFiscal1 != null)
				{
					nullable = new long?(selectedNotaFiscal1.get_Id());
				}
				else
				{
					nullable = null;
				}
				idExtrato = nullable;
				long num = value.get_Id();
				if (idExtrato.GetValueOrDefault() != num | !idExtrato.HasValue)
				{
					this.SelectedNotaFiscal = this.NotasFiscaisFiltrados.FirstOrDefault<NotaFiscal>((NotaFiscal x) => x.get_Id() == value.get_Id());
				}
			}
		}
	}
}