public async Task <string> recepcionMesajeHacienda() { string responsePost = ""; try { Thread.CurrentThread.CurrentCulture = Utilidades.getCulture(); string xml = await Request.Content.ReadAsStringAsync(); DocumentoElectronico documento = (DocumentoElectronico)EncodeXML.EncondeXML.getObjetcFromXML(xml); documento.verificaDatosParaXML(); EmisorReceptorIMEC elEmisor = null; using (var conexion = new DataModelFE()) { elEmisor = conexion.EmisorReceptorIMEC.Find(documento.emisor.identificacion.numero); if (elEmisor == null) { return("Emisor no registrado!!!"); } } responsePost = await ServicesHacienda.enviarDocumentoElectronico(false, documento, elEmisor, documento.tipoDocumento, Usuario.USUARIO_AUTOMATICO); } catch (DbEntityValidationException ex) { // Retrieve the error messages as a list of strings. var errorMessages = ex.EntityValidationErrors .SelectMany(x => x.ValidationErrors) .Select(x => x.ErrorMessage); // Join the list to a single string. var fullErrorMessage = string.Join("; ", errorMessages); // Throw a new DbEntityValidationException with the improved exception message. return(fullErrorMessage); } catch (Exception ex) { return(ex.Message); } return(responsePost); }
public async Task <HttpResponseMessage> recepcionmesajehacienda() { string responsePost = ""; try { Thread.CurrentThread.CurrentCulture = Utilidades.getCulture(); string xml = await Request.Content.ReadAsStringAsync(); DocumentoElectronico documento = (DocumentoElectronico)EncodeXML.XMLUtils.getObjetcFromXML(xml); documento.verificaDatosParaXML(); EmisorReceptorIMEC elEmisor = null; using (var conexion = new DataModelFE()) { long idE = long.Parse(documento.emisor.identificacion.numero); elEmisor = conexion.EmisorReceptorIMEC.Find(idE.ToString()); if (elEmisor == null) { //return "Emisor no registrado!!!"; return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Emisor no registrado!!!")); //return Ok("Emisor no registrado!!!"); } else// si el xml es de un emisor valido, verifica si existe el receptor y si no lo crea { string tipo = ""; string idReceptor = ""; if (!string.IsNullOrWhiteSpace(documento.receptor.identificacionExtranjero)) { tipo = "99"; idReceptor = documento.receptor.identificacionExtranjero; } else { //elimina ceros a la izquierda long idR = long.Parse(documento.receptor.identificacion.numero); tipo = documento.receptor.identificacion.tipo; idReceptor = idR.ToString(); } EmisorReceptorIMEC elReceptor = conexion.EmisorReceptorIMEC.Find(idReceptor); if (elReceptor == null) { // crea un cliente para que se muestre el emisor elReceptor = new EmisorReceptorIMEC(); elReceptor.identificacion = idReceptor; elReceptor.identificacionTipo = tipo; elReceptor.correoElectronico = documento.receptor.correoElectronico; if (documento.receptor.telefono != null) { elReceptor.telefonoCodigoPais = documento.receptor.telefono.codigoPais; elReceptor.telefono = documento.receptor.telefono.numTelefono; } elReceptor.nombre = documento.receptor.nombre; elReceptor.nombreComercial = documento.receptor.nombreComercial; conexion.EmisorReceptorIMEC.Add(elReceptor); conexion.SaveChanges(); } } } responsePost = await ServicesHacienda.enviarDocumentoElectronico(false, documento, elEmisor, documento.tipoDocumento, Usuario.USUARIO_AUTOMATICO); } catch (DbEntityValidationException ex) { // Retrieve the error messages as a list of strings. var errorMessages = ex.EntityValidationErrors .SelectMany(x => x.ValidationErrors) .Select(x => x.ErrorMessage); // Join the list to a single string. var fullErrorMessage = string.Join("; ", errorMessages); // Throw a new DbEntityValidationException with the improved exception message. //return fullErrorMessage; return(Request.CreateErrorResponse(HttpStatusCode.NotFound, fullErrorMessage)); //return Ok(fullErrorMessage); } catch (Exception ex) { //return ex.Message; return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex.Message)); //return Ok(ex.Message); } //return responsePost; return(Request.CreateErrorResponse(HttpStatusCode.NotFound, responsePost)); //return Ok(responsePost); }