public static string FacturasDeUnRecibo(Lfx.Data.IConnection dataBase, int ReciboId) { System.Text.StringBuilder Facturas = new System.Text.StringBuilder(); System.Data.DataTable TablaFacturas = dataBase.Select("SELECT id_comprob FROM recibos_comprob WHERE id_recibo=" + ReciboId.ToString()); foreach (System.Data.DataRow Factura in TablaFacturas.Rows) { if (Facturas.Length == 0) { Facturas.Append(Lbl.Comprobantes.Comprobante.TipoYNumeroCompleto(dataBase, Lfx.Data.Connection.ConvertDBNullToZero(Factura["id_comprob"]))); } else { Facturas.Append(", " + Lbl.Comprobantes.Comprobante.TipoYNumeroCompleto(dataBase, Lfx.Data.Connection.ConvertDBNullToZero(Factura["id_comprob"]))); } } return(Facturas.ToString()); }
public void Restore(string backupName) { string Carpeta = backupName + System.IO.Path.DirectorySeparatorChar; Lfx.Environment.Folders.EnsurePathExists(this.BackupPath); if (Carpeta != null && Carpeta.Length > 0 && System.IO.Directory.Exists(this.BackupPath + Carpeta)) { bool UsandoArchivoComprimido = false; Lfx.Types.OperationProgress Progreso = new Lfx.Types.OperationProgress("Restaurando copia de seguridad", "Este proceso va a demorar varios minutos. Por favor no lo interrumpa"); Progreso.Modal = true; /* Progreso.ChangeStatus("Descomprimiendo"); * // Descomprimir backup si está comprimido * if (System.IO.File.Exists(BackupPath + Carpeta + "backup.7z")) { * Lfx.FileFormats.Compression.Archive ArchivoComprimido = new Lfx.FileFormats.Compression.Archive(BackupPath + Carpeta + "backup.7z"); * ArchivoComprimido.ExtractAll(BackupPath + Carpeta); * UsandoArchivoComprimido = true; * } */ Progreso.ChangeStatus("Eliminando datos actuales"); using (Lfx.Data.IConnection ConnRestaurar = Lfx.Workspace.Master.GetNewConnection("Restauración de copia de seguridad") as Lfx.Data.IConnection) { Progreso.ChangeStatus("Acomodando estructuras"); Lfx.Workspace.Master.Structure.TagList.Clear(); Lfx.Workspace.Master.Structure.LoadFromFile(this.BackupPath + Carpeta + "dbstruct.xml"); //Lfx.Workspace.Master.CheckAndUpdateDatabaseVersion(true, true); using (BackupReader Lector = new BackupReader(this.BackupPath + Carpeta + "dbdata.lbd")) using (IDbTransaction Trans = ConnRestaurar.BeginTransaction()) { ConnRestaurar.EnableConstraints(false); Progreso.ChangeStatus("Incorporando tablas de datos"); Progreso.Max = (int)(Lector.Length / 1024); string TablaActual = null; string[] ListaCampos = null; object[] ValoresCampos = null; int CampoActual = 0; bool EndTable = false; qGen.BuilkInsert Insertador = new qGen.BuilkInsert(); do { string Comando = Lector.ReadString(4); switch (Comando) { case ":TBL": TablaActual = Lector.ReadPrefixedString4(); string NombreTabla; if (Lfx.Workspace.Master.Structure.Tables.ContainsKey(TablaActual) && Lfx.Workspace.Master.Structure.Tables[TablaActual].Label != null) { NombreTabla = Lfx.Workspace.Master.Structure.Tables[TablaActual].Label; } else { NombreTabla = TablaActual.ToTitleCase(); } EndTable = false; Progreso.ChangeStatus("Cargando " + NombreTabla); qGen.Delete DelCmd = new qGen.Delete(TablaActual); DelCmd.EnableDeleleteWithoutWhere = true; ConnRestaurar.ExecuteNonQuery(DelCmd); break; case ":FDL": ListaCampos = Lector.ReadPrefixedString4().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); ValoresCampos = new object[ListaCampos.Length]; CampoActual = 0; break; case ":FLD": ValoresCampos[CampoActual++] = Lector.ReadField(); break; case ".ROW": qGen.Insert Insertar = new qGen.Insert(TablaActual); for (int i = 0; i < ListaCampos.Length; i++) { Insertar.ColumnValues.AddWithValue(ListaCampos[i], ValoresCampos[i]); } Insertador.Add(Insertar); ValoresCampos = new object[ListaCampos.Length]; CampoActual = 0; break; case ":REM": Lector.ReadPrefixedString4(); break; case ".TBL": EndTable = true; break; } if (EndTable || Insertador.Count >= 1000) { if (Insertador.Count > 0) { ConnRestaurar.ExecuteNonQuery(Insertador); } Insertador.Clear(); Progreso.Value = (int)(Lector.Position / 1024); } } while (Lector.Position < Lector.Length); Lector.Close(); if (Lfx.Workspace.Master.MasterConnection.SqlMode == qGen.SqlModes.PostgreSql) { // PostgreSql: Tengo que actualizar las secuencias Progreso.ChangeStatus("Actualizando secuencias"); string PatronSecuencia = @"nextval\(\'(.+)\'(.*)\)"; foreach (string Tabla in Lfx.Data.DatabaseCache.DefaultCache.GetTableNames()) { string OID = ConnRestaurar.FieldString("SELECT c.oid FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace WHERE pg_catalog.pg_table_is_visible(c.oid) AND c.relname ~ '^" + Tabla + "$'"); System.Data.DataTable Campos = ConnRestaurar.Select("SELECT a.attname,pg_catalog.format_type(a.atttypid, a.atttypmod),(SELECT substring(d.adsrc for 128) FROM pg_catalog.pg_attrdef d WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef), a.attnotnull, a.attnum FROM pg_catalog.pg_attribute a WHERE a.attrelid = '" + OID + "' AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum"); foreach (System.Data.DataRow Campo in Campos.Rows) { if (Campo[2] != DBNull.Value && Campo[2] != null) { string DefaultCampo = System.Convert.ToString(Campo[2]); if (Regex.IsMatch(DefaultCampo, PatronSecuencia)) { string NombreCampo = System.Convert.ToString(Campo[0]); foreach (System.Text.RegularExpressions.Match Ocurrencia in Regex.Matches(DefaultCampo, PatronSecuencia)) { string Secuencia = Ocurrencia.Groups[1].ToString(); int MaxId = ConnRestaurar.FieldInt("SELECT MAX(" + NombreCampo + ") FROM " + Tabla) + 1; ConnRestaurar.ExecuteNonQuery("ALTER SEQUENCE " + Secuencia + " RESTART WITH " + MaxId.ToString()); } } } } } } if (System.IO.File.Exists(this.BackupPath + Carpeta + "blobs.lst")) { // Incorporar Blobs Progreso.ChangeStatus("Incorporando imágenes"); System.IO.StreamReader LectorBlobs = new System.IO.StreamReader(this.BackupPath + Carpeta + "blobs.lst", System.Text.Encoding.Default); string InfoImagen = null; do { InfoImagen = LectorBlobs.ReadLine(); if (InfoImagen != null && InfoImagen.Length > 0) { string Tabla = Lfx.Types.Strings.GetNextToken(ref InfoImagen, ","); string Campo = Lfx.Types.Strings.GetNextToken(ref InfoImagen, ","); string CampoId = Lfx.Types.Strings.GetNextToken(ref InfoImagen, ","); string NombreArchivoImagen = Lfx.Types.Strings.GetNextToken(ref InfoImagen, ","); // Guardar blob nuevo qGen.Update ActualizarBlob = new qGen.Update(Tabla); ActualizarBlob.WhereClause = new qGen.Where(Campo, CampoId); System.IO.FileStream ArchivoImagen = new System.IO.FileStream(this.BackupPath + Carpeta + NombreArchivoImagen, System.IO.FileMode.Open, System.IO.FileAccess.Read); byte[] Contenido = new byte[System.Convert.ToInt32(ArchivoImagen.Length) - 1 + 1]; ArchivoImagen.Read(Contenido, 0, System.Convert.ToInt32(ArchivoImagen.Length)); ArchivoImagen.Close(); ActualizarBlob.ColumnValues.AddWithValue(Campo, Contenido); ConnRestaurar.ExecuteNonQuery(ActualizarBlob); } }while (InfoImagen != null); LectorBlobs.Close(); } if (UsandoArchivoComprimido) { Progreso.ChangeStatus("Eliminando archivos temporales"); // Borrar los archivos que descomprim temporalmente System.IO.DirectoryInfo Dir = new System.IO.DirectoryInfo(this.BackupPath + Carpeta); foreach (System.IO.FileInfo DirItem in Dir.GetFiles()) { if (DirItem.Name != "backup.7z" && DirItem.Name != "info.txt") { System.IO.File.Delete(this.BackupPath + Carpeta + DirItem.Name); } } } Progreso.ChangeStatus("Terminando transacción"); Trans.Commit(); } Progreso.End(); } Lfx.Workspace.Master.RunTime.Toast("La copia de seguridad se restauró con éxito. A continuación se va a reiniciar la aplicación.", "Copia Restaurada"); } }
public Lazaro.Pres.Spreadsheet.Sheet ToWorkbookSheet() { Lazaro.Pres.Spreadsheet.Sheet Res = new Lazaro.Pres.Spreadsheet.Sheet(Titulo); foreach (Lfx.Data.Aggregate Agru in this.Aggregates) { Agru.Reset(); } foreach (Lazaro.Pres.Field Field in this.Fields) { Res.ColumnHeaders.Add(new ColumnHeader(Field.Label, Field.Width, Field.Alignment)); } if (this.Grouping != null) { this.Grouping.Reset(); } qGen.Select Sel = this.SelectCommand.Clone(); if (this.Grouping != null) { if (Sel.Order == null || Sel.Order.Length == 0) { Sel.Order = this.Grouping.FieldName; } else { Sel.Order = this.Grouping.FieldName + "," + Sel.Order; } } System.Data.DataTable Tabla = Connection.Select(Sel); foreach (System.Data.DataRow Registro in Tabla.Rows) { if (this.Grouping != null && Lfx.Types.Object.CompareByValue(this.Grouping.LastValue, Registro[Lazaro.Orm.Data.ColumnValue.GetNameOnly(this.Grouping.FieldName)]) != 0) { // Agrego un renglón de subtotales if (this.Grouping.LastValue != null) { Lazaro.Pres.Spreadsheet.Row SubTotales; if (this.ExpandGroups) { SubTotales = new Lazaro.Pres.Spreadsheet.AggregationRow(Res); } else { SubTotales = new Lazaro.Pres.Spreadsheet.Row(Res); } for (int i = 0; i < this.Fields.Count; i++) { Lazaro.Pres.Spreadsheet.Cell FuncCell = null; if (this.Grouping != null && this.Fields[i].Name == this.Grouping.FieldName && this.ExpandGroups == false) { FuncCell = new Cell(this.Grouping.LastValue); } else { foreach (Lfx.Data.Aggregate SubtAgru in this.Aggregates) { if (SubtAgru.FieldName == this.Fields[i].Name) { switch (SubtAgru.Function) { case Lfx.Data.AggregationFunctions.Count: FuncCell = new Cell(SubtAgru.Count); SubtAgru.ResetCounters(); break; case Lfx.Data.AggregationFunctions.Sum: FuncCell = new Cell(SubtAgru.Sum); SubtAgru.ResetCounters(); break; default: FuncCell = new Cell("#undef#"); SubtAgru.ResetCounters(); break; } } } } if (FuncCell != null) { SubTotales.Cells.Add(FuncCell); } else { SubTotales.Cells.Add(new Cell("")); } } Res.Rows.Add(SubTotales); } this.Grouping.LastValue = Registro[Lazaro.Orm.Data.ColumnValue.GetNameOnly(this.Grouping.FieldName)]; // Agrego un encabezado if (ExpandGroups) { Res.Rows.Add(new Lazaro.Pres.Spreadsheet.HeaderRow(Registro[Lazaro.Orm.Data.ColumnValue.GetNameOnly(this.Grouping.FieldName)].ToString())); } } if (Aggregates != null) { // Calculo las funciones de agregación foreach (Lfx.Data.Aggregate Agru in this.Aggregates) { switch (Agru.Function) { case Lfx.Data.AggregationFunctions.Count: Agru.Count++; break; case Lfx.Data.AggregationFunctions.Sum: Agru.Sum += System.Convert.ToDecimal(Registro[Lazaro.Orm.Data.ColumnValue.GetNameOnly(Agru.FieldName)]); break; } } } if (ExpandGroups) { Lazaro.Pres.Spreadsheet.Row Renglon = new Lazaro.Pres.Spreadsheet.Row(); foreach (Lazaro.Pres.Field Field in this.Fields) { Lazaro.Pres.Spreadsheet.Cell Celda = new Cell(Registro[Lazaro.Orm.Data.ColumnValue.GetNameOnly(Field.Name)]); Renglon.Cells.Add(Celda); } Res.Rows.Add(Renglon); } } return(Res); }