/*Metodo para ingresar informacion a la tabla Ventas y Ventas Totales, por medio de la instancia a un metodo * dentro de la clase Caja*/ private void btn_Pagar_Click(object sender, EventArgs e) { #region Validaciones if (dgv_ListadeProductos.RowCount == 0) { MessageBox.Show("Se tienen que agregar productos para la venta", "", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (txt_ClientePago.Text == string.Empty) { MessageBox.Show("Precio de Cliente no puede ser nulo", "", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (lbl_IdCliente.Text == string.Empty) { MessageBox.Show("Cliente no Agregado", "", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (Convert.ToInt32(txt_ClientePago.Text) < Convert.ToInt32(lbl_DineroaPagar.Text)) { MessageBox.Show("Cliente esta pagando de Menos", "", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { #endregion objCreaXML.MediodePago = cbo_MediodePago.SelectedValue.ToString(); objCreaXML.CondificionVenta = cbo_CondiciondeVenta.SelectedValue.ToString(); objCreaXML.Ubicacion = lbl_Ubicacion.Text; objCreaXML.Clientes = Convert.ToInt32(lbl_IdCliente.Text); objCreaXML.SELECT_DATOS_XML(); objCreaXML.SELECT_DATOS_XML_RECEPTOR(); objCreaXML.SELECT_DATOS_XML_TRAIDOS(); objCreaXML.SELECT_FECHA(); objCreaXML.SELECT_SUCURSAL(); objCreaXML.SELECT_SECUENCIA(); objCreaXML.CREAR_COD_SEGURIDAD(); CREAR_XML(); objCreaXML.FechaFull = Convert.ToDateTime(objCreaXML.Año + "-" + objCreaXML.Mes + "-" + objCreaXML.Dia + " " + objCreaXML.Fecha); objCreaXML.DineroTotal = lbl_DineroaPagar.Text; objCreaXML.Login = lbl_Usuario.Text; objCreaXML.INSERT_VENTATOTALES(); if (objCreaXML.Validacion == "Insertado") { int valor = 0; valor = dgv_Consecutivos.Rows.Count; for (int i = 0; i < valor; i++) { objCreaXML.Descripcion = dgv_Consecutivos.Rows[i].Cells[1].Value.ToString(); objCreaXML.Descuento = dgv_Consecutivos.Rows[i].Cells[5].Value.ToString(); objCreaXML.Cantidad = dgv_Consecutivos.Rows[i].Cells[2].Value.ToString(); objCreaXML.Precio = dgv_Consecutivos.Rows[i].Cells[3].Value.ToString(); objCreaXML.Total = dgv_Consecutivos.Rows[i].Cells[7].Value.ToString(); objCreaXML.Cod_Articulo = dgv_Consecutivos.Rows[i].Cells[8].Value.ToString(); objCreaXML.Id_VentaTotal = objCreaXML.Secuencia; objCreaXML.INSERT_VENTAS(); } try { objCreaXML.qweasd = Convert.ToInt32(objCreaXML.Id_VentaTotal); } catch (Exception ex) { MessageBox.Show(ex.Message + "CARGA_FACTURA()"); } objCreaXML.SecuenciaUpdate = Convert.ToString(Convert.ToInt32(objCreaXML.Secuencia) + 1); objCreaXML.UPDATE_SECUENCIA(); valor = 0; if (objCreaXML.Validacion == "Actualizado") { #region Firmador string Clave = "fe-" + "506" + objCreaXML.Diafin + objCreaXML.Mesfin + objCreaXML.Añofin + objCreaXML.Cedulafin + objCreaXML.Consecutivofin + "00001" + "01" + objCreaXML.Secuenciafin + "1" + objCreaXML.CodSeguridad; string Clave2 = "506" + objCreaXML.Diafin + objCreaXML.Mesfin + objCreaXML.Añofin + objCreaXML.Cedulafin + objCreaXML.Consecutivofin + "00001" + "01" + objCreaXML.Secuenciafin + "1" + objCreaXML.CodSeguridad; this.FIRMADOR(Clave, objCreaXML.Certificado); #endregion #region Enviar Hacienda XmlDocument xmlElectronica = new XmlDocument(); //xmlElectronica.Load("D:\\Documents\\Facturas\\" + Clave + "firmado.xml"); xmlElectronica.Load("G:\\Mi Unidad\\" + lbl_Ubicacion.Text + "\\" + Clave + "firmado.xml"); Emisor myEmisor = new Emisor(); myEmisor.numeroIdentificacion = objCreaXML.Cedula; myEmisor.TipoIdentificacion = objCreaXML.TipoIdentificacionfin; Receptor myReceptor = new Receptor(); if ((objCreaXML.Tipo_IdentificacionRE.Trim().Length > 0)) { myReceptor.sinReceptor = false; myReceptor.numeroIdentificacion = objCreaXML.CedulaRE; myReceptor.TipoIdentificacion = objCreaXML.TipoIdentificacionfinRE; } else { myReceptor.sinReceptor = true; } Recepcion myRecepcion = new Recepcion(); myRecepcion.emisor = myEmisor; myRecepcion.receptor = myReceptor; myRecepcion.clave = Clave2; myRecepcion.fecha = objCreaXML.Año + "-" + objCreaXML.Mesfin + "-" + objCreaXML.Diafin + "T" + objCreaXML.Fecha; myRecepcion.comprobanteXml = Funciones.EncodeStrToBase64(xmlElectronica.OuterXml); xmlElectronica = null; string Token = ""; Token = getToken(); Comunicacion enviaFactura = new Comunicacion(); enviaFactura.EnvioDatos(Token, myRecepcion); string jsonEnvio = ""; jsonEnvio = enviaFactura.jsonEnvio; string jsonRespuesta = ""; jsonRespuesta = enviaFactura.jsonRespuesta; System.IO.StreamWriter outputFile = new System.IO.StreamWriter("G:\\Mi Unidad\\" + lbl_Ubicacion.Text + "\\" + Clave + "_03_jsonEnvio.txt"); outputFile.Write(jsonEnvio); outputFile.Close(); outputFile = new System.IO.StreamWriter("G:\\Mi Unidad\\" + lbl_Ubicacion.Text + "\\" + Clave + "_04_jsonRespuesta.txt"); outputFile.Write(jsonRespuesta); outputFile.Close(); if (!(enviaFactura.xmlRespuesta == null)) { enviaFactura.xmlRespuesta.Save("G:\\Mi Unidad\\" + lbl_Ubicacion.Text + "\\" + Clave + "_05_RESP.xml"); } else { outputFile = new System.IO.StreamWriter("G:\\Mi Unidad\\" + lbl_Ubicacion.Text + "\\" + Clave + "_05_RESP_SinRespuesta.txt"); outputFile.Write(""); outputFile.Close(); } #endregion this.Limpieza(); } } CARGA_COD_FACTURA(); } }
public async Task ProcesaAsync(string xmlFactura, string directorioSalida, string nombreArchivo) { //Hay que definir la ruta de salida donde se van a guardar los XML. //if (!this.txtFolderSalida.Text.EndsWith("\\")) //{ // this.txtFolderSalida.Text += "\\"; //} //string directorio = this.txtFolderSalida.Text; //string nombreArchivo = this.txtConsecutivo.Text; XmlDocument xmlDocSF = new XmlDocument(); xmlDocSF.LoadXml(xmlFactura); xmlDocSF.Save((directorioSalida + (nombreArchivo + "_01_SF.xml"))); // se guarda el xml sin firmar. XmlTextWriter xmlTextWriter = new XmlTextWriter((directorioSalida + (nombreArchivo + "_01_SF.xml")), new System.Text.UTF8Encoding(false)); xmlDocSF.WriteTo(xmlTextWriter); xmlTextWriter.Close(); xmlDocSF = null; // se firma el XML FirmaServiceReference.FirmaServiceClient firmaClient = new FirmaServiceReference.FirmaServiceClient(); string xmlFirmado = await firmaClient.FirmarXMLAsync(directorioSalida + nombreArchivo, Emisor.RutaArchivoCertificado, Emisor.PinCertificado); //se carga el XML firmado XmlDocument xmlElectronica = new XmlDocument(); //xmlElectronica.Load((directorioSalida + (nombreArchivo + "_02_Firmado.xml"))); xmlElectronica.LoadXml(xmlFirmado); //se instancian las clases Json que se utilizan para enviar la factura FacturaElectronica.Emisor myEmisor = new FacturaElectronica.Emisor(); myEmisor.numeroIdentificacion = Emisor.Identificacion_Numero; myEmisor.TipoIdentificacion = Emisor.Identificacion_Tipo; FacturaElectronica.Receptor myReceptor = new FacturaElectronica.Receptor(); if (!string.IsNullOrEmpty(Receptor.Identificacion_Numero)) { myReceptor.sinReceptor = false; myReceptor.numeroIdentificacion = Receptor.Identificacion_Numero; myReceptor.TipoIdentificacion = Receptor.Identificacion_Tipo; } else { myReceptor.sinReceptor = true; } Recepcion myRecepcion = new Recepcion(); myRecepcion.emisor = myEmisor; myRecepcion.receptor = myReceptor; myRecepcion.clave = NumeroClave; myRecepcion.fecha = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz"); myRecepcion.comprobanteXml = Funciones.EncodeStrToBase64(xmlElectronica.OuterXml); xmlElectronica = null; //se obtiene el "Token" que se utiliza para establecer comunicacion con el API de Hacienda string Token = string.Empty; Token = getToken(Emisor.UsuarioApi, Emisor.ClaveApi); //se envía la factura a Hacienda. Comunicacion enviaFactura = new Comunicacion(); enviaFactura.EnvioDatos(Token, myRecepcion); string jsonEnvio = ""; jsonEnvio = enviaFactura.jsonEnvio; string jsonRespuesta = ""; jsonRespuesta = enviaFactura.jsonRespuesta; //txtJSONEnvio.Text = jsonEnvio; //txtJSONRespuesta.Text = jsonRespuesta; System.IO.StreamWriter outputFile = new System.IO.StreamWriter((directorioSalida + (nombreArchivo + "_03_jsonEnvio.txt"))); outputFile.Write(jsonEnvio); outputFile.Close(); outputFile = new System.IO.StreamWriter((directorioSalida + (nombreArchivo + "_04_jsonRespuesta.txt"))); outputFile.Write(jsonRespuesta); outputFile.Close(); if (!(enviaFactura.xmlRespuesta == null)) { //this.txtRespuestaHacienda.Text = enviaFactura.xmlRespuesta.OuterXml; enviaFactura.xmlRespuesta.Save((directorioSalida + (nombreArchivo + "_05_RESP.xml"))); } else { outputFile = new System.IO.StreamWriter((directorioSalida + (nombreArchivo + "_05_RESP_SinRespuesta.txt"))); outputFile.Write(enviaFactura.mensajeRespuesta); outputFile.Close(); //this.txtRespuestaHacienda.Text = "Consulte en unos minutos, factura se est� procesando."; //this.txtRespuestaHacienda.Text = (this.txtRespuestaHacienda.Text + ("\r\n" + ("\r\n" + "Consulte por Clave"))); //this.txtRespuestaHacienda.Text = (this.txtRespuestaHacienda.Text + ("\r\n" + ("\r\n" + enviaFactura.mensajeRespuesta))); } //MessageBox.Show(enviaFactura.mensajeRespuesta); }