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
|
using Gestor.Application.Actions;
using Gestor.Application.Helpers;
using Gestor.Application.Servicos;
using Gestor.Model.Common;
using Gestor.Model.Domain.Configuracoes;
using Gestor.Model.Domain.Generic;
using Gestor.Model.Domain.Seguros;
using Gestor.Model.Resources;
using System;
using System.Collections;
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.Generic
{
public class DialogEditarParcelasViewModel : BaseViewModel
{
private List<Parcela> _parcelasOriginais;
internal Gestor.Application.Servicos.ParcelaServico ParcelaServico;
private ObservableCollection<Parcela> _parcelas;
private bool _isFatura;
private string _status;
private Parcela _selectedParcela;
private Documento _selectedDocumento;
public bool IsFatura
{
get
{
return this._isFatura;
}
set
{
this._isFatura = value;
base.OnPropertyChanged("IsFatura");
}
}
public ObservableCollection<Parcela> Parcelas
{
get
{
return this._parcelas;
}
set
{
this._parcelas = value;
base.OnPropertyChanged("Parcelas");
}
}
public Documento SelectedDocumento
{
get
{
return this._selectedDocumento;
}
set
{
this._selectedDocumento = value;
base.OnPropertyChanged("SelectedDocumento");
}
}
public Parcela SelectedParcela
{
get
{
return this._selectedParcela;
}
set
{
this._selectedParcela = value;
base.OnPropertyChanged("SelectedParcela");
}
}
public string Status
{
get
{
return this._status;
}
set
{
this._status = value;
base.OnPropertyChanged("Status");
}
}
public DialogEditarParcelasViewModel(Documento documento)
{
this.SelectedDocumento = documento;
this.ParcelaServico = new Gestor.Application.Servicos.ParcelaServico();
this.Carregar(documento.get_Id());
TipoRecebimento? tipoRecebimento = documento.get_TipoRecebimento();
this.IsFatura = tipoRecebimento.GetValueOrDefault(1) == 2;
}
public void AlterandoValor()
{
if (this.SelectedParcela.get_Documento() == null)
{
this.SelectedParcela.set_Documento(this.SelectedDocumento);
}
this.SelectedParcela.get_Documento().set_Parcelas(this.Parcelas);
if (this.SelectedDocumento.get_TipoRecebimento().GetValueOrDefault() == 1 && this.SelectedParcela.get_SubTipo() == 1 && this.SelectedParcela.get_Comissao() == decimal.Zero)
{
this.SelectedParcela.set_Comissao(this.SelectedDocumento.get_Comissao());
}
this.SelectedParcela.set_Baixando(false);
this.AlterarValor();
}
public void AlterandoVencimento(Parcela parcela)
{
DateTime vencimento;
ObservableCollection<Parcela> parcelas;
if (Recursos.Configuracoes.All<ConfiguracaoSistema>((ConfiguracaoSistema y) => y.get_Configuracao() != 11))
{
DateTime? vigencia2 = parcela.get_Documento().get_Vigencia2();
if (vigencia2.HasValue)
{
vencimento = parcela.get_Vencimento();
vigencia2 = parcela.get_Documento().get_Vigencia2();
if ((vigencia2.HasValue ? vencimento > vigencia2.GetValueOrDefault() : false) && parcela.get_Documento().get_Controle().get_Ramo().get_Id() != (long)23)
{
Action<string> acionarSnackbar = Gestor.Application.Actions.Actions.AcionarSnackbar;
if (acionarSnackbar != null)
{
acionarSnackbar(string.Concat("DATA INVÁLIDA. ", Messages.get_VencimentoMaior()));
}
else
{
}
parcela.set_Vencimento(this._parcelasOriginais.First<Parcela>((Parcela x) => x.get_Id() == parcela.get_Id()).get_Vencimento());
}
}
}
if (Recursos.Configuracoes.All<ConfiguracaoSistema>((ConfiguracaoSistema x) => x.get_Configuracao() != 9))
{
if (parcela.get_Documento().get_Vigencia1() > DateTime.MinValue && parcela.get_NumeroParcela() == 1)
{
DateTime dateTime = parcela.get_Vencimento();
vencimento = parcela.get_Documento().get_Vigencia1();
if (dateTime < vencimento.AddDays(-30))
{
goto Label0;
}
}
if (parcela.get_NumeroParcela() == 1 || !(parcela.get_Vencimento() < parcela.get_Documento().get_Vigencia1()))
{
parcelas = this.Parcelas;
this.Parcelas = new ObservableCollection<Parcela>(
from x in parcelas
orderby x.get_NumeroParcela()
select x);
return;
}
Label0:
Action<string> action = Gestor.Application.Actions.Actions.AcionarSnackbar;
if (action != null)
{
action(string.Concat("DATA INVÁLIDA. ", Messages.get_VencimentoMenor()));
}
else
{
}
parcela.set_Vencimento(this._parcelasOriginais.First<Parcela>((Parcela x) => x.get_Id() == parcela.get_Id()).get_Vencimento());
}
parcelas = this.Parcelas;
this.Parcelas = new ObservableCollection<Parcela>(
from x in parcelas
orderby x.get_NumeroParcela()
select x);
}
public void AlterarValor()
{
if (this.SelectedParcela == null)
{
return;
}
if (this.SelectedParcela.get_SubTipo() != 1 || this.IsFatura)
{
this.SelectedParcela.set_Documento(this.SelectedDocumento);
return;
}
decimal premioTotal = this.SelectedDocumento.get_PremioTotal() - this.Parcelas.Where<Parcela>((Parcela x) => {
if (x.get_SubTipo() != 1)
{
return false;
}
return x.get_Id() != this.SelectedParcela.get_Id();
}).Sum<Parcela>((Parcela x) => x.get_Valor());
decimal valor = this.SelectedDocumento.get_PremioTotal() - this.Parcelas.Where<Parcela>((Parcela x) => {
if (x.get_SubTipo() != 1)
{
return false;
}
return x.get_Id() < this.SelectedParcela.get_Id();
}).Sum<Parcela>((Parcela x) => x.get_Valor());
valor -= this.SelectedParcela.get_Valor();
decimal numeroParcelas = this.SelectedDocumento.get_NumeroParcelas() - this.SelectedParcela.get_NumeroParcela();
if (numeroParcelas == decimal.Zero)
{
numeroParcelas = decimal.One;
}
decimal num = (numeroParcelas > decimal.Zero ? valor / numeroParcelas : decimal.Zero);
if (num == decimal.Zero)
{
this.SelectedParcela.set_Valor(premioTotal);
}
List<Parcela> parcelas = new List<Parcela>()
{
this.SelectedParcela
};
decimal num1 = Math.Abs(num);
decimal? tolerancia = this.SelectedDocumento.get_Controle().get_Seguradora().get_Tolerancia();
if ((num1 > tolerancia.GetValueOrDefault()) & tolerancia.HasValue && !this.Parcelas.Any<Parcela>((Parcela x) => {
if (x.get_SubTipo() != 1)
{
return false;
}
return x.get_NumeroParcela() > this.SelectedParcela.get_NumeroParcela();
}))
{
this.SelectedParcela.set_Valor(premioTotal);
}
this.Parcelas.Where<Parcela>((Parcela x) => {
if (x.get_SubTipo() != 1)
{
return false;
}
return x.get_NumeroParcela() != this.SelectedParcela.get_NumeroParcela();
}).ToList<Parcela>().ForEach((Parcela x) => {
if (x.get_NumeroParcela() > this.SelectedParcela.get_NumeroParcela())
{
x.set_Valor((num > decimal.Zero ? num : x.get_Valor()));
x.set_Comissao((x.get_Comissao() == decimal.Zero ? this.SelectedDocumento.get_Comissao() : x.get_Comissao()));
}
parcelas.Add(x);
});
foreach (Parcela parcela in
from x in parcelas
where x.get_NumeroParcela() >= this.SelectedParcela.get_NumeroParcela()
select x)
{
parcela.set_Documento(this.SelectedDocumento);
if (parcela.get_Id() == this.SelectedParcela.get_Id())
{
continue;
}
parcela.set_Valor(num);
}
this.Parcelas = new ObservableCollection<Parcela>(
from x in parcelas
orderby x.get_NumeroParcela()
select x);
}
public async void Carregar(long id)
{
await this.CarregarParcelas(id);
}
public async Task CarregarParcelas(long id)
{
ObservableCollection<Parcela> observableCollection;
ObservableCollection<Parcela> observableCollection1 = await (new Gestor.Application.Servicos.ParcelaServico()).BuscarParcelasAsync(id);
this._parcelasOriginais = new List<Parcela>();
foreach (Parcela parcela in observableCollection1)
{
this._parcelasOriginais.Add((Parcela)parcela.Clone());
}
DialogEditarParcelasViewModel dialogEditarParcelasViewModel = this;
if (this.IsFatura)
{
ObservableCollection<Parcela> observableCollection2 = observableCollection1;
observableCollection = new ObservableCollection<Parcela>(
from x in observableCollection2
orderby x.get_NumeroParcela()
select x);
}
else
{
observableCollection = observableCollection1;
}
dialogEditarParcelasViewModel.Parcelas = observableCollection;
}
public async Task<bool> SalvarParcelas()
{
bool flag;
foreach (Parcela parcela in this.Parcelas)
{
Parcela parcela1 = this._parcelasOriginais.First<Parcela>((Parcela x) => x.get_Id() == parcela.get_Id());
if (!(parcela.get_Vencimento() != parcela1.get_Vencimento()) && !(parcela.get_Valor() != parcela1.get_Valor()))
{
continue;
}
parcela1.set_Vencimento(parcela.get_Vencimento());
parcela1.set_Valor(parcela.get_Valor());
Parcela selectedParcela = this.SelectedParcela;
List<ConfiguracaoSistema> configuracoes = Recursos.Configuracoes;
bool flag1 = configuracoes.Any<ConfiguracaoSistema>((ConfiguracaoSistema x) => x.get_Configuracao() == 9);
List<ConfiguracaoSistema> configuracaoSistemas = Recursos.Configuracoes;
bool flag2 = configuracaoSistemas.Any<ConfiguracaoSistema>((ConfiguracaoSistema y) => y.get_Configuracao() == 11);
List<ConfiguracaoSistema> configuracoes1 = Recursos.Configuracoes;
List<KeyValuePair<string, string>> keyValuePairs = selectedParcela.Validate(flag1, flag2, true, configuracoes1.Any<ConfiguracaoSistema>((ConfiguracaoSistema x) => x.get_Configuracao() == 16));
if (keyValuePairs.Count <= 0)
{
await (new Gestor.Application.Servicos.ParcelaServico()).Save(parcela1, this._parcelasOriginais, null, true);
}
else
{
List<KeyValuePair<string, string>> keyValuePairs1 = keyValuePairs;
string str = string.Join(", ", keyValuePairs1.Select<KeyValuePair<string, string>, string>((KeyValuePair<string, string> x) => {
string[] strArrays = x.Key.Split(new char[] { '|' });
return string.Concat(((int)strArrays.Length > 1 ? strArrays[1] : x.Key), ": ", x.Value, Environment.NewLine);
}));
this.Status = string.Concat("NÃO FOI POSSÍVEL SALVAR DEVIDO AOS ERROS ABAIXO:", Environment.NewLine, str);
flag = false;
return flag;
}
}
base.RegistrarAcao(string.Format("EDITOU VENCIMENTO E/OU VALORES DAS PARCELAS/FATURAS DO DOCUMENTO DE ID {0}", this.Parcelas[0].get_Documento().get_Id()), this.Parcelas[0].get_Documento().get_Id(), new TipoTela?(5), null);
flag = true;
return flag;
}
}
}
|