private Dictionary <string, CamposImpresion> obtenerCamposImpresion(out Dictionary <string, CamposImpresion> porCodigo) { Dictionary <string, CamposImpresion> camposImpresion = new Dictionary <string, CamposImpresion>(); porCodigo = new Dictionary <string, CamposImpresion>(); try { MySqlConnection cone = new MySqlConnection(ConfigurationManager.ConnectionStrings["ce"].ConnectionString); MySqlCommand cmd = new MySqlCommand("select * from camposImpresion inner join grupoimpresion ", cone); cone.Open(); MySqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { CamposImpresion c = new CamposImpresion(); c.Campo = dr["cim_campo"].ToString().ToUpper(); c.Codigo = dr["cim_codigo"].ToString().ToUpper(); c.Formato = dr["cim_formato"].ToString(); int copias = 1; int.TryParse(dr["gim_copias"].ToString(), out copias); c.Copias = copias; c.Impresora = dr["gim_impresora"].ToString(); c.Tipo = dr["cim_tipo"].ToString().ToUpper(); c.Alineación = dr["cim_alineacion"].ToString().ToUpper(); camposImpresion.Add(c.Campo, c); porCodigo.Add(c.Codigo, c); } cone.Close(); } catch (Exception ex) { porCodigo = null; camposImpresion = null; LogManager.Mensaje m = new LogManager.Mensaje("SGECA.ColaDeImpresion", "obtenerCamposImpresion(out Dictionary)", 0, "Error al intentar obtener los campos de impresión.", ex.Message, "", true, LogManager.EMensaje.Critico, ex.StackTrace); Notify(m); } return(camposImpresion); }
private string formatearDatos(CamposImpresion c) { string valor = ""; try { switch (c.Datos.Tipo.ToUpper()) { case "INT": int t; if (int.TryParse(c.Datos.Valor.ToString(), out t)) { if (c.Formato.Contains("-")) { string[] h = c.Formato.Split('-'); int inicio = 0; for (int j = 0; j < h.Length; j++) { if (valor.Length > 0) { valor += "-"; } valor += c.Datos.Valor.ToString().Substring(inicio, h[j].Length); inicio += h[j].Length; } } else { valor = t.ToString(); if (c.Formato.Length > 0) { valor = valor.Trim().PadLeft(c.Formato.Length, '0').Substring(0, c.Formato.Length); } } } break; case "BIGINT": long l; if (long.TryParse(c.Datos.Valor.ToString(), out l)) { if (c.Formato.Contains("-")) { string[] h = c.Formato.Split('-'); int inicio = 0; for (int j = 0; j < h.Length; j++) { if (valor.Length > 0) { valor += "-"; } valor += c.Datos.Valor.ToString().Substring(inicio, h[j].Length); inicio += h[j].Length; } } else { valor = l.ToString(); if (c.Formato.Length > 0) { valor = valor.Trim().PadLeft(c.Formato.Length, '0').Substring(0, c.Formato.Length); } } } break; case "DATETIME": DateTime dt; if (DateTime.TryParse(c.Datos.Valor.ToString(), out dt)) { valor = dt.ToString(c.Formato); } break; case "DATE": DateTime dat; if (DateTime.TryParse(c.Datos.Valor.ToString(), out dat)) { valor = dat.ToString(c.Formato); } break; case "DECIMAL": decimal dc; if (Decimal.TryParse(c.Datos.Valor.ToString(), out dc)) { valor = dc.ToString(c.Formato); } break; case "VARCHAR": valor = c.Datos.Valor.ToString(); if (c.Formato.Length > 0) { if (c.Formato.Contains("#")) { valor = valor.Trim().PadLeft(c.Formato.Length, '0').Substring(0, c.Formato.Length); } else if (c.Formato.Length > 0) { valor = valor.Trim().PadLeft(c.Formato.Length, '0').Substring(0, c.Formato.Length); } } break; case "BLOB": valor = c.Datos.Valor.ToString(); if (c.Formato.Length > 0) { if (c.Formato == "*") { if (c.Datos.Valor is byte[]) { valor = System.Text.Encoding.Default.GetString((byte[])c.Datos.Valor); } } } break; default: valor = c.Datos.Valor.ToString(); break; } } catch (Exception Exception) { LogManager.Mensaje m = new LogManager.Mensaje("SGECA.ColaDeImpresion.builders.Documento", "formatearDatos", 0, "Error al intentar dar formato a los datos.", Exception.Message, "", true, LogManager.EMensaje.Critico, Exception.StackTrace); Notify(m); valor = null; } return(valor); }
public string prepararTexto(string idDocumento, string appPath) { if (!idDocumento.StartsWith(classId + "_")) { return(""); } int id = int.Parse(idDocumento.Replace(classId + "_", "")); try { Dictionary <string, CamposImpresion> camposImpresionPorCodigo; Dictionary <string, CamposImpresion> camposImpresionPorCampo = obtenerCamposImpresion(out camposImpresionPorCodigo); if (camposImpresionPorCodigo == null || camposImpresionPorCampo == null) { LogManager.Mensaje m = new LogManager.Mensaje("SGECA.ColaDeImpresion", "prepararTexto(int)", 0, "No puedo imprimir un documento si no hay campos definidos. Id: " + id, "", "", true, LogManager.EMensaje.Advertencia, ""); Notify(m); return(""); } Dictionary <string, string> valEnc = obtenerValoresEncabezado(camposImpresionPorCampo, id); if (valEnc == null) { LogManager.Mensaje m = new LogManager.Mensaje("SGECA.ColaDeImpresion", "prepararTexto(int)", 0, "No puedo imprimir un documento si no hay valores de encabezado. Id: " + id, "", "", true, LogManager.EMensaje.Advertencia, ""); Notify(m); return(""); } List <Dictionary <string, string> > valItems = obtenerValoresItems(camposImpresionPorCampo, id); if (valItems == null) { LogManager.Mensaje m = new LogManager.Mensaje("SGECA.ColaDeImpresion", "prepararTexto(int)", 0, "No puedo imprimir un documento si no hay valores de items. Id: " + id, "", "", true, LogManager.EMensaje.Advertencia, ""); Notify(m); return(""); } RichTextBoxPrintCtrl richTextBoxPrintCtrl = new RichTextBoxPrintCtrl(); richTextBoxPrintCtrl.LoadFile(appPath + @"\Templates\1.rtf"); string texto = richTextBoxPrintCtrl.Rtf; int position = 0, posicionAnterior = 0; while (texto.IndexOf('#') > 0) { position = texto.IndexOf('#'); int indiceCambioRegistroItem = texto.Substring(posicionAnterior, position - posicionAnterior).IndexOf('~'); if (indiceCambioRegistroItem > 0) { StringBuilder sb = new StringBuilder(texto); sb[posicionAnterior + indiceCambioRegistroItem] = ' '; texto = sb.ToString(); if (valItems.Count > 0) { valItems.RemoveAt(0); } } if (position < 0) { break; } int nPos = position + 2; while (true) { char caracter = texto.Substring(nPos, 1)[0]; if (caracter == '-' || caracter == '(' || caracter == ')' || ((int)caracter > 47 && (int)caracter < 58)) { nPos++; } else { break; } } string variable = texto.Substring(position + 1, nPos - position - 1).Trim(); string textoReemplazo = ""; if (!valEnc.TryGetValue(variable.Replace("-", "").Replace("(", "").Replace(")", "").ToUpper(), out textoReemplazo)) { if (valItems.Count > 0) { valItems[0].TryGetValue(variable.Replace("-", "").Replace("(", "").Replace(")", "").ToUpper(), out textoReemplazo); } } if (textoReemplazo == null) { textoReemplazo = ""; } CamposImpresion myValue = camposImpresionPorCodigo[variable.Replace("-", "").Replace("(", "").Replace(")", "").ToUpper()]; switch (myValue.Alineación) { case "D": textoReemplazo = textoReemplazo.PadLeft(nPos - position, ' '); break; case "I": textoReemplazo = textoReemplazo.PadRight(nPos - position, ' '); break; case "C": textoReemplazo = centrarCadena(textoReemplazo, nPos - position); break; default: textoReemplazo = textoReemplazo.PadLeft(nPos - position, ' '); break; } texto = texto.Substring(0, position) + textoReemplazo + texto.Substring(nPos, texto.Length - nPos); posicionAnterior = position + textoReemplazo.Length; } return(texto); } catch (Exception ex) { LogManager.Mensaje m = new LogManager.Mensaje("SGECA.ColaDeImpresion", "prepararTexto(int)", 0, "Error al intentar preparar el documento a imprimir. Id: " + id, ex.Message, "", true, LogManager.EMensaje.Critico, ex.StackTrace); Notify(m); return(""); } }