Imports bdGrupoSanchoToro.db Imports System.Data.Entity Imports DevExpress.Xpf.Core.ServerMode Imports DevExpress.Xpf.Core Imports DevExpress.XtraReports.UserDesigner Imports DevExpress.XtraReports.UI Imports System.IO Imports DevExpress.Xpf.Grid Imports DevExpress.Xpf.RichEdit Imports DevExpress.Office.Services Imports System.ComponentModel Imports tsWPFCore Imports DevExpress.Xpf.Docking Imports DevExpress.Xpf.Reports.UserDesigner Imports DevExpress.Xpf.Reports.UserDesigner.Native Imports System Imports System.Collections.Generic Imports System.Data Imports System.Linq Imports System.Text Imports System.Windows Imports System.Collections Imports bdGrupoSanchoToro.db.Utilidades Imports bdGrupoSanchoToro.dbcontext Public Class ucPlantillas Private bd As bdGrupoSanchoToro.tscGrupoSanchoToro Private PlantillaActual As plantillas Private WithEvents bplas As BindingList(Of plantillas) ' Private mdiController As XRDesignMdiController 'Public Overrides ReadOnly Property CampoIndice As String ' Get ' Return "" ' End Get 'End Property Public Overrides ReadOnly Property idRegistroAplicacionActual As String Get Return "Plantillas" End Get End Property Public Overrides ReadOnly Property DescripcionRegistro As String Get Return "Plantillas" End Get End Property Public Overrides ReadOnly Property NombreTablaBase As String Get Return "plantillas" End Get End Property Private Sub tvPlantillas_InitNewRow(sender As Object, e As DevExpress.Xpf.Grid.InitNewRowEventArgs) ' e.Source = New plantillas GC.SetCellValue(e.RowHandle, "Oculto", False) gc.SetCellValue(e.RowHandle, "TipoListado", False) gc.SetCellValue(e.RowHandle, "idEmpresa", EmpresaActual.idEmpresa) 'e.Handled = True End Sub Private Sub btEditar_Click(sender As Object, e As RoutedEventArgs) Dim pl As plantillas If GC.View.FocusedRowHandle = DataControlBase.NewItemRowHandle Then pl = GC.GetRow(DataControlBase.NewItemRowHandle) GC.View.CommitEditing(True) Else GC.View.CommitEditing(True) pl = Me.gc.GetFocusedRow End If If pl.idGrupo <> 0 Then EditarPlantilla(pl) Else DXMessageBox.Show("La columna Grupo de plantilla es obligatoria", "Atención", MessageBoxButton.OK, MessageBoxImage.Exclamation) End If End Sub Private Sub EditarPlantilla(pl As bdGrupoSanchoToro.db.plantillas) Try If pl.idPlantilla > 0 Then bd = tscGrupoSanchoToro.NuevoContexto pl = bd.plantillas.First(Function(X) X.idPlantilla = pl.idPlantilla) End If PlantillaActual = pl Select Case PlantillaActual.idGrupoNavigation.Codigo 'Case "GRUPLA.CERTIFICADOS" ' Dim tsEditor As tsWPFCore.dxwEditor ' If pl.idFichero.HasValue AndAlso pl.idFicheroNavigation.Fichero IsNot Nothing Then ' ' tsEditor = New tsWPFCore.dxwEditor(New IO.MemoryStream(pl.idFicheroNavigation.Fichero), DevExpress.XtraRichEdit.DocumentFormat.OpenXml, DevExpress.XtraRichEdit.DocumentFormat.OpenXml, AddressOf Guardar, AddressOf CargarDatosMailMergeCertificados) ' Else ' ' tsEditor = New tsWPFCore.dxwEditor(Nothing, DevExpress.XtraRichEdit.DocumentFormat.OpenXml, DevExpress.XtraRichEdit.DocumentFormat.OpenXml, AddressOf Guardar, AddressOf CargarDatosMailMergeCertificados) ' End If ' tsEditor.ShowDialog() Case "GRUPLA.DOCX" Dim tsEditor As tsWPFCore.dxwEditor If pl.idFichero.HasValue AndAlso pl.idFicheroNavigation.Fichero IsNot Nothing Then tsEditor = New tsWPFCore.dxwEditor(New IO.MemoryStream(pl.idFicheroNavigation.Fichero), DevExpress.XtraRichEdit.DocumentFormat.OpenXml, DevExpress.XtraRichEdit.DocumentFormat.OpenXml, AddressOf GuardarPlantilla) Else tsEditor = New tsWPFCore.dxwEditor(Nothing, DevExpress.XtraRichEdit.DocumentFormat.OpenXml, DevExpress.XtraRichEdit.DocumentFormat.OpenXml, AddressOf GuardarPlantilla) End If tsEditor.ShowDialog() Case "GRUPLA.ODT" Dim tsEditor As tsWPFCore.dxwEditor If pl.idFichero.HasValue AndAlso pl.idFicheroNavigation.Fichero IsNot Nothing Then tsEditor = New tsWPFCore.dxwEditor(New IO.MemoryStream(pl.idFicheroNavigation.Fichero), DevExpress.XtraRichEdit.DocumentFormat.OpenDocument, DevExpress.XtraRichEdit.DocumentFormat.OpenDocument, AddressOf GuardarPlantilla) Else tsEditor = New tsWPFCore.dxwEditor(Nothing, DevExpress.XtraRichEdit.DocumentFormat.OpenDocument, DevExpress.XtraRichEdit.DocumentFormat.OpenDocument, AddressOf GuardarPlantilla) End If tsEditor.ShowDialog() Case Else ' Dim fr = New InformesCOAS.xrFactura Dim xr As New XtraReport Try If pl.idFichero.HasValue = False OrElse pl.idFicheroNavigation.Fichero Is Nothing Then xr = New XtraReport Else xr.LoadLayoutFromXml(New MemoryStream(pl.idFicheroNavigation.Fichero)) End If Me.Guardar(Nothing, Nothing, True, False) Dim vi As New tsWPFCore.ucDiseñadorInformes(xr, pl.idPlantilla, AddressOf GuardarWPF) Dim GrupoDocumentos = Me.GrupoDocumentos Dim dc = tsWPFCore.Comun.dm.DockController Dim docpanel As DevExpress.Xpf.Docking.DocumentPanel Dim d = (From p In GrupoDocumentos.Items Where p.Tag = "Plantilla." & pl.idPlantilla.ToString) If d.Count > 0 Then docpanel = d.First Else docpanel = New DevExpress.Xpf.Docking.DocumentPanel docpanel.Caption = pl.Descripcion docpanel.Content = vi docpanel.ClosingBehavior = DevExpress.Xpf.Docking.ClosingBehavior.ImmediatelyRemove docpanel.Tag = "Plantilla." & pl.idPlantilla.ToString GrupoDocumentos.Items.Add(docpanel) End If dc.Activate(docpanel) Catch ex As Exception DXMessageBox.Show(ex.Message, "Error ucPlantillas.EditarPlantilla") 'Comun.GeneraRegistroCorreoExcepcion(bd, ex, "ucPlantillas.EditarPlantilla") Finally 'xr.Dispose() 'xr = Nothing 'System.GC.Collect() 'System.GC.WaitForFullGCComplete() End Try End Select Catch ex As Exception DXMessageBox.Show(ex.Message, "Error") End Try End Sub Private Sub GuardarPlantilla(ms As MemoryStream) Me.Guardar(Nothing, Nothing, True, False) If PlantillaActual.idFichero.HasValue = False Then Dim idTipoPlantilla = bd.enumeraciones.Where(Function(x) x.Codigo = "TIPFIC.PLANTILLA").First.idEnumeracion PlantillaActual.idFicheroNavigation = New ficheros With {.idTipo = idTipoPlantilla} End If Dim sextension As String Select Case PlantillaActual.idGrupoNavigation.Codigo Case "GRUPLA.ODT" sextension = ".odt" Case "GRUPLA.XHTML" sextension = ".xhtml" 'Case "GRUPLA.NOMINASWORD" ' sextension = ".odt" Case "GRUPLA.DOCX" sextension = ".docx" Case Else sextension = ".xml" End Select PlantillaActual.idFicheroNavigation.Fichero = ms.ToArray PlantillaActual.idFicheroNavigation.Fecha = Now PlantillaActual.idFicheroNavigation.Descripcion = PlantillaActual.Descripcion PlantillaActual.idFicheroNavigation.NombreFichero = PlantillaActual.idPlantilla.ToString & sextension bd.GuardarCambios() End Sub Private Sub CopiarPlantilla(sender As Object, e As RoutedEventArgs) Try Dim ra As bdGrupoSanchoToro.db.plantillas = Me.gc.CurrentItem Dim np As New plantillas Dim i As Integer = 1 Do np.Descripcion = "Copia " & i.ToString & " " & ra.Descripcion i += 1 Loop Until Not bd.plantillas.ToList.Any(Function(x) x.Descripcion = np.Descripcion) If ra.Codigo IsNot Nothing Then i = 1 Do np.Codigo = ra.Codigo & "-" & i.ToString i += 1 Loop Until Not bd.plantillas.ToList.Any(Function(x) x.Codigo = np.Codigo) End If np.idGrupo = ra.idGrupo np.Observaciones = ra.Observaciones np.Oculta = ra.Oculta np.TipoListado = ra.TipoListado bd.plantillas.Add(np) bd.GuardarCambios() Dim idTipoPlantilla = bd.enumeraciones.Where(Function(x) x.Codigo = "TIPFIC.PLANTILLA").First.idEnumeracion np.idFicheroNavigation = New ficheros With {.idTipo = idTipoPlantilla} Dim sextension As String Select Case ra.idGrupoNavigation.Codigo Case "GRUPLA.XHTML" sextension = ".xhtml" Case "GRUPLA.ODT" sextension = ".odt" Case "GRUPLA.DOCX" sextension = ".docx" Case Else sextension = ".xml" End Select np.idFicheroNavigation.Fichero = ra.idFicheroNavigation.Fichero np.idFicheroNavigation.Fecha = Now np.idFicheroNavigation.Descripcion = np.Descripcion np.idFicheroNavigation.NombreFichero = np.idPlantilla.ToString & sextension bd.GuardarCambios() EstableceDCPrincipal() Catch ex As Exception DXMessageBox.Show(ex.Message, "Error") End Try End Sub Private Sub bplas_ListChanged(sender As Object, e As ListChangedEventArgs) Handles bplas.ListChanged If e.ListChangedType = ListChangedType.ItemAdded Then bd.plantillas.Add(DirectCast(bplas.Item(e.NewIndex), plantillas)) End If End Sub Private Sub gc_DespuesEliminar(gc As tsWPFCore.tsGridControl) Handles gc.DespuesEliminar bplas.Remove(gc.CurrentItem) Me.bd.GuardarCambios() End Sub Private Sub tvPlantillas_ValidateRow(sender As Object, e As GridRowValidationEventArgs) Handles tvPlantillas.ValidateRow Try ErroresValidacion.EliminaError("DatosDuplicados") Dim pl As plantillas If tvPlantillas.FocusedRowHandle = DataControlBase.NewItemRowHandle Then pl = gc.GetRow(DataControlBase.NewItemRowHandle) Else pl = gc.CurrentItem End If If pl.idPlantilla = 0 Then If bplas.Where(Function(x) x.Descripcion = pl.Descripcion).Count > 1 OrElse bd.plantillas.Any(Function(x) x.Descripcion = pl.Descripcion) Then e.IsValid = False ' Debug.WriteLine("bplas:" & bplas.Any(Function(x) x.Descripcion = pl.Descripcion).ToString) ' Debug.WriteLine("bd:" & bd.plantillas.Any(Function(x) x.Descripcion = pl.Descripcion).ToString) End If If pl.Codigo IsNot Nothing AndAlso pl.Codigo <> "" Then If bplas.Where(Function(x) x.Codigo = pl.Codigo).Count > 1 OrElse bd.plantillas.Any(Function(x) x.Codigo = pl.Codigo) Then e.IsValid = False End If End If Else If bd.plantillas.Any(Function(x) x.idPlantilla <> pl.idPlantilla AndAlso (x.Descripcion = pl.Descripcion)) Then e.IsValid = False End If If pl.Codigo IsNot Nothing AndAlso pl.Codigo <> "" Then If bplas.Where(Function(x) x.Codigo = pl.Codigo).Count > 1 OrElse bd.plantillas.Any(Function(x) x.idPlantilla <> pl.idPlantilla AndAlso x.Codigo = pl.Codigo) Then e.IsValid = False End If End If End If If Not e.IsValid Then e.SetError("Nombre de Plantilla o Codigo ya utilizado") DXMessageBox.Show("Nombre de Plantilla o Codigo ya utilizado", "Error") End If bd.GuardarCambios() Catch EX As Exception e.SetError(EX.Message) e.IsValid = False DXMessageBox.Show(EX.Message, "Error") End Try End Sub Public Shared Sub GuardarWPF(b() As Byte, idPlantilla As Integer) Dim bd = tscGrupoSanchoToro.NuevoContexto() Dim plantilla = bd.plantillas.First(Function(X) X.idPlantilla = idPlantilla) bd.GuardarCambios() If plantilla.idFichero.HasValue = False Then Dim idTipoPlantilla = bd.enumeraciones.Where(Function(x) x.Codigo = "TIPFIC.PLANTILLA").First.idEnumeracion plantilla.idFicheroNavigation = New ficheros With {.idTipo = idTipoPlantilla} End If Dim sextension As String Select Case plantilla.idGrupoNavigation.Codigo Case "GRUPLA.XHTML" sextension = ".xhtml" Case "GRUPLA.DOCX" sextension = ".docx" Case Else sextension = ".xml" End Select plantilla.idFicheroNavigation.Fichero = b plantilla.idFicheroNavigation.Fecha = Now plantilla.idFicheroNavigation.Descripcion = plantilla.Descripcion plantilla.idFicheroNavigation.NombreFichero = plantilla.idPlantilla.ToString & sextension bd.GuardarCambios() End Sub Public Overrides Function EstableceDCPrincipal(Optional Background As Boolean = False, Optional FuerzaNuevo As Boolean = False, Optional Refrescar As Boolean = False) As tsUtilidades.EstadosAplicacion Try Dim gptmp = (From e In bd.enumeraciones Select e Where e.idGrupoEnumeracionNavigation.Grupo = "GRUPLA").ToList Dim gp As New List(Of bdGrupoSanchoToro.db.enumeraciones) For Each g In gptmp If Comun.ObtienePermisos(bd, g.Codigo, idUsuario).Impresion Then gp.Add(g) End If Next Me.cbGrupo.ItemsSource = gp Me.cbPermiso.ItemsSource = bd.permisos.ToList Dim plas = bd.plantillas.ToList.Where(Function(p) gp.Any(Function(x) x.Codigo = p.idGrupoNavigation.Codigo)).ToList Me.gc.DataContext = bd bplas = New BindingList(Of plantillas)(plas) Me.gc.ItemsSource = bplas Return tsUtilidades.EstadosAplicacion.SinDatos Catch ex As Exception Throw New Exception(ex.Message, ex) End Try End Function Public Overrides Sub EstableceDataContextSecundarios(Optional Background As Boolean = False) End Sub Public Overrides Sub EstableceTitulo() Me.docpanel.Caption = "Plantillas" Me.docpanel.Tag = "Plantillas" End Sub Public Overrides Sub Cargado() Me.ContenedorAplicacion.btNuevo.IsVisible = False Me.ContenedorAplicacion.siEstado.IsVisible = False If bd.usuarios.First(Function(x) x.idUsuario = bdGrupoSanchoToro.db.Utilidades.idUsuario).idGrupoNavigation.Descripcion.ToLower = "administradores" Then Me.gcPermiso.Visible = True Me.gcCodigo.Visible = True Me.gcEditar.VisibleIndex = Me.gc.Columns.Count End If Dim mi As New MenuItem() mi.Header = "Copiar Plantilla" AddHandler mi.Click, AddressOf CopiarPlantilla Me.gc.ContextMenu.Items.Add(mi) End Sub Public Overrides Function ObtieneBD() As tsUtilidades.ItsContexto bd = tscGrupoSanchoToro.NuevoContexto() Return bd End Function Public Overrides Function ObtienePermisos() As tsUtilidades.Permisos Return Comun.ObtienePermisos(Me.bd, "AP.SUPERADMINISTRADORES", idUsuario) End Function End Class