public void ProcesarRegistroCorreo() { try { Da_Comprobante daComprobante = new Da_Comprobante(); List <En_Comprobante> comprobantes = daComprobante.ComprobantesPendientesDeEnvio(); if (comprobantes.Count > 0) { log.InfoFormat("Se inicia el registro de correos, cantidad: {0}.", comprobantes.Count()); Task[] taskArray = new Task[comprobantes.Count]; int i = 0; foreach (En_Comprobante comprobante in comprobantes) { En_Comprobante comprobanteParam = comprobante; taskArray[i] = Task.Factory.StartNew(() => RegistrarCorreo(comprobanteParam)); i += 1; } Task.WaitAll(taskArray.ToArray()); log.InfoFormat("Se ha terminado el registro de los correos, cantidad: {0}.", comprobantes.Count()); } } catch (Exception ex) { log.Error(ex.Message.ToString(), ex); } }
public void ProcesarEnviarComprobantes() { try { Ad_Comprobante adComprobante = new Ad_Comprobante(); List <En_Comprobante> comprobantes = adComprobante.ComprobantesPendientesDeEnvio(); if (comprobantes.Count > 0) { Task[] taskArray = new Task[comprobantes.Count]; int i = 0; foreach (En_Comprobante comprobante in comprobantes) { En_Comprobante comprobanteParam = comprobante; taskArray[i] = Task.Factory.StartNew(() => EnviarComprobante(comprobanteParam)); i += 1; } Task.WaitAll(taskArray.ToArray()); } } catch (Exception ex) { log.Error(ex.Message.ToString(), ex); } }
public void EnviarComprobante(En_Comprobante comprobante) { long idComprobante = comprobante.IdComprobante; Ad_Archivo adArchivo = new Ad_Archivo(); Ad_Comprobante adComprobante = new Ad_Comprobante(); En_Archivo archivo = adArchivo.ObtenerArchivoComprobante(idComprobante); string nombre = archivo.Nombre; byte[] contenido = archivo.Contenido; string carpetaTemporal = adArchivo.ObtenerRutaTemporal("TempENVIO"); string carpetaArchivo = Path.GetFileNameWithoutExtension(nombre); string rutaCarpetaXML = Path.Combine(carpetaTemporal, carpetaArchivo); string rutaArchivoXML = Path.Combine(rutaCarpetaXML, nombre); string rutaArchivoZIP = string.Concat(rutaCarpetaXML, ".zip"); string nombreArchivoZIP = Path.GetFileName(rutaArchivoZIP); string nombreArchivoZipResponse = string.Concat("R-", nombreArchivoZIP); string rutaZipResponse = Path.Combine(carpetaTemporal, nombreArchivoZipResponse); CrearCarpeta(rutaCarpetaXML); CrearArchivo(rutaArchivoXML, contenido); Comprimir(rutaArchivoZIP, rutaCarpetaXML); EliminarCarpeta(rutaCarpetaXML); ServicePointManager.ServerCertificateValidationCallback = (snder, cert, chain, error) => true; byte[] archivoZip = File.ReadAllBytes(rutaArchivoZIP); wsSUNAT.sendBillRequest sendBill = new wsSUNAT.sendBillRequest(); wsSUNAT.billServiceClient billService = new wsSUNAT.billServiceClient(); try { billService.Open(); byte[] archivoResponse = billService.sendBill(nombreArchivoZIP, archivoZip, ""); adComprobante.InsertarCdrPendiente(idComprobante, archivoResponse); billService.Close(); log.Info(string.Format("El comprobante {0}-{1} de la empresa emisora con ruc: {2} se procesó correctamente.", comprobante.TipoComprobante, comprobante.SerieNumero, comprobante.RucEmisor)); } catch (FaultException ex) { if (billService.State == CommunicationState.Opened) { billService.Close(); } string codigo = ex.Code.Name.ToLower().Replace("client.", ""); string mensaje = ex.Message.ToString(); int reintento = adComprobante.QuitarPendienteEnvio(idComprobante, codigo); string mensajeReintento = (reintento == 1) ? "Se dejará de reintentar el envío de éste comprobante." : ""; log.Error(string.Format("El comprobante {0}-{1} de la empresa emisora con ruc: {2} obtuvo el código de error \"{3}\" con mensaje \"{4}\". {5}", comprobante.TipoComprobante, comprobante.SerieNumero, comprobante.RucEmisor, codigo, mensaje, mensajeReintento)); } EliminarArchivo(rutaZipResponse); EliminarArchivo(rutaArchivoZIP); }
public List <En_Comprobante> ComprobantesPendientesDeEnvio() { List <En_Comprobante> comprobantes = new List <En_Comprobante>(); SqlConnection cn = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand("dbo.usp_ListarComprobantesEnvioCorreo", cn) { CommandType = CommandType.StoredProcedure }; try { cn.Open(); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { En_Comprobante comprobante = new En_Comprobante { RucEmisor = (string)(dr[0]), IdComprobante = (long)dr[1], TipoComprobante = (string)dr[2], SerieNumero = (string)dr[3], CorreoElectronico = (string)dr[4] }; comprobantes.Add(comprobante); } cn.Close(); return(comprobantes); } catch (SqlException ex) { if (cn.State == ConnectionState.Open) { cn.Close(); } throw ex; } catch (Exception ex) { if (cn.State == ConnectionState.Open) { cn.Close(); } throw ex; } }
private void RegistrarCorreo(En_Comprobante comprobante) { try { string correoEmisor = ConfigurationManager.AppSettings["correoEmisor"]; string tipoComprobante = ""; switch (comprobante.TipoComprobante) { case "01": tipoComprobante = "Factura"; break; case "03": tipoComprobante = "Boleta"; break; case "07": tipoComprobante = "Nota de crédito"; break; case "08": tipoComprobante = "Nota de débito"; break; } string asunto = string.Format(ConfigurationManager.AppSettings["asunto"], tipoComprobante, comprobante.SerieNumero); Da_Correo daCorreo = new Da_Correo(); En_Correo enCorreo = new En_Correo { De = correoEmisor, Para = comprobante.CorreoElectronico, IdComprobante = comprobante.IdComprobante, Asunto = asunto }; daCorreo.InsertarComprobanteCorreo(enCorreo); log.InfoFormat("Se registró el correo para el comprobante {0}-{1} de la empresa emisora con ruc: {2}.", comprobante.TipoComprobante, comprobante.SerieNumero, comprobante.RucEmisor); } catch (Exception ex) { string mensajeError = string.Format("Ocurrió un error en el registro del correo para el comprobante {0}-{1} de la empresa emisora con ruc: {2}.", comprobante.TipoComprobante, comprobante.SerieNumero, comprobante.RucEmisor); log.Error(mensajeError, ex); } }