private void StartMain() { if (!ConexionDBChequeada && !TestDbConection()) { return; } else { ConexionDBChequeada = true; } ////////////// if (!DatosContribuyente && !VerificarDatosContribuyente()) { return; } else { DatosContribuyente = true; } ////////////// if (DatosActividadesEconomicas && !VerificarDatosContribuyente()) { return; } else { DatosActividadesEconomicas = true; } ////////////// if (!DatosSMTP && !CheckSmtp()) { return; } else { DatosSMTP = true; } ListViewMenu.Visibility = Visibility.Visible; lb_mainUser.Text = RecursosSistema.Contribuyente.Nombre; RecursosSistema.IniciarServicioConsulta(); ShowHistorial(); CorrerMigraciones(); }
private void EnviarAHacienda(object sender, RoutedEventArgs e) { try { loadingDisplayer.Visibility = Visibility.Visible; FacturaResolucion.Resolucion = Convert.ToInt32(((ComboBoxItem)cb_Resolucion.SelectedItem).Tag); FacturaResolucion.DetalleResolucion = txt_DetalleResolucion.Text; Contribuyente_Consecutivos Consecutivo; using (db_FacturaDigital db = new db_FacturaDigital()) { Consecutivo = db.Contribuyente_Consecutivos.First(q => q.Id_Contribuyente == RecursosSistema.Contribuyente.Id_Contribuyente); FacturaResolucion.NumeroConsecutivo = FormatoConsecutivoAceptacion(Consecutivo.Consecutivo_Confirmacion, FacturaResolucion.Resolucion); FacturaResolucion.Email_Enviado = false; FacturaResolucion.Fecha_Documento = DateTime.Now; db.Factura_Resolucion.Add(FacturaResolucion); Consecutivo.Consecutivo_Confirmacion++; try { new FacturaDB_ToMensajeReceptor(RecursosSistema.Contribuyente).Convertir(FacturaResolucion) .CrearXml(Tipo_documento.Confirmación_aceptación) .Enviar(); db.SaveChanges(); RecursosSistema.WindosNotification("Confirmacion", "El documento Clave [" + FacturaResolucion.Clave + "] se envío para su valoración"); RecursosSistema.Servicio_AgregarFactura(FacturaResolucion.Clave + "-" + FacturaResolucion.NumeroConsecutivo); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } loadingDisplayer.Visibility = Visibility.Collapsed; } } catch (Exception ex) { loadingDisplayer.Visibility = Visibility.Collapsed; this.LogError(ex); MessageBox.Show("Error al enviar la respuesta a Hacienda", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void AnularFactura(object sender, RoutedEventArgs e) { try { if (string.IsNullOrEmpty(txt_MotivoAnulacion.Text)) { MessageBox.Show("Favor ingresar el motivo de la aunlacion antes de continuar", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop); return; } if (MessageBox.Show("Esta seguro de anular esta factura?", "", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes) { return; } int casaMatriz = 1; int PuntoVenta = 1; DateTime FechaEmicionDocumento = DateTime.Now; Factura Anulacion = new Factura() { Codigo_Moneda = "CRC", CondicionVenta = fac.CondicionVenta, Email_Enviado = false, CasaMatriz = casaMatriz, PuntoVenta = PuntoVenta, Emisor_CorreoElectronico = fac.Emisor_CorreoElectronico, Emisor_Identificacion_Numero = fac.Emisor_Identificacion_Numero, Emisor_Identificacion_Tipo = fac.Emisor_Identificacion_Tipo, Emisor_Nombre = fac.Emisor_Nombre, Emisor_NombreComercial = fac.Emisor_NombreComercial, Emisor_Telefono_Codigo = fac.Emisor_Telefono_Codigo, Emisor_Telefono_Numero = fac.Emisor_Telefono_Numero, Emisor_Ubicacion_Barrio = fac.Emisor_Ubicacion_Barrio, Emisor_Ubicacion_Canton = fac.Emisor_Ubicacion_Canton, Emisor_Ubicacion_Distrito = fac.Emisor_Ubicacion_Distrito, Emisor_Ubicacion_Provincia = fac.Emisor_Ubicacion_Provincia, Emisor_Ubicacion_OtrasSenas = fac.Emisor_Ubicacion_OtrasSenas, Fecha_Emision_Documento = FechaEmicionDocumento, Estado = (int)EstadoComprobante.Enviado, Id_Contribuyente = fac.Id_Contribuyente, Id_TipoDocumento = (int)Tipo_documento.Nota_de_crédito_electrónica, MedioPago = fac.MedioPago, Receptor_CorreoElectronico = fac.Receptor_CorreoElectronico, Receptor_Identificacion_Numero = fac.Receptor_Identificacion_Numero, Receptor_Identificacion_Tipo = fac.Receptor_Identificacion_Tipo, Receptor_Nombre = fac.Receptor_Nombre, Receptor_NombreComercial = fac.Receptor_NombreComercial, Receptor_Telefono_Codigo = fac.Receptor_Telefono_Codigo, Receptor_Telefono_Numero = fac.Receptor_Telefono_Numero, Receptor_Ubicacion_Barrio = fac.Receptor_Ubicacion_Barrio, Receptor_Ubicacion_Canton = fac.Receptor_Ubicacion_Canton, Receptor_Ubicacion_Distrito = fac.Receptor_Ubicacion_Distrito, Receptor_Ubicacion_OtrasSenas = fac.Receptor_Ubicacion_OtrasSenas, Receptor_Ubicacion_Provincia = fac.Receptor_Ubicacion_Provincia, TotalMercanciasExentas = fac.TotalMercanciasExentas, TotalMercanciasGravadas = fac.TotalMercanciasGravadas, TotalServExentos = fac.TotalServExentos, TotalServGravados = fac.TotalServGravados, TotalImpuesto = fac.TotalImpuesto, TotalDescuentos = fac.TotalDescuentos, TotalGravado = fac.TotalGravado, TotalExento = fac.TotalExento, TotalVenta = fac.TotalVenta, TotalVentaNeta = fac.TotalVentaNeta, TotalComprobante = fac.TotalComprobante, InformacionReferencia_IdFactura = fac.Id_Factura, InformacionReferencia_Codigo = 1, InformacionReferencia_FechaEmision = FechaEmicionDocumento, InformacionReferencia_Numero = fac.Clave, InformacionReferencia_Razon = txt_MotivoAnulacion.Text }; Contribuyente_Consecutivos Consecutivo; using (db_FacturaDigital db = new db_FacturaDigital()) { List <Factura_Detalle> detalle = new List <Factura_Detalle>(); foreach (Factura_Detalle item in fac.Factura_Detalle) { Factura_Detalle newItem = (Factura_Detalle)item.Clone(); newItem.Factura = null; List <Factura_Detalle_Impuesto> newimpuestos = new List <Factura_Detalle_Impuesto>(); foreach (Factura_Detalle_Impuesto detalleimpuesto in db.Factura_Detalle_Impuesto.AsNoTracking().Where(q => q.Id_Factura_Detalle == newItem.Id_Factura_Detalle)) { Factura_Detalle_Impuesto newDetalleimpuesto = (Factura_Detalle_Impuesto)detalleimpuesto.Clone(); newDetalleimpuesto.Factura_Detalle = null; newimpuestos.Add(newDetalleimpuesto); } if (newimpuestos.Count > 0) { newItem.Factura_Detalle_Impuesto = newimpuestos; } detalle.Add(newItem); } Anulacion.Factura_Detalle = detalle; Consecutivo = db.Contribuyente_Consecutivos.First(q => q.Id_Contribuyente == RecursosSistema.Contribuyente.Id_Contribuyente); Anulacion.NumeroConsecutivo = Consecutivo.Consecutivo_NotasCredito; string ClaveHacienda = new GeneradorDeClavesHacienda(new GeneradorDeClavesHacienda() { ConsecutivoHacienda = new ConsecutivoHacienda(new ConsecutivoHacienda() { TipoDocumento = Tipo_documento.Nota_de_crédito_electrónica, NumeracionConsecutiva = Consecutivo.Consecutivo_NotasCredito, CasaMatriz = casaMatriz, PuntoVenta = PuntoVenta }), FechaEmicion = FechaEmicionDocumento, Identificacion_Contribuyente = Convert.ToInt64(RecursosSistema.Contribuyente.Identificacion_Numero), }).ToString(); Anulacion.Clave = ClaveHacienda; db.Factura.Add(Anulacion); Consecutivo.Consecutivo_NotasCredito++; Factura Original = db.Factura.First(q => q.Id_Factura == fac.Id_Factura); Original.Estado = (int)EstadoComprobante.Anulando; try { FacturaDB_ToNotaCredito Hacienda = new FacturaDB_ToNotaCredito(RecursosSistema.Contribuyente); Hacienda.Convertir(Anulacion, fac.Fecha_Emision_Documento).CrearXml(Tipo_documento.Nota_de_crédito_electrónica).Enviar(); Anulacion.XML_Enviado = Hacienda.XML.InnerXml; // new FacturaPDF.FacturaElectronicaPDF().CrearFactura(fac); db.SaveChanges(); RecursosSistema.WindosNotification("Factura", "La nota de crédito Clave [" + Anulacion.Clave + "] se envío para su valoración"); RecursosSistema.Servicio_AgregarFactura(Anulacion.Clave); this.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } }catch (Exception ex) { this.LogError(ex); MessageBox.Show("Ocurrio un error al anular la factura", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void CrearFactura(string CondicionVenta, string MedioPago) { try { DateTime FechaEmicionDocumento = DateTime.Now; int casaMatriz = 1; int PuntoVenta = 1; Tipo_documento tipoDocumento = Tipo_documento.Factura_electrónica; if (tb_TipoFactura.IsChecked.Value) { tipoDocumento = Tipo_documento.Tiquete_Electrónico; } #region HeaderFactura Factura fac = new Factura() { Codigo_Moneda = "CRC", CondicionVenta = CondicionVenta, Email_Enviado = false, CasaMatriz = casaMatriz, PuntoVenta = PuntoVenta, Emisor_CorreoElectronico = RecursosSistema.Contribuyente.CorreoElectronico, Emisor_Identificacion_Numero = RecursosSistema.Contribuyente.Identificacion_Numero, Emisor_Identificacion_Tipo = RecursosSistema.Contribuyente.Identificacion_Tipo, Emisor_Nombre = RecursosSistema.Contribuyente.Nombre, Emisor_NombreComercial = RecursosSistema.Contribuyente.NombreComercial, Emisor_Telefono_Codigo = RecursosSistema.Contribuyente.Telefono_Codigo, Emisor_Telefono_Numero = RecursosSistema.Contribuyente.Telefono_Numero, Emisor_Ubicacion_Barrio = RecursosSistema.Contribuyente.Barrio, Emisor_Ubicacion_Canton = RecursosSistema.Contribuyente.Canton, Emisor_Ubicacion_Distrito = RecursosSistema.Contribuyente.Distrito, Emisor_Ubicacion_Provincia = RecursosSistema.Contribuyente.Provincia, Emisor_Ubicacion_OtrasSenas = RecursosSistema.Contribuyente.OtrasSenas, Fecha_Emision_Documento = FechaEmicionDocumento, Estado = (int)EstadoComprobante.Enviado, Id_Contribuyente = RecursosSistema.Contribuyente.Id_Contribuyente, Id_TipoDocumento = (int)tipoDocumento, MedioPago = MedioPago, }; #endregion #region Totales if (ResumenProductoExento != 0) { fac.TotalMercanciasExentas = ResumenProductoExento; } if (ResumenProductoGravado != 0) { fac.TotalMercanciasGravadas = ResumenProductoGravado; } if (ResumenServicioExento != 0) { fac.TotalServExentos = ResumenServicioExento; } if (ResumenServicioGravado != 0) { fac.TotalServGravados = ResumenServicioGravado; } if (ResumenImpuesto != 0) { fac.TotalImpuesto = ResumenImpuesto; } if (ResumenDescuentos != 0) { fac.TotalDescuentos = ResumenDescuentos; } fac.TotalGravado = (fac.TotalMercanciasGravadas ?? 0) + (fac.TotalServGravados ?? 0); fac.TotalExento = (fac.TotalMercanciasExentas ?? 0) + (fac.TotalServExentos ?? 0); fac.TotalVenta = (fac.TotalGravado ?? 0) + (fac.TotalExento ?? 0); fac.TotalVentaNeta = fac.TotalVenta - (fac.TotalDescuentos ?? 0); fac.TotalComprobante = fac.TotalVentaNeta + (fac.TotalImpuesto ?? 0); #endregion if (tipoDocumento == Tipo_documento.Factura_electrónica) { Cliente ClienteSeleccionado = cb_Clientes.SelectedItem as Cliente; if (ClienteSeleccionado == null) { loadingDisplayer.Visibility = Visibility.Collapsed; MessageBox.Show("Error al obtener datos del cliente seleccionado", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop); return; } fac.Receptor_CorreoElectronico = ClienteSeleccionado.CorreoElectronico; fac.Receptor_Identificacion_Numero = ClienteSeleccionado.Identificacion_Numero; fac.Receptor_Identificacion_Tipo = ClienteSeleccionado.Identificacion_Tipo; fac.Receptor_Nombre = ClienteSeleccionado.Nombre; fac.Receptor_NombreComercial = ClienteSeleccionado.NombreComercial; fac.Receptor_Telefono_Codigo = ClienteSeleccionado.Telefono_Codigo; fac.Receptor_Telefono_Numero = ClienteSeleccionado.Telefono_Numero; fac.Receptor_Ubicacion_Barrio = ClienteSeleccionado.Barrio; fac.Receptor_Ubicacion_Canton = ClienteSeleccionado.Canton; fac.Receptor_Ubicacion_Distrito = ClienteSeleccionado.Distrito; fac.Receptor_Ubicacion_OtrasSenas = ClienteSeleccionado.OtrasSenas; fac.Receptor_Ubicacion_Provincia = ClienteSeleccionado.Provincia; } if (tipoDocumento == Tipo_documento.Tiquete_Electrónico) { string clienteTiquete; if (string.IsNullOrEmpty(txt_Cliente_Tiquete.Text)) { clienteTiquete = "Cliente Contado"; } else { clienteTiquete = txt_Cliente_Tiquete.Text; } fac.Receptor_Nombre = clienteTiquete; } fac.Factura_Detalle = new List <Factura_Detalle>(FacturaDetalle); Contribuyente_Consecutivos Consecutivo; using (db_FacturaDigital db = new db_FacturaDigital()) { Consecutivo = db.Contribuyente_Consecutivos.First(q => q.Id_Contribuyente == RecursosSistema.Contribuyente.Id_Contribuyente); fac.NumeroConsecutivo = Consecutivo.Consecutivo_Facturas; string ClaveHacienda = new GeneradorDeClavesHacienda(new GeneradorDeClavesHacienda() { ConsecutivoHacienda = new ConsecutivoHacienda(new ConsecutivoHacienda() { TipoDocumento = Tipo_documento.Factura_electrónica, NumeracionConsecutiva = Consecutivo.Consecutivo_Facturas, CasaMatriz = casaMatriz, PuntoVenta = PuntoVenta }), FechaEmicion = FechaEmicionDocumento, Identificacion_Contribuyente = Convert.ToInt64(RecursosSistema.Contribuyente.Identificacion_Numero), }).ToString(); fac.Clave = ClaveHacienda; db.Factura.Add(fac); Consecutivo.Consecutivo_Facturas++; try { if (tipoDocumento == Tipo_documento.Factura_electrónica) { FacturaDB_ToFacturaElectronica Hacienda = new FacturaDB_ToFacturaElectronica(RecursosSistema.Contribuyente); Hacienda.Convertir(fac).CrearXml(tipoDocumento).Enviar(); fac.XML_Enviado = Hacienda.XML.InnerXml; new FacturaPDF.FacturaElectronicaPDF().CrearFactura(fac); } else { } db.SaveChanges(); LimpiarVista(); RecursosSistema.WindosNotification("Factura", "La factura Clave [" + fac.Clave + "] se envío para su valoración"); RecursosSistema.Servicio_AgregarFactura(fac.Clave); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } loadingDisplayer.Visibility = Visibility.Collapsed; } } catch (Exception ex) { loadingDisplayer.Visibility = Visibility.Collapsed; this.LogError(ex); MessageBox.Show("Ocurrio un error al crear la factura en la base de datos", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void Guardar(object sender, RoutedEventArgs e) { try { bool ExisteContribuyente = true; using (db_FacturaDigital db = new db_FacturaDigital()) { DataModel.EF.Contribuyente Contribuyente = db.Contribuyente.FirstOrDefault(); if (Contribuyente == null) { ExisteContribuyente = false; Contribuyente = new DataModel.EF.Contribuyente(); } #region DatosGenerales if (string.IsNullOrEmpty(txt_Identificacion.Text)) { MessageBox.Show("Favor llenar el campo de identificacion", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop); return; } Contribuyente.Identificacion_Numero = txt_Identificacion.Text; Contribuyente.Identificacion_Tipo = (cb_Cedula.SelectedItem as ComboBoxItem).Tag.ToString(); if (string.IsNullOrEmpty(txt_Nombre.Text)) { MessageBox.Show("Favor llenar el campo de nombre", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop); return; } Contribuyente.Nombre = txt_Nombre.Text; if (string.IsNullOrEmpty(txt_comercial.Text)) { MessageBox.Show("Favor llenar el campo de nombre comercial", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop); return; } Contribuyente.NombreComercial = txt_comercial.Text; if (string.IsNullOrEmpty(txt_Correo.Text)) { MessageBox.Show("Favor llenar el campo de correo", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop); return; } Contribuyente.CorreoElectronico = txt_Correo.Text; int Region; if (!int.TryParse(txt_TelefonoRegion.Text, out Region)) { MessageBox.Show("Favor llenar el campo de region (solamente numeros) ", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop); return; } Contribuyente.Telefono_Codigo = Region; int telefono; if (!int.TryParse(txt_TelefonoNumero.Text, out telefono)) { MessageBox.Show("Favor llenar el campo de telefono (solamente numeros)", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop); return; } Contribuyente.Telefono_Numero = telefono; #endregion #region Ubicacion UbicacionesType provincia = cb_Provincia.SelectedItem as UbicacionesType; if (provincia == null) { MessageBox.Show("Favor seleccionar una provincia", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop); return; } Contribuyente.Provincia = provincia.Id; UbicacionesType canton = cb_canton.SelectedItem as UbicacionesType; if (canton == null) { MessageBox.Show("Favor seleccionar una Canton", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop); return; } Contribuyente.Canton = canton.Id; UbicacionesType Distrito = cb_distrito.SelectedItem as UbicacionesType; if (Distrito == null) { MessageBox.Show("Favor seleccionar una Distrito", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop); return; } Contribuyente.Distrito = Distrito.Id; UbicacionesType Barrio = cb_barrio.SelectedItem as UbicacionesType; if (Distrito == null) { MessageBox.Show("Favor seleccionar una Barrio", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop); return; } Contribuyente.Barrio = Barrio.Id; if (string.IsNullOrEmpty(txt_otrasSenas.Text)) { MessageBox.Show("Favor llenar el campo de otras senas", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop); return; } Contribuyente.OtrasSenas = txt_otrasSenas.Text; #endregion #region Datos Hacienda if (string.IsNullOrEmpty(txt_UsuarioHacienda.Text)) { MessageBox.Show("Favor llenar el campo de usuario de hacienda ", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop); return; } Contribuyente.UsuarioHacienda = txt_UsuarioHacienda.Text; if (string.IsNullOrEmpty(txt_contrasena.Text)) { MessageBox.Show("Favor llenar el campo de contrasena de hacienda", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop); return; } Contribuyente.ContrasenaHacienda = txt_contrasena.Text; #endregion #region Certificado if (string.IsNullOrEmpty(CertificadoUrl) && (Contribuyente.Certificado == null || Contribuyente.Certificado.Length == 0)) { MessageBox.Show("Favor seleccionar un certificado", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop); return; } if (!string.IsNullOrEmpty(CertificadoUrl)) { Contribuyente.Certificado = File.ReadAllBytes(CertificadoUrl); } if (string.IsNullOrEmpty(txt_contrasenaCertificado.Text)) { MessageBox.Show("Favor llenar el campo de contrasena de certificado", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop); return; } Contribuyente.Contrasena_Certificado = txt_contrasenaCertificado.Text; try { X509Certificate2 x509 = new X509Certificate2(Contribuyente.Certificado, Contribuyente.Contrasena_Certificado, X509KeyStorageFlags.Exportable); x509.Verify(); string Values = x509.Subject.Split(',').First(q => q.ToUpper().Contains("SERIALNUMBER")); string SerialNumber = Values.Split('=')[1]; string OnlyNumber = new string(SerialNumber.Where(c => char.IsDigit(c)).ToArray()); if (!OnlyNumber.Contains(Contribuyente.Identificacion_Numero) && !Contribuyente.Identificacion_Numero.Contains(OnlyNumber)) { if (MessageBox.Show("El numero de identificacion registrado en el certificado no concuerda con el ingresado por el usuario en el campo [Identificacion]. Aun asi desea continuar?", "Validacion", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.No) { return; } } } catch (Exception ex) { this.LogError(ex); MessageBox.Show("No se pudo abrir el ceritificado. Esto se puede deber a que la contrasena indicada no es valida o que el contenido del certificado es invalido", "Error", MessageBoxButton.OK, MessageBoxImage.Stop); return; } #endregion if (!ExisteContribuyente) { db.Contribuyente.Add(Contribuyente); } db.SaveChanges(); Contribuyente_Consecutivos conse = db.Contribuyente_Consecutivos.FirstOrDefault(q => q.Id_Contribuyente == Contribuyente.Id_Contribuyente); if (conse == null) { conse = new Contribuyente_Consecutivos() { Consecutivo_Facturas = 1, Consecutivo_NotasCredito = 1, Consecutivo_Tiquete_Electrónico = 1, Consecutivo_Confirmacion = 1, Id_Contribuyente = Contribuyente.Id_Contribuyente }; db.Contribuyente_Consecutivos.Add(conse); db.SaveChanges(); } MessageBox.Show("Perfil actualizado correctamente", "Informacion", MessageBoxButton.OK, MessageBoxImage.Information); try { RecursosSistema.Contribuyente = Contribuyente; RecursosSistema.OnStartMain_Load(); }catch (Exception ex) { this.LogError(ex); MessageBox.Show("Reinicie el sistema para continuar"); } } } catch (Exception ex) { this.LogError(ex); MessageBox.Show("Ocurrio al guardar el perfil del contribuyente", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void Save(object sender, RoutedEventArgs e) { try { #region Validaciones if (string.IsNullOrEmpty(txt_host.Text)) { MessageBox.Show("Error debe de ingresar la dirección del servidor", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop); return; } if (string.IsNullOrEmpty(txt_Usuario.Text)) { MessageBox.Show("Error debe de ingresar el usuario", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop); return; } if (string.IsNullOrEmpty(txt_contrasena.Password)) { MessageBox.Show("Error debe de ingresar la contraseña", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop); return; } if (string.IsNullOrEmpty(txt_Puerto.Text)) { MessageBox.Show("Error debe de ingresar el numero de puerto", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop); return; } int puerto; if (!int.TryParse(txt_Puerto.Text, out puerto) || puerto <= 0) { MessageBox.Show("El puerto debe de ser un dato numerico mayor a 0", "Validacion", MessageBoxButton.OK, MessageBoxImage.Stop); return; } #endregion using (db_FacturaDigital db = new db_FacturaDigital()) { SMTP value = db.SMTP.FirstOrDefault(); if (value == null) { value = new SMTP(); db.SMTP.Add(value); } value.Contrasena = txt_contrasena.Password; value.Puerto = puerto; value.Url_Servidor = txt_host.Text; value.Usuario = txt_Usuario.Text; value.SSL = chk_SSL.IsChecked.Value; value.Detalle_Email = txt_EmailDetails.Text; value.Id_Contribuyente = RecursosSistema.Contribuyente.Id_Contribuyente; db.SaveChanges(); } }catch (Exception ex) { this.LogError(ex); MessageBox.Show("Error al guardar los datos del SMPT", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } MessageBox.Show("Datos de smtp almacenados correctamente", "Informacion", MessageBoxButton.OK, MessageBoxImage.Information); try { RecursosSistema.OnStartMain_Load(); } catch (Exception ex) { this.LogError(ex); MessageBox.Show("Reinicie el sistema para continuar"); } }
void Loop() { string LastConsultaToken = null; while (true) { try { ConsultaRequest Consulta = FacturasPendientes.Take(); try { Thread.Sleep(new TimeSpan(0, 0, 10)); ///En teoria lo que esto deberia de hacer es que si una misma factura ///Se llama seguida espere un segundo. De esa forma si tengo una cola de 5 facturas ///y de esas 5 una esta pegana no me detenga a las otras y a la vez para no afectar el rendimiento ///ya que seria peligroso que se encicle por una factura que esta en error o que hacienda dure mucho ///de todas formas al final hay un sleep de 10 seg por cualquier cosa ///la otra cosa que hace es darle tiempo a hacienda a que termine de procesar ///entonces con esto podemos decir que si tenemos 5 facturas las procese rapido por eje ///si de las 5 hay 2 con error las 3 buenas se van a procesar rapido ///las ultimas 2 van a quedar en el ciclo esperando un minuto teniendo en cuenta que esto es una cola y no una pila if (Consulta.Token == LastConsultaToken) { Thread.Sleep(new TimeSpan(0, 1, 0)); } else { LastConsultaToken = Consulta.Token; } RespuestaHaciendaModel Respuesta; lock (ContribuyenteCopiaLocal) { Respuesta = new HaciendaComunication(ContribuyenteCopiaLocal).Consultar(Consulta.Clave); } if (Respuesta != null) { /* * recibido,procesando,aceptado,rechazado */ if (Respuesta.ind_estado.ToLower().Equals("aceptado") || Respuesta.ind_estado.ToLower().Equals("rechazado")) { Respuesta.respuesta_xml = Encoding.UTF8.GetString(Convert.FromBase64String(Respuesta.respuesta_xml)); using (db_FacturaDigital db = new db_FacturaDigital()) { Factura fac = db.Factura.First(q => q.Clave == Consulta.Clave); fac.XML_Respuesta = Respuesta.respuesta_xml; int EstadoHacienda; string DetalleRespuestaHacienda; ObtenerEstadoRespuestaHacienda(Respuesta.respuesta_xml, out EstadoHacienda, out DetalleRespuestaHacienda); fac.Estado = EstadoHacienda; fac.HaciendaDetalle = DetalleRespuestaHacienda; if ((Tipo_documento)fac.Id_TipoDocumento == Tipo_documento.Nota_de_crédito_electrónica) { Factura facoriginal = db.Factura.FirstOrDefault(); if (EstadoHacienda == 1) { facoriginal.Estado = (int)EstadoComprobante.Anulando; } else if (EstadoHacienda == 3) { facoriginal.Estado = (int)EstadoComprobante.ErrorAnulando; } } db.SaveChanges(); if (fac.Estado == 1 || fac.Estado == 2) { RecursosSistema.WindosNotification("Facturación", "Factura aceptada [" + Consulta.Clave + "]"); new SendSmtp.SendSmtp(fac.Id_Factura).Enviar(); fac.Email_Enviado = true; } else if (fac.Estado == 3) { RecursosSistema.WindosNotification("Facturación", "Factura rechazada [" + Consulta.Clave + "]"); } } } else if (Respuesta.ind_estado.ToLower().Contains("error")) { using (db_FacturaDigital db = new db_FacturaDigital()) { Factura fac = db.Factura.First(q => q.Clave == Consulta.Clave); fac.Estado = (int)EstadoComprobante.ErrorInternoHacienda; db.SaveChanges(); } } else { if (Consulta.FechaEmicion.AddHours(3) < DateTime.Now) { using (db_FacturaDigital db = new db_FacturaDigital()) { Factura fac = db.Factura.First(q => q.Clave == Consulta.Clave); fac.Estado = (int)EstadoComprobante.SinRespuestaDeHacienda; db.SaveChanges(); } } else { FacturasPendientes.Add(Consulta);//vuelva a ponerla en lista de espera si es la unica en teoria debera esprar un minuto 10 seg } } } }catch (Exception ex) { Consulta.Errores++; if (Consulta.Errores >= 3) { RecursosSistema.WindosNotification("Error", "Error al procesar la respuesta de la factura [" + Consulta.Clave + "]"); } else { this.LogError(ex); FacturasPendientes.Add(Consulta);//vuelva a ponerla en lista de espera si es la unica en teoria debera esprar un minuto 10 seg } } }catch (Exception ex) { this.LogError(ex); } } }