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
|
using AutoMapper;
using Gestor.Infrastructure.Entities.Financeiro;
using Gestor.Infrastructure.Entities.Generic;
using Gestor.Infrastructure.Helpers;
using Gestor.Infrastructure.Mappers;
using Gestor.Infrastructure.Repository.Generic;
using Gestor.Infrastructure.Repository.Interface;
using Gestor.Infrastructure.UnitOfWork.Generic;
using Gestor.Model.Common;
using Gestor.Model.Domain.Financeiro;
using Gestor.Model.Domain.Financeiro.Relatorios;
using Gestor.Model.Domain.Generic;
using NHibernate;
using NHibernate.Connection;
using NHibernate.Impl;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
namespace Gestor.Infrastructure.Repository.Logic
{
public class LancamentoRepository : GenericRepository<LancamentoDb>, ILancamentoRepository, IGenericRepository<LancamentoDb>
{
private readonly GenericUnitOfWork _unitOfWork;
public LancamentoRepository(GenericUnitOfWork unitOfWork) : base(unitOfWork.Session)
{
this._unitOfWork = unitOfWork;
}
public List<Lancamento> AddRange(List<Lancamento> lancamento)
{
List<LancamentoDb> lancamentoDbs = ApplicationMapper.Mapper.Map<List<Lancamento>, List<LancamentoDb>>(lancamento);
base.AddRange(lancamentoDbs);
return ApplicationMapper.Mapper.Map<List<LancamentoDb>, List<Lancamento>>(lancamentoDbs);
}
public bool BancosContasUtilizado(long id)
{
return base.All().Any<LancamentoDb>((LancamentoDb x) => x.Conta.Id == id);
}
public void Delete(long id)
{
LancamentoDb lancamentoDb = base.FindEntityById(id);
if (lancamentoDb == null)
{
return;
}
base.Delete(lancamentoDb);
}
public List<Lancamento> Fechamento(FiltroFinanceiro filtro)
{
string str = (filtro.Centro == null || filtro.Centro.Count == 0 ? "" : string.Concat(" AND c.idcentro IN (", string.Join<long>(",", filtro.Centro), ")"));
string str1 = (filtro.Plano == null || filtro.Plano.Count == 0 ? "" : string.Concat(" AND cp.idcplano IN (", string.Join<long>(",", filtro.Plano), ")"));
string str2 = (filtro.Planos == null || filtro.Planos.Count == 0 ? "" : string.Concat(" AND c.idcplanos IN (", string.Join<long>(",", filtro.Planos), ")"));
string str3 = (filtro.Conta == null || filtro.Conta.Count == 0 ? "" : string.Concat(" AND l.idconta IN (", string.Join<long>(",", filtro.Conta), ")"));
return this.Select(string.Format(" AND {0} >= '{1:yyyy-MM-dd}' AND {2} <= '{3:yyyy-MM-dd}' {4} {5} {6} {7}", new object[] { filtro.Referencia, filtro.Inicio, filtro.Referencia, filtro.Fim, str, str1, str2, str3 }));
}
public Saldo FecharSaldo(Saldo saldo)
{
object connection;
SessionFactoryImpl sessionFactory = this._unitOfWork.Session.SessionFactory as SessionFactoryImpl;
decimal num = new decimal();
if (sessionFactory != null)
{
connection = sessionFactory.ConnectionProvider.GetConnection();
}
else
{
connection = null;
}
using (SqlConnection sqlConnection = connection as SqlConnection)
{
if (sqlConnection != null)
{
using (SqlCommand sqlCommand = sqlConnection.CreateCommand())
{
sqlCommand.CommandText = string.Format("SELECT sinal, vlrbaixa FROM lancamen WHERE idconta = {0} AND dtbaixa IS NOT NULL AND dtbaixa >= '{1:yyyy-MM-dd}' AND dtbaixa < '{2:yyyy-MM-dd}' ", saldo.Conta.Id, saldo.DataInicio, saldo.DataFinal);
DataTable dataTable = new DataTable();
using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter())
{
sqlDataAdapter.SelectCommand = sqlCommand;
sqlDataAdapter.Fill(dataTable);
}
num = (dataTable.Rows.Count == 0 ? decimal.Zero : dataTable.AsEnumerable().ToList<DataRow>().Sum<DataRow>((DataRow x) => {
decimal? nullable;
decimal? nullable1;
if (x.Field<string>("sinal") == "0")
{
nullable = x.Field<decimal?>("vlrbaixa");
return nullable.GetValueOrDefault();
}
decimal? nullable2 = x.Field<decimal?>("vlrbaixa");
if (nullable2.HasValue)
{
nullable1 = new decimal?(-nullable2.GetValueOrDefault());
}
else
{
nullable = null;
nullable1 = nullable;
}
nullable = nullable1;
return nullable.GetValueOrDefault();
}));
}
}
}
saldo.ValorFinal = new decimal?(saldo.ValorInicio + num);
return saldo;
}
public List<Lancamento> Find(DateTime inicio, DateTime fim, StatusLancamento status)
{
if (status == StatusLancamento.Baixados)
{
return this.Select(string.Format(" AND CAST(l.dtpre AS DATE) >= '{0:yyyy-MM-dd}' AND CAST(l.dtpre AS DATE) <= '{1:yyyy-MM-dd}' AND l.dtbaixa IS NOT NULL", inicio, fim));
}
if (status != StatusLancamento.Todos)
{
return this.Select(string.Format(" AND CAST(l.dtpre AS DATE) >= '{0:yyyy-MM-dd}' AND CAST(l.dtpre AS DATE) <= '{1:yyyy-MM-dd}' AND l.dtbaixa IS NULL", inicio, fim));
}
return this.Select(string.Format(" AND CAST(l.dtpre AS DATE) >= '{0:yyyy-MM-dd}' AND CAST(l.dtpre AS DATE) <= '{1:yyyy-MM-dd}'", inicio, fim));
}
public List<Lancamento> FindByBaixa(DateTime inicio, DateTime fim)
{
return this.Select(string.Format(" AND CAST(l.dtbaixa AS DATE) >= '{0:yyyy-MM-dd}' AND CAST(l.dtbaixa AS DATE) <= '{1:yyyy-MM-dd}'", inicio, fim));
}
public List<Lancamento> FindByCodigoBanco(List<string> codigos)
{
List<string> strs = codigos;
List<Lancamento> lancamentos = new List<Lancamento>();
while (strs.Count > 0)
{
List<string> list = strs.Take<string>(2100).ToList<string>();
string str = string.Concat(" AND l.CodigoBanco IN ('", string.Join(",", list), "')");
lancamentos.AddRange(this.Select(str));
strs = (list.Count < 2100 ? new List<string>() : strs.Except<string>(list).ToList<string>());
}
return lancamentos;
}
public List<ExtratoConta> FindByConta(DateTime inicio, DateTime fim, long id)
{
return this.Select(string.Format(" AND l.idconta = {0} AND CAST(l.dtbaixa AS DATE) >= '{1:yyyy-MM-dd}' AND CAST(l.dtbaixa AS DATE) <= '{2:yyyy-MM-dd}'", id, inicio, fim)).Select<Lancamento, ExtratoConta>((Lancamento x) => {
decimal? valorPago;
string nome;
decimal zero;
ExtratoConta extratoContum = new ExtratoConta()
{
IdLancamento = x.Id
};
DateTime? baixa = x.Baixa;
extratoContum.Baixa = new DateTime?((baixa.HasValue ? baixa.GetValueOrDefault() : DateTime.MinValue));
ControleFinanceiro controle = x.Controle;
if (controle != null)
{
Fornecedor fornecedor = controle.Fornecedor;
if (fornecedor != null)
{
nome = fornecedor.Nome;
}
else
{
nome = null;
}
}
else
{
nome = null;
}
extratoContum.Fornecedor = nome;
extratoContum.Historico = x.Historico;
extratoContum.TipoPagamento = new TipoPagamento?(x.TipoPagamento);
if (!x.ValorPago.HasValue)
{
zero = decimal.Zero;
}
else if (x.Sinal == Sinal.Credito)
{
valorPago = x.ValorPago;
zero = Math.Abs(valorPago.Value);
}
else
{
valorPago = x.ValorPago;
zero = -Math.Abs(valorPago.Value);
}
extratoContum.Valor = new decimal?(zero);
extratoContum.Sinal = x.Sinal;
return extratoContum;
}).ToList<ExtratoConta>();
}
public List<Lancamento> FindByControle(long id, StatusLancamento status)
{
if (status == StatusLancamento.Baixados)
{
return this.Select(string.Format(" AND l.idcfinan = {0} AND l.dtbaixa IS NOT NULL", id));
}
if (status != StatusLancamento.Todos)
{
return this.Select(string.Format(" AND l.idcfinan = {0} AND l.dtbaixa IS NULL", id));
}
return this.Select(string.Format(" AND l.idcfinan = {0}", id));
}
public Lancamento FindByControle(long id, int parcela)
{
return this.Select(string.Format(" AND l.idcfinan = {0} AND l.parcela = {1}", id, parcela)).FirstOrDefault<Lancamento>();
}
public List<Lancamento> FindByFornecedor(long id, StatusLancamento status)
{
if (status == StatusLancamento.Baixados)
{
return this.Select(string.Format(" AND c.idfornecedor = {0} AND l.dtbaixa IS NOT NULL", id));
}
if (status != StatusLancamento.Todos)
{
return this.Select(string.Format(" AND c.idfornecedor = {0} AND l.dtbaixa IS NULL", id));
}
return this.Select(string.Format(" AND c.idfornecedor = {0}", id));
}
public List<Lancamento> FindByFornecedor(long id, DateTime date, Sinal sinal)
{
return this.Select(string.Format(" AND c.idfornecedor = {0} AND l.dtpre >= '{1:yyyy-MM-dd}' AND l.dtpre <= '{2:yyyy-MM-dd}' AND l.sinal = {3}", new object[] { id, date, date.AddMonths(1), (int)sinal }));
}
public Lancamento FindById(long id)
{
LancamentoDb lancamentoDb = base.FindEntityById(id);
return ApplicationMapper.Mapper.Map<LancamentoDb, Lancamento>(lancamentoDb);
}
public List<Lancamento> FindByLancamento(DateTime inicio, DateTime fim, StatusLancamento status)
{
if (status == StatusLancamento.Baixados)
{
return this.Select(string.Format(" AND CAST(l.datacriacao AS DATE) >= '{0:yyyy-MM-dd}' AND CAST(l.datacriacao AS DATE) <= '{1:yyyy-MM-dd}' AND l.dtbaixa IS NOT NULL", inicio, fim));
}
if (status != StatusLancamento.Todos)
{
return this.Select(string.Format(" AND CAST(l.datacriacao AS DATE) >= '{0:yyyy-MM-dd}' AND CAST(l.datacriacao AS DATE) <= '{1:yyyy-MM-dd}' AND l.dtbaixa IS NULL", inicio, fim));
}
return this.Select(string.Format(" AND CAST(l.datacriacao AS DATE) >= '{0:yyyy-MM-dd}' AND CAST(l.datacriacao AS DATE) <= '{1:yyyy-MM-dd}'", inicio, fim));
}
public List<Lancamento> FindByPagamento(DateTime inicio, DateTime fim)
{
return this.Select(string.Format(" AND CAST(l.dtpagto AS DATE) >= '{0:yyyy-MM-dd}' AND CAST(l.dtpagto AS DATE) <= '{1:yyyy-MM-dd}'", inicio, fim));
}
public List<Lancamento> FindLancamentosByConta(DateTime inicio, DateTime fim, long id)
{
return this.Select(string.Format(" AND CAST(l.dtpre AS DATE) >= '{0:yyyy-MM-dd}' AND CAST(l.dtpre AS DATE) <= '{1:yyyy-MM-dd}' AND l.idconta = {2}", inicio, fim, id));
}
public List<Lancamento> FindPersonalizado(DateTime inicio, DateTime fim, StatusLancamento status, FiltroLancamentoData filtrodata)
{
string referencia = this.GetReferencia(filtrodata);
if (status == StatusLancamento.Baixados)
{
return this.Select(string.Format(" AND {0} >= '{1:yyyy-MM-dd}' AND {2} <= '{3:yyyy-MM-dd}' AND l.dtbaixa IS NOT NULL", new object[] { referencia, inicio, referencia, fim }));
}
if (status != StatusLancamento.Todos)
{
return this.Select(string.Format(" AND {0} >= '{1:yyyy-MM-dd}' AND {2} <= '{3:yyyy-MM-dd}' AND l.dtbaixa IS NULL", new object[] { referencia, inicio, referencia, fim }));
}
return this.Select(string.Format(" AND {0} >= '{1:yyyy-MM-dd}' AND {2} <= '{3:yyyy-MM-dd}'", new object[] { referencia, inicio, referencia, fim }));
}
private string GetReferencia(FiltroLancamentoData filtro)
{
if (filtro == FiltroLancamentoData.Baixa)
{
return "CAST(l.dtbaixa AS DATE)";
}
if (filtro != FiltroLancamentoData.Pagamento)
{
return "CAST(l.dtpre AS DATE)";
}
return "CAST(l.dtpagto AS DATE)";
}
public bool HasByFornecedor(long id)
{
return base.All().Any<LancamentoDb>((LancamentoDb x) => x.Controle.Fornecedor.Id == id);
}
public Lancamento Merge(Lancamento lancamento)
{
LancamentoDb lancamentoDb = ApplicationMapper.Mapper.Map<Lancamento, LancamentoDb>(lancamento);
base.Merge(lancamentoDb);
return ApplicationMapper.Mapper.Map<LancamentoDb, Lancamento>(lancamentoDb);
}
public Lancamento SaveOrUpdate(Lancamento lancamento)
{
LancamentoDb lancamentoDb = ApplicationMapper.Mapper.Map<Lancamento, LancamentoDb>(lancamento);
this.SaveOrUpdate(lancamentoDb);
return ApplicationMapper.Mapper.Map<LancamentoDb, Lancamento>(lancamentoDb);
}
private List<Lancamento> Select(string condition)
{
object connection;
DataTable dataTable = new DataTable();
SessionFactoryImpl sessionFactory = this._unitOfWork.Session.SessionFactory as SessionFactoryImpl;
if (sessionFactory != null)
{
connection = sessionFactory.ConnectionProvider.GetConnection();
}
else
{
connection = null;
}
using (SqlConnection sqlConnection = connection as SqlConnection)
{
using (SqlCommand sqlCommand = sqlConnection.CreateCommand())
{
AuxiliarFinanceiro.Criar(sqlCommand);
sqlCommand.CommandText = string.Concat("SELECT DISTINCT c.idfornecedor, f.nome, f.Ativo, c.idfornecedor, c.idcplanos, c.idcentro, c.historico as historicocfinan, c.parcela as parcelas, cp.idcplano, l.* FROM lancamen l INNER JOIN cfinan c on c.idcfinan = l.idcfinan LEFT OUTER JOIN cplanos cp on c.idcplanos = cp.idcplanos LEFT OUTER JOIN fornecedor f on f.idfornecedor = c.idfornecedor WHERE 1=1 ", condition);
using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter())
{
sqlDataAdapter.SelectCommand = sqlCommand;
sqlDataAdapter.Fill(dataTable);
}
}
}
return CustomMap.MapLancamento(dataTable);
}
}
}
|