TFS_ANTIGUO/SInCathay/ULA.Cathay.CreditoModel.DAL/Credito/DesembolsoFacturasDAL.cs

94 lines
2.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Ultimus.Utilitarios;
using ULA.Cathay.CreditoModel.Credito;
using System.Data.Entity;
using System.Data.Entity.Validation;
namespace ULA.Cathay.CreditoModel.DAL.Credito
{
public class DesembolsoFacturasDAL
{
UltimusLogs logs = new UltimusLogs("DesembolsoFacturasDAL");
public DesembolsoDatosGenerales InsertarActualizar(DesembolsoDatosGenerales model)
{
try
{
using (CreditoContex db = new CreditoContex())
{
var actuales = db.DesembolsoFacturas.AsNoTracking().Where(c => c.IdSolicitud == model.IdSolicitud);
foreach (var item in model.DesembolsoFacturas)
{
item.ClearVirtualProperties();
if (actuales == null || !actuales.Any(c => c.IdDesembolsoFacturas == item.IdDesembolsoFacturas))
{
item.FechaCreacion = DateTime.Now;
db.Set(item.GetType()).Add(item);
}
else
{
db.Set(item.GetType()).Attach(item);
db.Entry(item).State = EntityState.Modified;
}
}
db.SaveChanges();
if (actuales != null)
{
foreach (var item in actuales)
{
item.ClearVirtualProperties();
if (!model.DesembolsoFacturas.Any(c => c.IdDesembolsoFacturas == item.IdDesembolsoFacturas))
{
db.Set(item.GetType()).Attach(item);
db.Set(item.GetType()).Remove(item);
}
}
}
db.SaveChanges();
}
}
catch (DbEntityValidationException ex)
{
logs.Error(ex);
throw ex;
}
catch (Exception ex)
{
logs.Error(ex);
throw ex;
}
return model;
}
public List<DesembolsoFacturas> Obtener(int IdSolicitud)
{
List<DesembolsoFacturas> model = null;
try
{
using (CreditoContex db = new CreditoContex())
{
model = db.DesembolsoFacturas.Include("Moneda").Where(c => c.IdSolicitud == IdSolicitud).ToList();
}
}
catch (Exception ex)
{
logs.Error(ex);
throw ex;
}
return model;
}
}
}