Files
SanchoToro/GestionGrupoSanchoToro/Contabilidad/Aplicaciones/ucBalanceSumasYSaldos.xaml.vb
2026-01-30 11:10:56 +01:00

282 lines
15 KiB
VB.net

Imports System.Data.Entity
Imports DevExpress.Xpf.Core.ServerMode
Imports bdGrupoSanchoToro
Imports DevExpress.Xpf.Grid
Imports DevExpress.Xpf.Core
Imports System.Linq.Dynamic
Imports DevExpress.Xpf.Bars
Imports tsl5.Datos
Imports tsWPF
Imports DevExpress.Data
Imports tsWPF.Comun
Imports tsWPFCore.Comun
Imports tsWPFCore
Imports tsUtilidades
Imports bdGrupoSanchoToro.db
Imports bdGrupoSanchoToro.db.Utilidades
Public Class ucBalanceSumasYSaldos
Private TotalGastos As Double
Private TotalIngresos As Double
Dim bd As tscGrupoSanchoToro
' Private _Ejercicios As List(Of Integer)
' Private _NivelCuentas As Integer
Public Overrides ReadOnly Property idRegistroAplicacionActual As String
Get
Return "BalanceSumasYSaldos" & _Nivel.ToString
End Get
End Property
Public Overrides ReadOnly Property DescripcionRegistro As String
Get
Return "Cuentas"
End Get
End Property
Public Overrides ReadOnly Property NombreTablaBase As String
Get
Return "Cuentas"
End Get
End Property
Public Overrides Sub EstableceDataContextSecundarios(Optional Background As Boolean = False)
End Sub
Public Overrides Sub EstableceTitulo()
Me.docpanel.Caption = "Balance de Sumas y Saldos (" & _Nivel.ToString & " DÍGITOS)"
End Sub
Private _Nivel As Integer
Public Overrides Sub Cargado()
_Nivel = CInt(Rutinas.Ttagi(Me.OtrosParametros, "OPAR"))
Dim Ejercicios = bd.ejercicioscontables.OrderByDescending(Function(x) x.FechaInicio).ToList
Dim ejab = Ejercicios.Where(Function(x) x.FechaCierre Is Nothing)
deFechaInicio.EditValue = ejab.OrderBy(Function(x) x.FechaInicio).First.FechaInicio
deFechaFin.EditValue = ejab.OrderByDescending(Function(x) x.FechaFin).First.FechaFin
End Sub
Public Overrides Function EstableceDCPrincipal(Optional Background As Boolean = False, Optional FuerzaNuevo As Boolean = False, Optional Refrescar As Boolean = False) As EstadosAplicacion
Return EstadosAplicacion.AplicacionSinIndice
End Function
Public Overrides Function ObtieneBD() As tsUtilidades.ItsContexto
bd = tscGrupoSanchoToro.NuevoContexto()
Return bd
End Function
'Public Overrides Function ObtieneConexionBD() As BBDD
' Return bdCOASVD.GestionAsegasaEntities.bdga
'End Function
Public Overrides Function ObtienePermisos() As tsUtilidades.Permisos
Return Comun.ObtienePermisos(Me.bd, "AP.CONTABILIDAD", idUsuario)
End Function
Public Sub New()
' Esta llamada es exigida por el diseñador.
InitializeComponent()
'Me.lgFiltrosBusquedaRapida.Visibility = Visibility.Collapsed
'Me.lgFiltrosBusquedaAvanzada.Visibility = Visibility.Visible
' Agregue cualquier inicialización después de la llamada a InitializeComponent().
End Sub
Public Function ObtieneApuntes(BackGround As Boolean, FechaInicio As DateOnly, FechaFin As DateOnly, Nivel As Integer, IncluirAsientoRegularizacion As Boolean) As List(Of CuentaBalanceSumasYSaldos)
Dim parametros(0) As Object
Dim iNumExc As Integer = 0
Dim lcbe As New List(Of CuentaBalanceSumasYSaldos)
Do
Try
Dim ctas = bd.cuentas.Where(Function(x) ((x.idEjercicioNavigation.FechaInicio <= FechaInicio And FechaInicio <= x.idEjercicioNavigation.FechaFin) OrElse (FechaInicio <= x.idEjercicioNavigation.FechaInicio And FechaFin >= x.idEjercicioNavigation.FechaInicio)) And x.NumeroCuenta.Length = Nivel).ToList()
'Dim ctas = bd.cuentas.Where(Function(x) x.NumeroCuenta.Length = Nivel).GroupBy(Function(x) x.NumeroCuenta).Select(Function(x) x.OrderByDescending(Function(y) y.ejercicioscontables.FechaInicio).FirstOrDefault).Select(Function(x) New With {.NumeroCuenta = x.NumeroCuenta, .Denominacion = x.Denominacion})
Dim fit1 As DateOnly = New DateOnly(FechaInicio.Year, 1, 1)
Dim fit2 As DateOnly = New DateOnly(FechaInicio.Year, 4, 1)
Dim fit3 As DateOnly = New DateOnly(FechaInicio.Year, 7, 1)
Dim fit4 As DateOnly = New DateOnly(FechaInicio.Year, 10, 1)
Dim fft1 As DateOnly = New DateOnly(FechaInicio.Year, 3, 31)
Dim fft2 As DateOnly = New DateOnly(FechaInicio.Year, 6, 30)
Dim fft3 As DateOnly = New DateOnly(FechaInicio.Year, 9, 30)
Dim fft4 As DateOnly = New DateOnly(FechaInicio.Year, 12, 31)
Dim ff As DateOnly = If(fft4 > FechaFin, fft4, FechaFin)
If Nivel = 8 Then
Dim FechaInicioPeriodo As New DateOnly(FechaInicio.Year, 1, 1)
Dim iqApuntes As IQueryable(Of apuntes)
If IncluirAsientoRegularizacion Then
iqApuntes = bd.apuntes.Include("asientos").AsQueryable
Else
iqApuntes = bd.apuntes.Include("asientos").Where(Function(x) x.idAsientoNavigation.Tipo <> asientos.TipoAsiento.REGULARIZACION)
End If
lcbe = iqApuntes.Where(Function(x) x.idAsientoNavigation.Fecha >= FechaInicioPeriodo AndAlso x.idAsientoNavigation.Fecha <= ff).ToList.GroupBy(Function(x) New With {Key .Cta = x.idCuentaNavigation.NumeroCuenta}) _
.Select(Function(g) New CuentaBalanceSumasYSaldos With {.Cuenta = g.Key.Cta,
.Denominacion = ctas.FirstOrDefault(Function(c) c.NumeroCuenta = g.Key.Cta).Denominacion,
.Debe = g.Where(Function(x) x.idAsientoNavigation.Fecha >= FechaInicio AndAlso x.idAsientoNavigation.Fecha <= FechaFin).Sum(Function(y) y.Debe),
.Haber = g.Where(Function(x) x.idAsientoNavigation.Fecha >= FechaInicio AndAlso x.idAsientoNavigation.Fecha <= FechaFin).Sum(Function(y) y.Haber),
.DebeAcumulado = g.Where(Function(x) x.idAsientoNavigation.Fecha <= FechaFin).Sum(Function(x) x.Debe),
.HaberAcumulado = g.Where(Function(x) x.idAsientoNavigation.Fecha <= FechaFin).Sum(Function(x) x.Haber),
.DebeT1 = g.Where(Function(x) x.idAsientoNavigation.Fecha >= fit1 AndAlso x.idAsientoNavigation.Fecha <= fft1).Sum(Function(y) y.Debe),
.HaberT1 = g.Where(Function(x) x.idAsientoNavigation.Fecha >= fit1 AndAlso x.idAsientoNavigation.Fecha <= fft1).Sum(Function(y) y.Haber),
.DebeT2 = g.Where(Function(x) x.idAsientoNavigation.Fecha >= fit2 AndAlso x.idAsientoNavigation.Fecha <= fft2).Sum(Function(y) y.Debe),
.HaberT2 = g.Where(Function(x) x.idAsientoNavigation.Fecha >= fit2 AndAlso x.idAsientoNavigation.Fecha <= fft2).Sum(Function(y) y.Haber),
.DebeT3 = g.Where(Function(x) x.idAsientoNavigation.Fecha >= fit3 AndAlso x.idAsientoNavigation.Fecha <= fft3).Sum(Function(y) y.Debe),
.HaberT3 = g.Where(Function(x) x.idAsientoNavigation.Fecha >= fit3 AndAlso x.idAsientoNavigation.Fecha <= fft3).Sum(Function(y) y.Haber),
.DebeT4 = g.Where(Function(x) x.idAsientoNavigation.Fecha >= fit4 AndAlso x.idAsientoNavigation.Fecha <= fft4).Sum(Function(y) y.Debe),
.HaberT4 = g.Where(Function(x) x.idAsientoNavigation.Fecha >= fit4 AndAlso x.idAsientoNavigation.Fecha <= fft4).Sum(Function(y) y.Haber)
}).OrderBy(Function(x) x.Cuenta).ToList
Else
Dim FechaInicioPeriodo As New DateOnly(FechaInicio.Year, 1, 1)
Dim iqApuntes As IQueryable(Of apuntes)
If IncluirAsientoRegularizacion Then
iqApuntes = bd.apuntes.Include("asientos").AsQueryable
Else
iqApuntes = bd.apuntes.Include("asientos").Where(Function(x) x.idAsientoNavigation.Tipo <> asientos.TipoAsiento.REGULARIZACION)
End If
lcbe = iqApuntes.Where(Function(x) x.idAsientoNavigation.Fecha >= FechaInicioPeriodo And x.idAsientoNavigation.Fecha <= ff).ToList.GroupBy(Function(x) New With {Key .CtaSuperior = x.idCuentaNavigation.NumeroCuenta.Substring(0, Nivel)}) _
.Select(Function(g) New CuentaBalanceSumasYSaldos With {.Cuenta = g.Key.CtaSuperior,
.Denominacion = ctas.FirstOrDefault(Function(c) c.NumeroCuenta = g.Key.CtaSuperior).Denominacion,
.Debe = g.Where(Function(x) x.idAsientoNavigation.Fecha >= FechaInicio AndAlso x.idAsientoNavigation.Fecha <= FechaFin).Sum(Function(y) y.Debe),
.Haber = g.Where(Function(x) x.idAsientoNavigation.Fecha >= FechaInicio AndAlso x.idAsientoNavigation.Fecha <= FechaFin).Sum(Function(y) y.Haber),
.DebeAcumulado = g.Where(Function(x) x.idAsientoNavigation.Fecha <= FechaFin).Sum(Function(x) x.Debe),
.HaberAcumulado = g.Where(Function(x) x.idAsientoNavigation.Fecha <= FechaFin).Sum(Function(x) x.Haber),
.DebeT1 = g.Where(Function(x) x.idAsientoNavigation.Fecha >= fit1 AndAlso x.idAsientoNavigation.Fecha <= fft1).Sum(Function(y) y.Debe),
.HaberT1 = g.Where(Function(x) x.idAsientoNavigation.Fecha >= fit1 AndAlso x.idAsientoNavigation.Fecha <= fft1).Sum(Function(y) y.Haber),
.DebeT2 = g.Where(Function(x) x.idAsientoNavigation.Fecha >= fit2 AndAlso x.idAsientoNavigation.Fecha <= fft2).Sum(Function(y) y.Debe),
.HaberT2 = g.Where(Function(x) x.idAsientoNavigation.Fecha >= fit2 AndAlso x.idAsientoNavigation.Fecha <= fft2).Sum(Function(y) y.Haber),
.DebeT3 = g.Where(Function(x) x.idAsientoNavigation.Fecha >= fit3 AndAlso x.idAsientoNavigation.Fecha <= fft3).Sum(Function(y) y.Debe),
.HaberT3 = g.Where(Function(x) x.idAsientoNavigation.Fecha >= fit3 AndAlso x.idAsientoNavigation.Fecha <= fft3).Sum(Function(y) y.Haber),
.DebeT4 = g.Where(Function(x) x.idAsientoNavigation.Fecha >= fit4 AndAlso x.idAsientoNavigation.Fecha <= fft4).Sum(Function(y) y.Debe),
.HaberT4 = g.Where(Function(x) x.idAsientoNavigation.Fecha >= fit4 AndAlso x.idAsientoNavigation.Fecha <= fft4).Sum(Function(y) y.Haber)
}).OrderBy(Function(x) x.Cuenta).ToList
End If
Exit Do
Catch ex As Exception
If BackGround = False Then Throw New Exception(ex.Message, ex)
iNumExc += 1
If iNumExc > 3 Then
Throw New Exception(ex.Message, ex)
End If
End Try
Loop
Return lcbe
End Function
Public Async Sub ObtieneApuntesAsync(bd As tscGrupoSanchoToro, Background As Boolean)
Try
Dim rs As New List(Of CuentaBalanceSumasYSaldos)
Dim fi, ff As DateOnly?
fi = deFechaInicio.EditValue
ff = deFechaFin.EditValue
If Background Then
If fi.HasValue AndAlso ff.HasValue Then
Me.ContenedorAplicacion.IsEnabled = False
Await Task.Run(Sub()
rs = ObtieneApuntes(Background, fi, ff, _Nivel, cbIncluirAsientoApertura.IsChecked)
End Sub)
End If
Else
If fi.HasValue = False OrElse ff.HasValue = False Then
DXMessageBox.Show("Las fechas de inicio y fin son obligatorias", "Atención")
Else
If fi.Value.Year <> ff.Value.Year Then
DXMessageBox.Show("Atención los ejercicios contables desde la fecha de inicio a la de fin son diferentes por lo que los datos trimestrales solo serán del primer año", "Atención")
End If
If DXSplashScreen.IsActive = False Then DXSplashScreen.Show(Of tsWPFCore.SplashScreenTecnosis)()
DXSplashScreen.SetState("Buscando Apuntes ...")
rs = ObtieneApuntes(Background, fi, ff, _Nivel, cbIncluirAsientoApertura.IsChecked)
End If
End If
gc.ItemsSource = rs
DataContext = rs
gc.ExpandAllGroups()
Catch ex As Exception
If DXSplashScreen.IsActive Then DXSplashScreen.Close()
DXMessageBox.Show(ex.Message, "Error")
Finally
Me.ContenedorAplicacion.IsEnabled = True
If DXSplashScreen.IsActive Then DXSplashScreen.Close()
End Try
End Sub
Private Sub Nuevo() Handles Me.BotonNuevoPulsado
FuncionesDinamicas.AbrirAP(New ucCuenta, OtrosParametros)
End Sub
Private Sub ApLineas_Enlazar(Celda As EditGridCellData, Defecto As Boolean) Handles Me.Enlazar
Dim id As Integer = DirectCast(Me.gc.CurrentItem, cuentas).idCuenta
Dim uc As New ucCuenta(id)
FuncionesDinamicas.AbrirAP(New ucCuenta(id), OtrosParametros)
End Sub
Private Sub BtBuscar_Click(sender As Object, e As RoutedEventArgs)
ObtieneApuntesAsync(bd, False)
End Sub
End Class
Public Class CuentaBalanceSumasYSaldos
Property Cuenta As String
Property Denominacion As String
Property Debe As Double
Property Haber As Double
ReadOnly Property Saldo As Double
Get
Return Math.Round(Debe - Haber, 2, MidpointRounding.AwayFromZero)
End Get
End Property
Property DebeT1 As Double
Property HaberT1 As Double
ReadOnly Property SaldoT1 As Double
Get
Return Math.Round(DebeT1 - HaberT1, 2, MidpointRounding.AwayFromZero)
End Get
End Property
Property DebeT2 As Double
Property HaberT2 As Double
ReadOnly Property SaldoT2 As Double
Get
Return Math.Round(DebeT2 - HaberT2, 2, MidpointRounding.AwayFromZero)
End Get
End Property
Property DebeT3 As Double
Property HaberT3 As Double
ReadOnly Property SaldoT3 As Double
Get
Return Math.Round(DebeT3 - HaberT3, 2, MidpointRounding.AwayFromZero)
End Get
End Property
Property DebeT4 As Double
Property HaberT4 As Double
ReadOnly Property SaldoT4 As Double
Get
Return Math.Round(DebeT4 - HaberT4, 2, MidpointRounding.AwayFromZero)
End Get
End Property
Property DebeAcumulado As Double
Property HaberAcumulado As Double
ReadOnly Property SaldoAcumulado As Double
Get
Return Math.Round(DebeAcumulado - HaberAcumulado, 2, MidpointRounding.AwayFromZero)
End Get
End Property
End Class