using System; using System.Collections.Generic; using System.Configuration; using System.Data.Entity.Validation; using System.IO; using System.Linq; using System.Web; using System.Web.Http; using System.Text; using ULA.Davivienda.PrendarioModel.Prendario; using ULA.Davivienda.PrestamoAutos.Commons; using ULA.Davivienda.PrestamoAutos.Json; using ULA.Davivienda.PrestamoAutos.WCFAttachmentService; using ULA.Davivienda.PrestamoAutos.ServiciosCobisCarteraPrendario; using Ultimus.Utilitarios; namespace ULA.Davivienda.PrestamoAutos.ControllersWebApi { public class FirmaDocumentosAPIController : ApiController { LogFile log = new LogFile(); [HttpGet] public bool AdjuntoPrendaFirmada(string process, int solicitud) { try { AttachmentServiceClient client = new AttachmentServiceClient(); string[] files = client.GetFileList(process, solicitud.ToString() + "/PrendaFirmada"); return files.Length > 0; } catch (Exception ex) { log.EscribirArchivo("ULA.Davivienda.PrestamoAutos", ex); return false; } } #region GuardarDatos [HttpPost] public JsonResponseBase GuardarDatos([FromBody] DataFaseII Model) { JsonResponseBase respuesta = new JsonResponseBase(); try { using (PrendarioContext db = new PrendarioContext()) { DataFaseII datafaseII = db.DataFaseII.Where(a => a.IdSolicitud == Model.IdSolicitud).FirstOrDefault(); if (datafaseII != null) { datafaseII.ClienteFirmo = Model.ClienteFirmo; datafaseII.ClienteNoFirmo = Model.ClienteNoFirmo; datafaseII.CumpleCondiciones = Model.CumpleCondiciones; datafaseII.NoCumpleCondiciones = Model.NoCumpleCondiciones; db.Entry(datafaseII).State = System.Data.Entity.EntityState.Modified; } db.SaveChanges(); respuesta.CodigoRespuesta = TipoCodigoRespuesta.EXITOSO.ToString(); } } catch (DbEntityValidationException ex) { var errorMessages = ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage); var fullErrorMessage = string.Join("; ", errorMessages); var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage); UltimusLogs.logger.Error(exceptionMessage); respuesta.CodigoRespuesta = TipoCodigoRespuesta.ERROR.ToString(); respuesta.DescripcionRespuesta = exceptionMessage; log.EscribirArchivo("ULA.Davivienda.PrestamoAutos", exceptionMessage); } catch (Exception ex) { log.EscribirArchivo("ULA.Davivienda.PrestamoAutos", ex); respuesta.CodigoRespuesta = TipoCodigoRespuesta.ERROR.ToString(); } return respuesta; } #endregion #region Enviar [HttpPost] public JsonResponseBase Enviar([FromBody] JsonEnviarInforma _model) { JsonResponseBase respuesta = new JsonResponseBase(); try { using (PrendarioContext db = new PrendarioContext()) { Solicitud _modelSolicitud = db.Solicitud.Where(e => e.IdSolicitud == _model.IdSolicitud).FirstOrDefault(); _modelSolicitud.AplicaIntegracionConCartera = _modelSolicitud.AplicaIntegracionConCartera.HasValue ? _modelSolicitud.AplicaIntegracionConCartera : false; if (_modelSolicitud.AplicaIntegracionConCartera.Value) { try { log.EscribirArchivo("ULA.Davivienda.PrestamoAutos", "Flaq 1 => " + _model.IdDecision); log.EscribirArchivo("ULA.Davivienda.PrestamoAutos", "Flaq 2 => " + Convert.ToString((int)EnumDecision.IrAInforma)); if (_model.IdDecision == Convert.ToString((int)EnumDecision.IrAInforma)) { DataFaseII df2 = db.DataFaseII.Where(q => q.IdSolicitud == _model.IdSolicitud).FirstOrDefault(); if (df2 == null) { respuesta.CodigoRespuesta = TipoCodigoRespuesta.ADVERTENCIA.ToString(); respuesta.DescripcionRespuesta = "DataFaseII no existe"; return respuesta; } log.EscribirArchivo("ULA.Davivienda.PrestamoAutos", "Flaq 3"); //Si no hay operacion registrada en Ultimus entonces no invoca el servicio if (!string.IsNullOrWhiteSpace(df2.NoOperacion)) { log.EscribirArchivo("ULA.Davivienda.PrestamoAutos", "Flaq 4"); string xmlPath = string.Format("{0}\\AnularOperaciones.xml", ConfigurationManager.AppSettings["XmlPath"]); string xmlString = File.ReadAllText(xmlPath); xmlString = xmlString.Replace("", string.Format("{0}", df2.NoOperacion)); xmlString = xmlString.Replace("", string.Format("{0}", "")); log.EscribirArchivo("ULA.Davivienda.PrestamoAutos", "Flaq 5 => " + xmlString); using (CarteraPrendarioClient client = new CarteraPrendarioClient()) { AnularOperacionesRequest req = new AnularOperacionesRequest(); req.Proceso = _model.Process; req.Solicitud = Convert.ToString(_model.IdSolicitud); req.strXML = xmlString; req.Usuario = _model.UserId; AnularOperacionesResponse res = client.AnularOperaciones(req); log.EscribirArchivo("ULA.Davivienda.PrestamoAutos", "Flaq 6 => " + res.resultado); if (res.errCode == "99") throw new Exception(res.errMessage); } } log.EscribirArchivo("ULA.Davivienda.PrestamoAutos", "Flaq 7"); } log.EscribirArchivo("ULA.Davivienda.PrestamoAutos", "Flaq 8"); } catch (Exception ex) { //TODO - El error en COBIS deberá ser bloqueante respuesta.CodigoRespuesta = TipoCodigoRespuesta.ERROR.ToString(); log.EscribirArchivo("ULA.Davivienda.PrestamoAutos", string.Format("Error en COBIS-AnularOperacionesRequest:{0}", ex.ToString())); return respuesta; } } log.EscribirArchivo("ULA.Davivienda.PrestamoAutos", "Flaq 9"); GlobalAPIController global = new GlobalAPIController(); respuesta = global.Enviar(_model); } } catch (Exception ex) { log.EscribirArchivo("ULA.Davivienda.PrestamoAutos", ex); respuesta.CodigoRespuesta = TipoCodigoRespuesta.ERROR.ToString(); } return respuesta; } #endregion #region ObtenerBoleta [HttpPost] public JsonGenerarBoletaResponse ObtenerBoleta([FromBody] JsonGenerarBoletaRequest _model) { JsonGenerarBoletaResponse respuesta = null; string asp= string.Empty; string fecha = string.Empty; string server = string.Empty; string num_objeto= string.Empty; string cod_cliente_boleta_seguros = string.Empty; string cod_cliente_boleta_op_desembolso = string.Empty; string instrBoletaSeguros = string.Empty; string instrBoletaOperacionesDesembolso = string.Empty; string[] arrUrlBoleta = new string[2] { "", "" }; try { respuesta = new JsonGenerarBoletaResponse(); server = ConfigurationManager.AppSettings["BoletaMensajeria.Server"]; asp = ConfigurationManager.AppSettings["BoletaMensajeria.UrlPage"]; num_objeto = ConfigurationManager.AppSettings["BoletaMensajeria.NumeroObjeto"]; cod_cliente_boleta_seguros = ConfigurationManager.AppSettings["BoletaMensajeria.CodigoClienteBoletaSeguro"]; cod_cliente_boleta_op_desembolso = ConfigurationManager.AppSettings["BoletaMensajeria.CodigoClienteBoletaOperacionesDesembolso"]; instrBoletaSeguros = ConfigurationManager.AppSettings["BoletaMensajeria.InstruccionBoletaSeguro"]; instrBoletaOperacionesDesembolso = ConfigurationManager.AppSettings["BoletaMensajeria.InstruccionBoletaOperacionesDesembolso"]; instrBoletaSeguros += _model.ClientName; instrBoletaOperacionesDesembolso += _model.ClientName; string dia = System.DateTime.Today.Day.ToString(); string mes = System.DateTime.Today.Month.ToString(); string anio = System.DateTime.Today.Year.ToString(); if (dia.Length < 2) dia = "0" + dia; if (mes.Length < 2) mes = "0" + mes; fecha = dia + "/" + mes + "/" + anio; arrUrlBoleta[0] = "http://" + server + "/" + asp + "usuremitente=" + _model.UserId + "&fecEjecucion=" + fecha + "&numobjeto=" + num_objeto + "&instrucciones=" + instrBoletaSeguros + "&codCliente=" + cod_cliente_boleta_seguros; arrUrlBoleta[1] = "http://" + server + "/" + asp + "usuremitente=" + _model.UserId + "&fecEjecucion=" + fecha + "&numobjeto=" + num_objeto + "&instrucciones=" + instrBoletaOperacionesDesembolso + "&codCliente=" + cod_cliente_boleta_op_desembolso; respuesta.UrlBoletaSeguros = arrUrlBoleta[0]; respuesta.UrlBoletaOperacionesDesembolso = arrUrlBoleta[1]; respuesta.CodigoRespuesta = TipoCodigoRespuesta.EXITOSO.ToString(); } catch (Exception ex) { log.EscribirArchivo("ULA.Davivienda.PrestamoAutos", ex); respuesta.CodigoRespuesta = TipoCodigoRespuesta.ERROR.ToString(); } return respuesta; } #endregion #region ObtenerNumeroBoleta [HttpPost] public JsonGenerarBoletaResponse ObtenerNumeroBoleta([FromBody] JsonGenerarBoletaRequest _model) { JsonGenerarBoletaResponse respuesta = null; string numObjeto = string.Empty; int[] arrNumeroBoleta = new int[2] { 0, 0 }; InterfacesAPIController interfaces = null; WCFServiciosGenerales.ObtenerNumeroBoletaResponse resp = null; try { interfaces = new InterfacesAPIController(); respuesta = new JsonGenerarBoletaResponse(); numObjeto = ConfigurationManager.AppSettings["BoletaMensajeria.NumeroObjeto"]; resp = interfaces.ObtenerNumeroBoleta(new WCFServiciosGenerales.ObtenerNumeroBoletaRequest() { NumObjeto = numObjeto, Proceso = WCFServiciosGenerales.EnumeradorProcesoUltimus.PrestamoPrendario, Usuario = _model.UserId }); if (resp.errCode == TipoCodigoRespuesta.ERROR.ToString()) { throw new Exception(resp.errMessage); } if (resp.NumeroBoleta != 0) { arrNumeroBoleta[0] = resp.NumeroBoleta - 1; arrNumeroBoleta[1] = resp.NumeroBoleta; } respuesta.NumeroBoletaSeguros = arrNumeroBoleta[0]; respuesta.NumeroBoletaOperacionesDesembolso = arrNumeroBoleta[1]; respuesta.CodigoRespuesta = TipoCodigoRespuesta.EXITOSO.ToString(); } catch (Exception ex) { log.EscribirArchivo("ULA.Davivienda.PrestamoAutos", ex); respuesta.CodigoRespuesta = TipoCodigoRespuesta.ERROR.ToString(); } return respuesta; } #endregion } }