using System; using System.Collections.Generic; using System.Linq; using ULA.Cathay.CreditoModel.Credito; using Ultimus.Utilitarios; using System.Data.Entity; using System.Data.Entity.SqlServer; namespace ULA.Cathay.CreditoModel.DAL.Credito { public class ControlCarteraDAL { private UltimusLogs logs = new UltimusLogs("ControlCarteraDAL"); public List Obtener(string IdLinea = null, int? TotalDiasVencimiento = null, bool? RenovarVencidas = null, int? Idsolicitud=null) { List model = new List(); try { DateTime ToDay = DateTime.Now; using (CreditoContex db = new CreditoContex()) { if (!string.IsNullOrWhiteSpace(IdLinea)) { model = db.ControlCartera.Where(c => c.IdLinea == IdLinea && c.SolicitudGenerada != true && c.IdSolicitud==Idsolicitud).ToList(); } else if (TotalDiasVencimiento.HasValue) { model = ( from cc in db.ControlCartera.Include(c => c.Moneda) where cc.SolicitudGenerada != true && cc.FechaVencimiento >= ToDay && SqlFunctions.DateDiff("day", ToDay, cc.FechaVencimiento) <= TotalDiasVencimiento select cc).ToList(); } } } catch (Exception ex) { logs.Error(ex); throw ex; } return model; } public ControlCartera Obtener(int IdSolicitud) { ControlCartera respuesta = new ControlCartera(); try { using(CreditoContex db = new CreditoContex()) { respuesta = db.ControlCartera.Where(x => x.IdSolicitud == IdSolicitud).FirstOrDefault(); } } catch (Exception ex) { logs.Error(ex); throw ex; } return respuesta; } public ControlCartera InsertarActualizar(ControlCartera model) { try { using (CreditoContex db = new CreditoContex()) { ControlCartera actual = db.ControlCartera.AsNoTracking().Where(c => c.IdLinea == model.IdLinea && c.SolicitudGenerada == false && c.Incidente == null && c.IdSolicitud == null && c.FechaIncidente == null).FirstOrDefault(); if (actual != null) { model.IdCartera = actual.IdCartera; db.Entry(actual).State = System.Data.Entity.EntityState.Detached; db.ControlCartera.Attach(model); db.Entry(model).State = System.Data.Entity.EntityState.Modified; } else { long? Next_id; long idActual; Next_id = db.ControlCartera.Max(t => (long?)t.IdCartera); idActual = (Next_id == null) ? 0 : Convert.ToInt32(Next_id); model.IdCartera = (idActual + 1); db.ControlCartera.Add(model); } db.SaveChanges(); } } catch (Exception ex) { logs.Error(ex); throw ex; } return model; } public ControlCartera Actualizar(ControlCartera model) { try { using (CreditoContex db = new CreditoContex()) { List lista = Obtener(model.IdLinea,null,null,model.IdSolicitud); if (lista != null && lista.Count > 0) { foreach (var registrocartera in lista) { registrocartera.UsuarioCreador = model.UsuarioCreador; registrocartera.Incidente = model.Incidente; registrocartera.FechaIncidente = DateTime.Now; registrocartera.SolicitudGenerada = true; db.Entry(registrocartera).State = System.Data.Entity.EntityState.Modified; } db.SaveChanges(); } } } catch (Exception ex) { logs.Error(ex); throw ex; } return model; } } }