Option Strict Off Imports DevExpress.Xpf.Editors Imports DevExpress.Xpf.LayoutControl Imports tsWPFCore Imports System.ComponentModel Imports System.Data Imports DevExpress.Xpf.Editors.Validation Imports DevExpress.Xpf.Editors.Helpers Imports DevExpress.Xpf.Core Imports tsWPF.modExtensiones Imports System.Data.Common Imports System.Data.Entity Imports DevExpress.Xpf.Grid Imports tsUtilidades.Extensiones Imports DevExpress.Xpf.Bars Imports tsUtilidades.Datos Imports tsWPF.Comun Imports bdGrupoSanchoToro.db Public Class ucAsientoModelo Private bd As tscGrupoSanchoToro Friend _idAsiento As Nullable(Of Integer) Friend _idAsientoACopiar As Integer? Public Sub New(Optional idAsiento As Integer? = Nothing, Optional idAsientoACopiar As Integer? = Nothing) ' Llamada necesaria para el diseñador. InitializeComponent() _idAsiento = idAsiento _idAsientoACopiar = idAsientoACopiar ' Agregue cualquier inicialización después de la llamada a InitializeComponent(). End Sub Public Overrides ReadOnly Property idRegistroAplicacionActual As String Get If Estado = tsUtilidades.EstadosAplicacion.Nuevo Then Return "AsientosModelo.Nuevo" Else Return "AsientosModelo." & DirectCast(Me.DataContext, asientosmodelos).Codigo.ToString End If End Get End Property Public Overrides ReadOnly Property DescripcionRegistro As String Get Return "AsientosModelo" End Get End Property Public Overrides ReadOnly Property NombreTablaBase As String Get Return "asientosmodelos" End Get End Property Public Overrides Sub EstableceDataContextSecundarios(Optional Background As Boolean = False) cbTipoDocumento.ItemsSource = bdGrupoSanchoToro.db.apuntes.ListaTiposDocumentos End Sub Public Overrides Sub EstableceTitulo() If Me.Estado = tsUtilidades.EstadosAplicacion.ModificandoRegistro Then Dim asiento As asientosmodelos = Me.DataContext Me.docpanel.Caption = "Asiento Modelo " & asiento.idAsientoModelo.ToString Me.docpanel.Tag = "Asiento Modelo." & asiento.idAsientoModelo.ToString Else Me.docpanel.Caption = "Asiento Modelo Nuevo" Me.docpanel.Tag = "Asiento Modelo.Nuevo" End If End Sub Public Overrides Sub Cargado() tvApuntesModelo.ShowSearchPanel(False) End Sub Dim ra As asientosmodelos Public Overrides Function EstableceDCPrincipal(Optional Background As Boolean = False, Optional FuerzaNuevo As Boolean = False, Optional Refrescar As Boolean = False) As tsUtilidades.EstadosAplicacion Dim NuevoEstado As tsUtilidades.EstadosAplicacion If FuerzaNuevo OrElse _idAsiento Is Nothing Then 'Dim uEc = bd.ejercicioscontables.Where(Function(x) x.FechaCierre Is Nothing).OrderByDescending(Function(x) x.FechaInicio).First ra = New asientosmodelos ra.FechaIntroduccion = Now Dim usuarioprueba = bdGrupoSanchoToro.db.Utilidades.idUsuario ra.idUsuarioNavigation = bd.usuarios.First(Function(x) x.idUsuario = bdGrupoSanchoToro.db.Utilidades.idUsuario) If _idAsientoACopiar.HasValue Then Dim asiac = bd.asientos.First(Function(x) x.idAsiento = _idAsientoACopiar) For Each ap In asiac.apuntes Dim napm As New apuntesmodelo ra.apuntesmodelo.Add(napm) With napm .NumeroCuenta = ap.idCuentaNavigation.NumeroCuenta .Concepto = ap.Concepto .Debe = ap.Debe .Haber = ap.Haber .idConcepto = ap.idConcepto .NumeroDocumento = ap.NumeroDocumento .TipoDocumento = ap.TipoDocumento End With Next End If ''sergio listen: preguntar EjerciciosContables a manolo ''ra.ejercicioscontables = uEc NuevoEstado = tsUtilidades.EstadosAplicacion.Nuevo Else Dim id As Integer = _idAsiento ra = bd.asientosmodelos.First(Function(X) X.idAsientoModelo = id) NuevoEstado = tsUtilidades.EstadosAplicacion.ModificandoRegistro End If 'sergio listen: ¿descomentar? 'ra.RellenaCuentaTmp() Me.DataContext = ra gcApuntesModelos.ItemsSource = ra.apuntesmodelo Return NuevoEstado End Function Public Overrides Function ObtieneBD() As tsUtilidades.ItsContexto bd = tscGrupoSanchoToro.NuevoContexto() Return bd End Function 'Public Overrides Function ObtieneConexionBD() As BBDD ' Return bdGrupoSanchoToro.gestionasegasaEntities.bdga 'End Function Public Overrides Function ObtienePermisos() As tsUtilidades.Permisos Return Comun.ObtienePermisos(Me.bd, "AP.SUPERADMINISTRATIVOS", bdGrupoSanchoToro.db.Utilidades.idUsuario) End Function Private Sub ucAsientoModelo_AntesGuardar(sender As Object, e As ItemClickEventArgs, ByRef Cancelar As Boolean, ByRef MensajesError As Hashtable, OpcionGuardado As Integer) Handles Me.AntesGuardar Dim ra As asientosmodelos = Me.DataContext Dim hte As New Hashtable Dim sd = Math.Round(ra.apuntesmodelo.Sum(Function(x) x.Debe), 2, MidpointRounding.AwayFromZero) Dim sh = Math.Round(ra.apuntesmodelo.Sum(Function(x) x.Haber), 2, MidpointRounding.AwayFromZero) If sd <> sh Then hte.Add("Almacenar-SUMA", "La suma del Debe no coincide con la suma del Haber") End If If sd = 0 Then If ra.apuntesmodelo.Count > 0 Then hte.Add("Almacenar-suma0", "La suma de los apuntes no puede ser 0") Else hte.Add("Almacenar-0Apuntes", "Es obligatorio meter al menos 2 apuntes") End If End If If hte.Count > 0 Then MensajesError = hte Cancelar = True End If End Sub Private Sub BtConcepto_DefaultButtonClick(sender As Object, e As RoutedEventArgs) Try Dim asiento As asientosmodelos = Me.DataContext If gcApuntesModelos.CurrentItem Is Nothing Then tvApuntesModelo.AddNewRow() Dim ap As apuntesmodelo = gcApuntesModelos.CurrentItem If tvApuntesModelo.ActiveEditor Is Nothing Then Dispatcher.BeginInvoke(New Action(Sub() tvApuntesModelo.ShowEditor()), System.Windows.Threading.DispatcherPriority.Render) End If Dim dxw As New dxwConceptosPredefinidos(bd) If dxw.ShowDialog Then Dim con = bd.conceptosapuntes.First(Function(x) x.idConcepto = dxw.idConcepto) ap.Concepto = con.Concepto If tvApuntesModelo.ActiveEditor IsNot Nothing Then tvApuntesModelo.ActiveEditor.EditValue = con.Concepto tvApuntesModelo.PostEditor() tvApuntesModelo.CloseEditor() gcApuntesModelos.RefreshRow(tvApuntesModelo.FocusedRowHandle) tvApuntesModelo.MoveNextCell() End If Catch ex As Exception FuncionesDinamicas.ErrorNoControladoAp(Me, ex) DXMessageBox.Show(ex.Message, "Error") End Try End Sub Private Sub ucAsientoModelo_DespuesGuardar(sender As Object, e As ItemClickEventArgs, OpcionGuardado As Integer) Handles Me.DespuesGuardar _idAsientoACopiar = Nothing _idAsiento = ra.idAsientoModelo End Sub Private Sub gcApuntesModelo_EnterPulsado() Handles gcApuntesModelos.EnterPulsado Select Case gcApuntesModelos.CurrentColumn.FieldName.ToLower Case "concepto" If tvApuntesModelo.FocusedRowHandle = DataControlBase.NewItemRowHandle Then Dim ap As apuntes = gcApuntesModelos.CurrentItem Dim apant = ra.apuntesmodelo.FirstOrDefault(Function(x) x.Concepto <> "") If apant IsNot Nothing Then ap.idConcepto = apant.idConcepto ap.Concepto = apant.Concepto End If gcApuntesModelos.RefreshRow(tvApuntesModelo.FocusedRowHandle) End If Case "tipodocumento" If tvApuntesModelo.FocusedRowHandle = DataControlBase.NewItemRowHandle Then Dim ap As apuntes = gcApuntesModelos.CurrentItem Dim apant = ra.apuntesmodelo.FirstOrDefault(Function(x) x.TipoDocumento.HasValue) If apant IsNot Nothing Then ap.TipoDocumento = apant.TipoDocumento End If gcApuntesModelos.RefreshRow(tvApuntesModelo.FocusedRowHandle) End If Case "numerodocumento" If tvApuntesModelo.FocusedRowHandle = DataControlBase.NewItemRowHandle Then Dim ap As apuntes = gcApuntesModelos.CurrentItem Dim apant = ra.apuntesmodelo.FirstOrDefault(Function(x) x.NumeroDocumento.NothingAVacio) If apant IsNot Nothing Then ap.NumeroDocumento = apant.NumeroDocumento End If gcApuntesModelos.RefreshRow(tvApuntesModelo.FocusedRowHandle) End If End Select End Sub Private Sub tvApuntesModelo_CellValueChanged(sender As Object, e As CellValueChangedEventArgs) Handles tvApuntesModelo.CellValueChanged Try Dim ap As apuntesmodelo = e.Row Select Case e.Column.FieldName.ToLower Case "numerocuenta" Dim NumeroCuenta As String = ap.NumeroCuenta.NothingAVacio If NumeroCuenta.Contains(".") Then NumeroCuenta = NumeroCuenta.Replace(".", "0".PadRight(9 - NumeroCuenta.Length, "0")).Acortar(8) ap.NumeroCuenta = NumeroCuenta gcApuntesModelos.RefreshRow(tvApuntesModelo.FocusedRowHandle) End If End Select Catch ex As Exception FuncionesDinamicas.ErrorNoControladoAp(Me, ex) DXMessageBox.Show(ex.Message, "Error") End Try End Sub Private Sub BtCuenta_DefaultButtonClick(sender As Object, e As RoutedEventArgs) Handles btCuenta.DefaultButtonClick Try If tvApuntesModelo.HasValidationError Then tvApuntesModelo.CancelRowEdit() Dim asiento As asientosmodelos = Me.DataContext If gcApuntesModelos.CurrentItem Is Nothing Then tvApuntesModelo.AddNewRow() If tvApuntesModelo.ActiveEditor Is Nothing Then Dispatcher.BeginInvoke(New Action(Sub() tvApuntesModelo.ShowEditor()), System.Windows.Threading.DispatcherPriority.Render) End If Dim idj = bd.ejercicioscontables.OrderByDescending(Function(x) x.FechaInicio).First.idEjercicio Dim dxw As New dxwCuentas(bd, idj) If dxw.ShowDialog Then Dim ap As apuntesmodelo = gcApuntesModelos.CurrentItem Dim cta = bd.cuentas.First(Function(x) x.idCuenta = dxw.idCuenta) ap.NumeroCuenta = cta.NumeroCuenta If tvApuntesModelo.ActiveEditor IsNot Nothing Then tvApuntesModelo.ActiveEditor.EditValue = cta.NumeroCuenta tvApuntesModelo.PostEditor() gcApuntesModelos.RefreshRow(tvApuntesModelo.FocusedRowHandle) tvApuntesModelo.MoveNextCell() End If Catch ex As Exception FuncionesDinamicas.ErrorNoControladoAp(Me, ex) DXMessageBox.Show(ex.Message, "Error") End Try End Sub End Class