protected void btnGuardar_Click(object sender, EventArgs e) { try { foreach (GridViewRow row in gridViewDocumentos.Rows) { var chkBox = row.FindControl("ckActivo") as CheckBox; IDataItemContainer container = (IDataItemContainer)chkBox.NamingContainer; if (chkBox.Checked) { PersistRowIndex(container.DataItemIndex); } else { RemoveRowIndex(container.DataItemIndex); } } List <Documento> documentos = DocumentosAsociados(); if (documentos == null || documentos.Count == 0 || documentos.Count > 1) { string script = @"<script type='text/javascript'> alert('" + "Debe seleccionar un Documento" + "');</script>"; ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", script, false); } else { Documento documento = documentos.ElementAt(0); var client = new CfeServiceClient(); client.ClientCredentials.UserName.UserName = "******"; client.ClientCredentials.UserName.Password = "******"; ReqBody solicitud = new ReqBody(); solicitud.CodComercio = "UWTEST01"; solicitud.CodTerminal = "UWCAJA01"; solicitud.HMAC = ""; RequerimientoParaUcfe req = new RequerimientoParaUcfe(); req.TipoMensaje = 320; // req.Uuid = "10000222"; req.TipoCfe = documento.TipoDocumento; req.Serie = documento.Serie; req.NumeroCfe = documento.NroSerie.ToString(); req.IdReq = "1"; req.FechaReq = documento.Fecha.Year + "" + documento.Fecha.Month + "" + documento.Fecha.Day; req.HoraReq = documento.Fecha.Hour + "" + documento.Fecha.Minute + "" + documento.Fecha.Second; req.CodComercio = "UWTEST01"; req.CodTerminal = "UWCAJA01"; solicitud.Req = req; solicitud.RequestDate = documento.Fecha.Year + "-" + documento.Fecha.Month + "-" + documento.Fecha.Day + "T" + documento.Fecha.Hour + ":" + documento.Fecha.Minute + ":" + documento.Fecha.Second; solicitud.Tout = 120000; RespBody respuesta = null; if (client.InnerChannel.State != System.ServiceModel.CommunicationState.Faulted) { respuesta = client.Invoke(solicitud); } if (respuesta != null) { if (respuesta.ErrorCode != 0) { string script = @"<script type='text/javascript'> alert('" + "Error: " + respuesta.ErrorMessage + "');</script>"; ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", script, false); } else { if (respuesta.Resp.CodRta.Equals("00")) { Sistema.GetInstancia().AnularDocumento(documento.IdDocumento); string script = @"<script type='text/javascript'> alert('" + "El documento se anulo correctamente" + "');</script>"; ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", script, false); limpiarSeleccion(); } else { string script = @"<script type='text/javascript'> alert('" + respuesta.Resp.MensajeRta + "');</script>"; ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", script, false); } } } else { string script = @"<script type='text/javascript'> alert('" + "Error de conexión con el punto de emisión" + "');</script>"; ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", script, false); } } } catch { } }
protected void btnGuardar_Click(object sender, EventArgs e) { if (IsValid) { try { Documento doc = new Documento(); doc.Activo = true; bool error = false; Cliente cli = Sistema.GetInstancia().BuscarClienteId(Int32.Parse(ddlCliente.SelectedValue)); doc.cliente = cli; doc.Fecha = DateTime.Parse(txtFecha.Text); doc.IdCliente = cli.IdCliente; doc.TipoDocumento = "182"; doc.retenciones = ObtenerRetenciones(); if (!error) { XmlDocument xml = null; xml = GenerarXmlResguardo(doc); String xmlTexto = ""; xmlTexto = AjustarCFE(xml.InnerXml, "eResg"); try { var client = new CfeServiceClient(); var client2 = new ConsultaCfeClient(); client.ClientCredentials.UserName.UserName = "******"; client.ClientCredentials.UserName.Password = Sistema.GetInstancia().Contrasena(Session["rut"].ToString()); client2.ClientCredentials.UserName.UserName = "******"; client2.ClientCredentials.UserName.Password = Sistema.GetInstancia().Contrasena(Session["rut"].ToString()); ReqBody solicitud = new ReqBody(); solicitud.CodComercio = Sistema.GetInstancia().CodComercio(Session["idLocal"].ToString()); solicitud.CodTerminal = Sistema.GetInstancia().CodTerminal(Session["idLocal"].ToString()); solicitud.HMAC = ""; RequerimientoParaUcfe req = new RequerimientoParaUcfe(); req.TipoMensaje = 310; // req.Uuid = "10000222"; req.Uuid = "RES" + Sistema.GetInstancia().ObtenerProximoCodigo(Session["rut"].ToString()).ToString(); req.TipoCfe = doc.TipoDocumento; req.IdReq = "1"; req.FechaReq = doc.Fecha.Year + "" + doc.Fecha.Month + "" + doc.Fecha.Day; req.HoraReq = doc.Fecha.Hour + "" + doc.Fecha.Minute + "" + doc.Fecha.Second; req.CodComercio = Sistema.GetInstancia().CodComercio(Session["idLocal"].ToString()); req.CodTerminal = Sistema.GetInstancia().CodTerminal(Session["idLocal"].ToString()); if (!String.IsNullOrEmpty(txtAdenda.Text)) { req.Adenda = txtAdenda.Text; } req.CfeXmlOTexto = xmlTexto; if (doc.retenciones != null) { String adenda = ""; foreach (RetencionPercepcionResguardos retencion in doc.retenciones) { adenda += retencion.CodigoPercepcionRetencion.NroForm.ToString() + retencion.CodigoPercepcionRetencion.NroLinea.ToString() + " : " + retencion.CodigoPercepcionRetencion.Descripcion; adenda += System.Environment.NewLine; } req.Adenda = adenda; } solicitud.Req = req; solicitud.RequestDate = doc.Fecha.Year + "-" + doc.Fecha.Month + "-" + doc.Fecha.Day + "T" + doc.Fecha.Hour + ":" + doc.Fecha.Minute + ":" + doc.Fecha.Second; solicitud.Tout = 120000; RespBody respuesta = null; if (client.InnerChannel.State != System.ServiceModel.CommunicationState.Faulted) { respuesta = client.Invoke(solicitud); } if (respuesta != null) { if (respuesta.ErrorCode != 0) { string script = @"<script type='text/javascript'> alert('" + "Error: " + respuesta.ErrorMessage + "');</script>"; ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", script, false); } else { if (respuesta.Resp.CodRta.Equals("00")) { doc.NroSerie = Int32.Parse(respuesta.Resp.NumeroCfe); doc.Serie = respuesta.Resp.Serie; doc.xmlFirmado = respuesta.Resp.XmlCfeFirmado; doc.EstadoDGI = "Procesado"; String msg = Sistema.GetInstancia().GuardarDocumento(doc); try { byte[] pdf = client2.ObtenerPdf("120185850015", Int32.Parse(respuesta.Resp.TipoCfe), respuesta.Resp.Serie, doc.NroSerie); // Sistema.GetInstancia().PDFActual = pdf; Session["pdf"] = pdf; //if (Sistema.GetInstancia().PDFActual != null) //{ // Response.Redirect("VisorPDF.aspx"); //} if (Session["pdf"] != null) { Response.Redirect("VisorPDF.aspx"); } } catch (Exception ex) { } } else if (respuesta.Resp.CodRta.Equals("11")) { doc.NroSerie = Int32.Parse(respuesta.Resp.NumeroCfe); doc.Serie = respuesta.Resp.Serie; doc.xmlFirmado = respuesta.Resp.XmlCfeFirmado; doc.EstadoDGI = "Aceptado"; String msg = Sistema.GetInstancia().GuardarDocumento(doc); try { byte[] pdf = client2.ObtenerPdf("120185850015", Int32.Parse(respuesta.Resp.TipoCfe), respuesta.Resp.Serie, doc.NroSerie); //Sistema.GetInstancia().PDFActual = pdf; Session["pdf"] = pdf; //if (Sistema.GetInstancia().PDFActual != null) //{ // Response.Redirect("VisorPDF.aspx"); //} if (Session["pdf"] != null) { Response.Redirect("VisorPDF.aspx"); } } catch (Exception ex) { } } else if (respuesta.Resp.CodRta.Equals("01")) { string script = @"<script type='text/javascript'> alert('" + "Denegado: " + respuesta.Resp.MensajeRta + "');</script>"; ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", script, false); } else if (respuesta.Resp.CodRta.Equals("03")) { string script = @"<script type='text/javascript'> alert('" + "Comercio invalido" + "');</script>"; ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", script, false); } else if (respuesta.Resp.CodRta.Equals("05")) { string script = @"<script type='text/javascript'> alert('" + "CFE Rechazado por DGI" + "');</script>"; ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", script, false); } else if (respuesta.Resp.CodRta.Equals("12")) { string script = @"<script type='text/javascript'> alert('" + "Requerimiento invalido" + "');</script>"; ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", script, false); } else if (respuesta.Resp.CodRta.Equals("30")) { string script = @"<script type='text/javascript'> alert('" + "Error en formato: " + respuesta.Resp.MensajeRta + "');</script>"; ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", script, false); } else if (respuesta.Resp.CodRta.Equals("31")) { string script = @"<script type='text/javascript'> alert('" + "Error en formato CFE" + respuesta.Resp.MensajeRta + "');</script>"; ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", script, false); } else if (respuesta.Resp.CodRta.Equals("89")) { string script = @"<script type='text/javascript'> alert('" + "Terminal invalida" + "');</script>"; ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", script, false); } else if (respuesta.Resp.CodRta.Equals("96")) { string script = @"<script type='text/javascript'> alert('" + "Error en sistema" + "');</script>"; ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", script, false); } else if (respuesta.Resp.CodRta.Equals("99")) { string script = @"<script type='text/javascript'> alert('" + "Sesion no iniciada" + "');</script>"; ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", script, false); } else { string script = @"<script type='text/javascript'> alert('" + "Denegado: " + respuesta.Resp.MensajeRta + "');</script>"; ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", script, false); } } } else { string script = @"<script type='text/javascript'> alert('" + "Error de conexión con el punto de emisión" + "');</script>"; ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", script, false); } } catch (Exception ex) { } //Guardar serie y nro } } catch (Exception ex) { string script = @"<script type='text/javascript'> alert('" + "Error al guardar" + "');</script>"; ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", script, false); } } }
protected void btnGuardar_Click(object sender, EventArgs e) { try { List <Documento> documentos = Sistema.GetInstancia().ObtenerDocumentosAceptados(Session["rut"].ToString()); if (documentos != null) { var client = new CfeServiceClient(); client.ClientCredentials.UserName.UserName = "******"; client.ClientCredentials.UserName.Password = "******"; foreach (Documento documento in documentos) { ReqBody solicitud = new ReqBody(); solicitud.CodComercio = "UWTEST01"; solicitud.CodTerminal = "UWCAJA01"; solicitud.HMAC = ""; RequerimientoParaUcfe req = new RequerimientoParaUcfe(); req.TipoMensaje = 360; // req.Uuid = "10000222"; req.TipoCfe = documento.TipoDocumento; req.Serie = documento.Serie; req.NumeroCfe = documento.NroSerie.ToString(); req.IdReq = "1"; req.FechaReq = documento.Fecha.Year + "" + documento.Fecha.Month + "" + documento.Fecha.Day; req.HoraReq = documento.Fecha.Hour + "" + documento.Fecha.Minute + "" + documento.Fecha.Second; req.CodComercio = "UWTEST01"; req.CodTerminal = "UWCAJA01"; solicitud.Req = req; solicitud.RequestDate = documento.Fecha.Year + "-" + documento.Fecha.Month + "-" + documento.Fecha.Day + "T" + documento.Fecha.Hour + ":" + documento.Fecha.Minute + ":" + documento.Fecha.Second; solicitud.Tout = 120000; RespBody respuesta = null; if (client.InnerChannel.State != System.ServiceModel.CommunicationState.Faulted) { respuesta = client.Invoke(solicitud); } if (respuesta != null) { if (respuesta.ErrorCode != 0) { string script = @"<script type='text/javascript'> alert('" + "Error: " + respuesta.ErrorMessage + "');</script>"; ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", script, false); } else { if (respuesta.Resp.CodRta.Equals("00")) { Sistema.GetInstancia().ModificarEstado(documento.IdDocumento, "Procesado", null); } else if (respuesta.Resp.CodRta.Equals("11")) { } else if (respuesta.Resp.CodRta.Equals("01") || respuesta.Resp.CodRta.Equals("05")) { Sistema.GetInstancia().ModificarEstado(documento.IdDocumento, "Anulado", respuesta.Resp.MensajeRta); } else if (respuesta.Resp.CodRta.Equals("03")) { string script = @"<script type='text/javascript'> alert('" + "Comercio invalido" + "');</script>"; ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", script, false); } else if (respuesta.Resp.CodRta.Equals("30")) { string script = @"<script type='text/javascript'> alert('" + "Error en formato" + "');</script>"; ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", script, false); } else if (respuesta.Resp.CodRta.Equals("31")) { string script = @"<script type='text/javascript'> alert('" + "Error en formato CFE" + "');</script>"; ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", script, false); } else if (respuesta.Resp.CodRta.Equals("89")) { string script = @"<script type='text/javascript'> alert('" + "Terminal invalida" + "');</script>"; ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", script, false); } else if (respuesta.Resp.CodRta.Equals("96")) { string script = @"<script type='text/javascript'> alert('" + "Error en sistema" + "');</script>"; ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", script, false); } else { string script = @"<script type='text/javascript'> alert('" + "Denegado: " + respuesta.Resp.MensajeRta + "');</script>"; ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", script, false); } } } else { string script = @"<script type='text/javascript'> alert('" + "Error de conexión con el punto de emisión" + "');</script>"; ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", script, false); } } } string script2 = @"<script type='text/javascript'> alert('" + "Los estados se actualizaron correctamente" + "');</script>"; ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", script2, false); } catch { string script3 = @"<script type='text/javascript'> alert('" + "Error al actualizar los estados" + "');</script>"; ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", script3, false); } }