363 lines
14 KiB
C#
363 lines
14 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Data.Entity.Validation;
|
|||
|
|
using System.Data.Entity;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Web.Http;
|
|||
|
|
using ULA.Davivienda.PrendarioModel;
|
|||
|
|
using ULA.Davivienda.PrendarioModel.Prendario;
|
|||
|
|
using ULA.Davivienda.PrestamoAutos.Commons;
|
|||
|
|
using ULA.Davivienda.PrestamoAutos.CotizadorServiceLayer;
|
|||
|
|
using ULA.Davivienda.PrestamoAutos.Json;
|
|||
|
|
using ULA.Davivienda.PrestamoAutos.WCFPrendarioSeguros;
|
|||
|
|
using Ultimus.Utilitarios;
|
|||
|
|
|
|||
|
|
namespace ULA.Davivienda.PrestamoAutos.ControllersWebApi
|
|||
|
|
{
|
|||
|
|
public class MotivosAPIController : ApiController
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
Crypt crypt = new Crypt();
|
|||
|
|
LogFile log = new LogFile();
|
|||
|
|
|
|||
|
|
|
|||
|
|
#region ObtenerMotivo
|
|||
|
|
[HttpGet]
|
|||
|
|
public List<JsonCatalogos> ObtenerMotivo(int? idTipoMotivo = null)
|
|||
|
|
{
|
|||
|
|
List<JsonCatalogos> listaMotivo = null;
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
listaMotivo = new List<JsonCatalogos>();
|
|||
|
|
List<Motivo> Motivo = new List<Motivo>();
|
|||
|
|
|
|||
|
|
using (PrendarioContext db = new PrendarioContext())
|
|||
|
|
{
|
|||
|
|
if (idTipoMotivo == null)
|
|||
|
|
Motivo = db.Motivo.Where(e=> e.Estado == true).ToList();
|
|||
|
|
else
|
|||
|
|
Motivo = db.Motivo.Where(e => e.IdTipoMotivo == idTipoMotivo && e.Estado == true).ToList();
|
|||
|
|
|
|||
|
|
foreach (var n in Motivo)
|
|||
|
|
{
|
|||
|
|
JsonCatalogos catalogo = new JsonCatalogos();
|
|||
|
|
catalogo.codigo = n.IdMotivo.ToString().Trim();
|
|||
|
|
catalogo.descripcion = n.Descripcion.ToString().Trim();
|
|||
|
|
listaMotivo.Add(catalogo);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
log.EscribirArchivo("ULA.Davivienda.PrestamoAutos", ex);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return listaMotivo;
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
#region InsertarMotivoSolicitud
|
|||
|
|
[HttpPost]
|
|||
|
|
public string InsertarMotivoSolicitud(MotivoSolicitud _model)
|
|||
|
|
{
|
|||
|
|
int? Next_id = null;
|
|||
|
|
int? Next_idSec = null;
|
|||
|
|
|
|||
|
|
int idActual = 0;
|
|||
|
|
int idActualSec= 0;
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
using (PrendarioContext db = new PrendarioContext())
|
|||
|
|
{
|
|||
|
|
//IdMotivoSolicitud
|
|||
|
|
idActual = _model.IdMotivoSolicitud;
|
|||
|
|
if (idActual == 0)
|
|||
|
|
{
|
|||
|
|
Next_id = db.MotivoSolicitud.Max(t => (int?)t.IdMotivoSolicitud);
|
|||
|
|
idActual = (Next_id == null) ? 0 : Convert.ToInt32(Next_id);
|
|||
|
|
idActual++;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//IdMotivoSolicitudSec
|
|||
|
|
Next_idSec = db.MotivoSolicitud.Where(e=> e.IdMotivoSolicitud == idActual).Max(t => (int?)t.IdMotivoSolicitudSec);
|
|||
|
|
idActualSec = (Next_idSec == null) ? 0 : Convert.ToInt32(Next_idSec);
|
|||
|
|
idActualSec++;
|
|||
|
|
|
|||
|
|
|
|||
|
|
var IsIdRegistered = db.MotivoSolicitud.Where(y => y.IdSolicitud == _model.IdSolicitud
|
|||
|
|
&& y.EtapaOrigen == _model.EtapaOrigen
|
|||
|
|
&& y.Estado == _model.Estado
|
|||
|
|
&& y.Motivo.IdMotivo == _model.IdMotivo).Select(e => true).FirstOrDefault();
|
|||
|
|
|
|||
|
|
if (IsIdRegistered != true)
|
|||
|
|
{
|
|||
|
|
_model.IdMotivoSolicitud = idActual;
|
|||
|
|
_model.IdMotivoSolicitudSec = idActualSec;
|
|||
|
|
_model.FeRegistro = DateTime.Now;
|
|||
|
|
db.MotivoSolicitud.Add(_model);
|
|||
|
|
db.SaveChanges();
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
log.EscribirArchivo("ULA.Davivienda.PrestamoAutos", ex);
|
|||
|
|
|
|||
|
|
return TipoCodigoRespuesta.ERROR.ToString();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return TipoCodigoRespuesta.EXITOSO.ToString();
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
|
|||
|
|
#region InsertarMotivoSolicitudLista
|
|||
|
|
[HttpPost]
|
|||
|
|
public string InsertarMotivoSolicitudLista([FromBody] List<MotivoSolicitud> _model)
|
|||
|
|
{
|
|||
|
|
int? Next_id = null;
|
|||
|
|
int? Next_idSec = null;
|
|||
|
|
|
|||
|
|
int idActual = 0;
|
|||
|
|
int idActualSec = 0;
|
|||
|
|
|
|||
|
|
List<MotivoSolicitud> _modelDelete = null;
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
using (PrendarioContext db = new PrendarioContext())
|
|||
|
|
{
|
|||
|
|
//obtiene el IdMotivoSolicitud que viene cargado en el item cero(item pivot).
|
|||
|
|
idActual = _model[0].IdMotivoSolicitud;
|
|||
|
|
|
|||
|
|
//Si no existe un IdMotivoSolicitud crea un nuevo Id.
|
|||
|
|
//Si existe un IdMotivoSolicitud elimina todos los motivos asociados a ese Id.
|
|||
|
|
if (idActual == 0)
|
|||
|
|
{
|
|||
|
|
Next_id = db.MotivoSolicitud.Max(t => (int?)t.IdMotivoSolicitud);
|
|||
|
|
idActual = (Next_id == null) ? 0 : Convert.ToInt32(Next_id);
|
|||
|
|
idActual++;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
_modelDelete = db.MotivoSolicitud.Where(x => x.IdMotivoSolicitud == idActual).ToList();
|
|||
|
|
db.MotivoSolicitud.RemoveRange(_modelDelete);
|
|||
|
|
db.SaveChanges();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
//Remueve el indice cero ya que es solo pivot
|
|||
|
|
_model.RemoveAt(0);
|
|||
|
|
|
|||
|
|
|
|||
|
|
//IdMotivoSolicitudSec
|
|||
|
|
Next_idSec = db.MotivoSolicitud.Where(e => e.IdMotivoSolicitud == idActual).Max(t => (int?)t.IdMotivoSolicitud);
|
|||
|
|
idActualSec = (Next_idSec == null) ? 0 : Convert.ToInt32(Next_idSec);
|
|||
|
|
|
|||
|
|
//Inserta los diferentes motivos de la lista de entrada
|
|||
|
|
foreach (MotivoSolicitud _modelItem in _model)
|
|||
|
|
{
|
|||
|
|
idActualSec++;
|
|||
|
|
_modelItem.IdMotivoSolicitud = idActual;
|
|||
|
|
_modelItem.IdMotivoSolicitudSec = idActualSec;
|
|||
|
|
_modelItem.FeRegistro = DateTime.Now;
|
|||
|
|
db.MotivoSolicitud.Add(_modelItem);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
db.SaveChanges();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
log.EscribirArchivo("ULA.Davivienda.PrestamoAutos", ex);
|
|||
|
|
|
|||
|
|
return TipoCodigoRespuesta.ERROR.ToString();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return TipoCodigoRespuesta.EXITOSO.ToString();
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
|
|||
|
|
#region ObtenerMotivoSolicitud
|
|||
|
|
[HttpGet]
|
|||
|
|
public List<MotivoSolicitud> ObtenerMotivoSolicitud(int? IdSolicitud = null,
|
|||
|
|
string EtapaOrigen = "",
|
|||
|
|
int? IdTipoMotivo = null,
|
|||
|
|
int? Estado = null,
|
|||
|
|
string OrigenOrDestino = "")
|
|||
|
|
{
|
|||
|
|
List<MotivoSolicitud> resp = null;
|
|||
|
|
int? maxId = null;
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
resp = new List<MotivoSolicitud>();
|
|||
|
|
|
|||
|
|
using (PrendarioContext db = new PrendarioContext())
|
|||
|
|
{
|
|||
|
|
List<MotivoSolicitud> _listaMotivoSolicitud = null;
|
|||
|
|
|
|||
|
|
switch(OrigenOrDestino.ToUpper())
|
|||
|
|
{
|
|||
|
|
case "SOURCE":
|
|||
|
|
|
|||
|
|
_listaMotivoSolicitud = db.MotivoSolicitud.Where(y => y.IdSolicitud == IdSolicitud
|
|||
|
|
&& y.EtapaOrigen == EtapaOrigen
|
|||
|
|
&& y.Estado == Estado
|
|||
|
|
&& y.Motivo.IdTipoMotivo == IdTipoMotivo).ToList();
|
|||
|
|
|
|||
|
|
if (_listaMotivoSolicitud.Count > 0)
|
|||
|
|
{
|
|||
|
|
maxId = _listaMotivoSolicitud.Max(t => (int?)t.IdMotivoSolicitud);
|
|||
|
|
|
|||
|
|
resp = db.MotivoSolicitud
|
|||
|
|
.Include(e => e.Motivo)
|
|||
|
|
.Where(y => y.IdSolicitud == IdSolicitud
|
|||
|
|
&& y.EtapaOrigen == EtapaOrigen
|
|||
|
|
&& y.Estado == Estado
|
|||
|
|
&& y.Motivo.IdTipoMotivo == IdTipoMotivo
|
|||
|
|
&& y.IdMotivoSolicitud == maxId).ToList();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
break;
|
|||
|
|
case "DESTINATION":
|
|||
|
|
|
|||
|
|
|
|||
|
|
_listaMotivoSolicitud = db.MotivoSolicitud.Where(y => y.IdSolicitud == IdSolicitud
|
|||
|
|
&& y.Estado == 3
|
|||
|
|
&& y.Motivo.IdTipoMotivo == IdTipoMotivo).ToList();
|
|||
|
|
|
|||
|
|
if (_listaMotivoSolicitud.Count > 0)
|
|||
|
|
{
|
|||
|
|
maxId = _listaMotivoSolicitud.Max(t => (int?)t.IdMotivoSolicitud);
|
|||
|
|
|
|||
|
|
resp = db.MotivoSolicitud
|
|||
|
|
.Include(e => e.Motivo)
|
|||
|
|
.Where(y => y.IdSolicitud == IdSolicitud
|
|||
|
|
&& y.Estado == 3
|
|||
|
|
&& y.Motivo.IdTipoMotivo == IdTipoMotivo
|
|||
|
|
&& y.IdMotivoSolicitud == maxId).ToList();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
log.EscribirArchivo("ULA.Davivienda.PrestamoAutos", ex);
|
|||
|
|
}
|
|||
|
|
return resp;
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
#region EliminarMotivoSolicitud
|
|||
|
|
[HttpPost]
|
|||
|
|
public string EliminarMotivoSolicitud(MotivoSolicitud _model)
|
|||
|
|
{
|
|||
|
|
MotivoSolicitud _modelDelete = null;
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
using (PrendarioContext db = new PrendarioContext())
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
//Obtiene el motivo de la solicitud
|
|||
|
|
_modelDelete = db.MotivoSolicitud
|
|||
|
|
.Where(y => y.IdMotivoSolicitud == _model.IdMotivoSolicitud
|
|||
|
|
&& y.IdMotivoSolicitudSec == _model.IdMotivoSolicitudSec)
|
|||
|
|
.FirstOrDefault();
|
|||
|
|
|
|||
|
|
|
|||
|
|
if (_modelDelete != null)
|
|||
|
|
db.MotivoSolicitud.Remove(_modelDelete);
|
|||
|
|
|
|||
|
|
|
|||
|
|
//Ejecuta el save en base de datos
|
|||
|
|
db.SaveChanges();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
log.EscribirArchivo("ULA.Davivienda.PrestamoAutos", ex);
|
|||
|
|
|
|||
|
|
return TipoCodigoRespuesta.ERROR.ToString();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return TipoCodigoRespuesta.EXITOSO.ToString();
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
#region ActualizarMotivoSolicitud
|
|||
|
|
[HttpPost]
|
|||
|
|
public string ActualizarMotivoSolicitud(MotivoSolicitud _model)
|
|||
|
|
{
|
|||
|
|
List<MotivoSolicitud> _modelListUpdate = null;
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
using (PrendarioContext db = new PrendarioContext())
|
|||
|
|
{
|
|||
|
|
if (_model.IdMotivoSolicitud == -1)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
_modelListUpdate = db.MotivoSolicitud
|
|||
|
|
.Include(t=> t.Motivo)
|
|||
|
|
.Where(t => t.IdSolicitud == _model.IdSolicitud
|
|||
|
|
&& t.EtapaOrigen == _model.EtapaOrigen
|
|||
|
|
&& t.Motivo.IdTipoMotivo == _model.Motivo.IdTipoMotivo).ToList();
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
_modelListUpdate = db.MotivoSolicitud.Where(t => t.IdMotivoSolicitud == _model.IdMotivoSolicitud).ToList();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
foreach (MotivoSolicitud _modelUpdate in _modelListUpdate)
|
|||
|
|
{
|
|||
|
|
_modelUpdate.Estado = _model.Estado;
|
|||
|
|
_modelUpdate.Comentario = _model.Comentario;
|
|||
|
|
|
|||
|
|
db.Entry(_modelUpdate).State = System.Data.Entity.EntityState.Modified;
|
|||
|
|
|
|||
|
|
db.SaveChanges();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
log.EscribirArchivo("ULA.Davivienda.PrestamoAutos", ex);
|
|||
|
|
|
|||
|
|
return TipoCodigoRespuesta.ERROR.ToString();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return TipoCodigoRespuesta.EXITOSO.ToString();
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|