Files
Asegasa.NET/bdAsegasa/Extensiones/cuentas.cs
2026-04-28 11:52:16 +02:00

421 lines
22 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using tsUtilidades.Extensiones;
namespace bdAsegasa.db
{
public partial class cuentas
{
public static int LongitudCuentaFinal = 8;
public const string CUENTA_PERDIDAS_Y_GANANCIAS = "12900000";
public virtual List<apuntes> ApuntesTemporales { get; set; }
public string Descripcion
{
get { return this.NumeroCuenta + " " + this.Denominacion; }
}
public void RellenaApuntesTemporales(bool IncluirAsientoCierre)
{
if (this.Nivel == LongitudCuentaFinal)
{
var apap = this.apuntes.Where(x => x.idAsientoNavigation.Tipo == (int)asientos.TipoAsiento.APERTURA).ToList();
if (apap.Count == 0)
{
this.SaldoInicialTmp = 0;
}
else
{
this.SaldoInicialTmp = Math.Round(apap.Sum(x => x.Debe - x.Haber), 2, MidpointRounding.AwayFromZero);
}
if (IncluirAsientoCierre)
{
ApuntesTemporales = this.apuntes.Where(x => x.idAsientoNavigation.Tipo != (int)asientos.TipoAsiento.APERTURA).ToList();
}
else
{
ApuntesTemporales = this.apuntes.Where(x => x.idAsientoNavigation.Tipo != (int)asientos.TipoAsiento.APERTURA && x.idAsientoNavigation.Tipo != (int)asientos.TipoAsiento.REGULARIZACION).ToList();
}
}
else
{
var bd = tscgestionasegasa.NuevoContexto();
var apap = bd.apuntes.Where(x => x.idCuentaNavigation.NumeroCuenta.StartsWith(this.NumeroCuenta) && x.idAsientoNavigation.Tipo == (int)asientos.TipoAsiento.APERTURA).ToList();
if (apap.Count == 0)
{
this.SaldoInicialTmp = 0;
}
else
{
this.SaldoInicialTmp = Math.Round(apap.Sum(x => x.Debe - x.Haber), 2, MidpointRounding.AwayFromZero);
}
if (IncluirAsientoCierre)
{
ApuntesTemporales = bd.apuntes.Where(x => x.idAsientoNavigation.idEjercicio == this.idEjercicio && x.idCuentaNavigation.NumeroCuenta.StartsWith(this.NumeroCuenta) && x.idAsientoNavigation.Tipo != (int)asientos.TipoAsiento.APERTURA).ToList();
}
else
{
ApuntesTemporales = bd.apuntes.Where(x => x.idAsientoNavigation.idEjercicio == this.idEjercicio && x.idCuentaNavigation.NumeroCuenta.StartsWith(this.NumeroCuenta) && x.idAsientoNavigation.Tipo != (int)asientos.TipoAsiento.REGULARIZACION && x.idAsientoNavigation.Tipo != (int)asientos.TipoAsiento.APERTURA).ToList();
}
}
if (ApuntesTemporales.Count == 0)
{
this.SaldoDebeTmp = 0;
this.SaldoHaberTmp = 0;
}
else
{
this.SaldoDebeTmp = Math.Round(ApuntesTemporales.Sum(x => x.Debe), 2, MidpointRounding.AwayFromZero);
this.SaldoHaberTmp = Math.Round(ApuntesTemporales.Sum(x => x.Haber), 2, MidpointRounding.AwayFromZero);
}
this.SaldoFinalTmp = this.SaldoInicialTmp + this.SaldoDebeTmp - this.SaldoHaberTmp;
}
public double SaldoInicialTmp { get; set; }
public double SaldoDebeTmp { get; set; }
public double SaldoHaberTmp { get; set; }
public double SaldoFinalTmp { get; set; }
public static cuentas CreaCuenta(tscgestionasegasa bd, int idEjercicio, string NumeroCuenta, string Descripcion, string Observaciones = "")
{
var cta = bd.cuentas.FirstOrDefault(x => x.NumeroCuenta == NumeroCuenta && x.idEjercicio == idEjercicio);
if (cta == null)
{
cta = new cuentas();
cta.NumeroCuenta = NumeroCuenta;
cta.idEjercicio = idEjercicio;
cta.Denominacion = Descripcion.Acortar(150);
cta.EsCuentaFinal = true;
cta.Observaciones = Observaciones;
bd.cuentas.Add(cta);
bd.SaveChanges();
}
return cta;
}
public static cuentas CreaCuenta(gestionasegasaEntities bd, DateOnly Fecha, string NumeroCuenta, string Descripcion, string Observaciones = "")
{
var ej = bd.ejercicioscontables.FirstOrDefault(x => x.FechaInicio <= Fecha && x.FechaFin >= Fecha);
if (ej == null) throw new Exception("No existe ningún ejercicio abierto para la fecha " + Fecha.ToString());
return CreaCuenta(bd, ej.idEjercicio, NumeroCuenta, Descripcion, Observaciones);
}
public double TotalSaldoAntesCierre
{
get
{
var ac = this.idEjercicioNavigation.asientos.FirstOrDefault(x => x.Tipo == (int)asientos.TipoAsiento.REGULARIZACION);
if (ac == null)
{
return TotalSaldo;
}
else
{
var tdac = ac.apuntes.Where(x => x.idCuentaNavigation.NumeroCuenta.StartsWith(this.NumeroCuenta)).Sum(x => x.Debe);
var thac = ac.apuntes.Where(x => x.idCuentaNavigation.NumeroCuenta.StartsWith(this.NumeroCuenta)).Sum(x => x.Haber);
return TotalSaldo - tdac + thac;
}
}
}
public string Grupo1
{
get
{
if (this.NumeroCuenta.Length > 1)
{
string nc = this.NumeroCuenta.Substring(0, 1);
return nc + " - " + this.idEjercicioNavigation.cuentas.First(x => x.NumeroCuenta == nc).Denominacion;
}
return "";
}
}
public string Grupo2
{
get
{
if (this.NumeroCuenta.Length > 2)
{
string nc = this.NumeroCuenta.Substring(0, 2);
return nc + " - " + this.idEjercicioNavigation.cuentas.First(x => x.NumeroCuenta == nc).Denominacion;
}
return "";
}
}
public string Grupo3
{
get
{
if (this.NumeroCuenta.Length > 3)
{
string nc = this.NumeroCuenta.Substring(0, 3);
return nc + " - " + this.idEjercicioNavigation.cuentas.First(x => x.NumeroCuenta == nc).Denominacion;
}
return "";
}
}
public string Grupo4
{
get
{
if (this.NumeroCuenta.Length > 4)
{
string nc = this.NumeroCuenta.Substring(0, 4);
return nc + " - " + this.idEjercicioNavigation.cuentas.First(x => x.NumeroCuenta == nc).Denominacion;
}
return "";
}
}
private gestionasegasaEntities _bd;
public gestionasegasaEntities bd
{
get
{
if (_bd == null) _bd = (gestionasegasaEntities?)tscgestionasegasa.NuevoContexto();
return _bd;
}
}
public cuentas CuentaSuperior1
{
get
{
if (this.NumeroCuenta.Length == 1) return null;
string superior = this.NumeroCuenta.Substring(0, 1);
return bd.cuentas.First(x => x.NumeroCuenta == superior && x.idEjercicio == this.idEjercicio);
}
}
public cuentas CuentaSuperior2
{
get
{
if (this.NumeroCuenta.Length < 3) return null;
string superior = this.NumeroCuenta.Substring(0, 2);
return bd.cuentas.First(x => x.NumeroCuenta == superior && x.idEjercicio == this.idEjercicio);
}
}
public cuentas CuentaSuperior3
{
get
{
if (this.NumeroCuenta.Length < 4) return null;
string superior = this.NumeroCuenta.Substring(0, 3);
return bd.cuentas.First(x => x.NumeroCuenta == superior && x.idEjercicio == this.idEjercicio);
}
}
public cuentas CuentaSuperior4
{
get
{
if (this.NumeroCuenta.Length < 5) return null;
string superior = this.NumeroCuenta.Substring(0, 4);
return bd.cuentas.First(x => x.NumeroCuenta == superior && x.idEjercicio == this.idEjercicio);
}
}
public int Nivel
{
get { return this.NumeroCuenta.NothingAVacio().Length; }
}
private vf_cuentas _ValoresExtendidos;
public vf_cuentas ValoresExtendidos
{
get
{
if (_ValoresExtendidos == null) _ValoresExtendidos = Obtiene_vf_cuenta();
return _ValoresExtendidos;
}
}
public vf_cuentas Obtiene_vf_cuenta()
{
try
{
var bd = tscgestionasegasa.NuevoContexto();
vf_cuentas ct;
if (this.Nivel == 8)
{
ct = bd.vf_cuentas.First(x => x.idCuenta == this.idCuenta);
}
else
{
if (Nivel == 0)
{
ct = new vf_cuentas();
// Initialize all zero
}
else
{
var sumatorio = bd.vf_cuentas.Where(x => x.NumeroCuenta.StartsWith(this.NumeroCuenta) && x.idEjercicio == this.idEjercicio).ToList();
ct = new vf_cuentas();
ct.idCuenta = this.idCuenta;
ct.idEjercicio = this.idEjercicio;
ct.Mote = this.Mote;
ct.Denominacion = this.Denominacion;
ct.idEmpresaAmortizacion = this.idEmpresaAmortizacion;
ct.NumeroCuenta = this.NumeroCuenta;
ct.Observaciones = this.Observaciones;
ct.PresupuestoEnero = Math.Round(sumatorio.Sum(x => x.PresupuestoEnero), 2, MidpointRounding.AwayFromZero);
ct.PresupuestoFebrero = Math.Round(sumatorio.Sum(x => x.PresupuestoFebrero), 2, MidpointRounding.AwayFromZero);
ct.PresupuestoMarzo = Math.Round(sumatorio.Sum(x => x.PresupuestoMarzo), 2, MidpointRounding.AwayFromZero);
ct.PresupuestoAbril = Math.Round(sumatorio.Sum(x => x.PresupuestoAbril), 2, MidpointRounding.AwayFromZero);
ct.PresupuestoMayo = Math.Round(sumatorio.Sum(x => x.PresupuestoMayo), 2, MidpointRounding.AwayFromZero);
ct.PresupuestoJunio = Math.Round(sumatorio.Sum(x => x.PresupuestoJunio), 2, MidpointRounding.AwayFromZero);
ct.PresupuestoJulio = Math.Round(sumatorio.Sum(x => x.PresupuestoJulio), 2, MidpointRounding.AwayFromZero);
ct.PresupuestoAgosto = Math.Round(sumatorio.Sum(x => x.PresupuestoAgosto), 2, MidpointRounding.AwayFromZero);
ct.PresupuestoSeptiembre = Math.Round(sumatorio.Sum(x => x.PresupuestoSeptiembre), 2, MidpointRounding.AwayFromZero);
ct.PresupuestoOctubre = Math.Round(sumatorio.Sum(x => x.PresupuestoOctubre), 2, MidpointRounding.AwayFromZero);
ct.PresupuestoNoviembre = Math.Round(sumatorio.Sum(x => x.PresupuestoNoviembre), 2, MidpointRounding.AwayFromZero);
ct.PresupuestoDiciembre = Math.Round(sumatorio.Sum(x => x.PresupuestoDiciembre), 2, MidpointRounding.AwayFromZero);
ct.DebeEnero = Math.Round(sumatorio.Sum(x => x.DebeEnero.NothingA0()), 2, MidpointRounding.AwayFromZero);
ct.DebeFebrero = Math.Round(sumatorio.Sum(x => x.DebeFebrero.NothingA0()), 2, MidpointRounding.AwayFromZero);
ct.DebeMarzo = Math.Round(sumatorio.Sum(x => x.DebeMarzo.NothingA0()), 2, MidpointRounding.AwayFromZero);
ct.DebeAbril = Math.Round(sumatorio.Sum(x => x.DebeAbril.NothingA0()), 2, MidpointRounding.AwayFromZero);
ct.DebeMayo = Math.Round(sumatorio.Sum(x => x.DebeMayo.NothingA0()), 2, MidpointRounding.AwayFromZero);
ct.DebeJunio = Math.Round(sumatorio.Sum(x => x.DebeJunio.NothingA0()), 2, MidpointRounding.AwayFromZero);
ct.DebeJulio = Math.Round(sumatorio.Sum(x => x.DebeJulio.NothingA0()), 2, MidpointRounding.AwayFromZero);
ct.DebeAgosto = Math.Round(sumatorio.Sum(x => x.DebeAgosto.NothingA0()), 2, MidpointRounding.AwayFromZero);
ct.DebeSeptiembre = Math.Round(sumatorio.Sum(x => x.DebeSeptiembre.NothingA0()), 2, MidpointRounding.AwayFromZero);
ct.DebeOctubre = Math.Round(sumatorio.Sum(x => x.DebeOctubre.NothingA0()), 2, MidpointRounding.AwayFromZero);
ct.DebeNoviembre = Math.Round(sumatorio.Sum(x => x.DebeNoviembre.NothingA0()), 2, MidpointRounding.AwayFromZero);
ct.DebeDiciembre = Math.Round(sumatorio.Sum(x => x.DebeDiciembre.NothingA0()), 2, MidpointRounding.AwayFromZero);
ct.HaberEnero = Math.Round(sumatorio.Sum(x => x.HaberEnero.NothingA0()), 2, MidpointRounding.AwayFromZero);
ct.HaberFebrero = Math.Round(sumatorio.Sum(x => x.HaberFebrero.NothingA0()), 2, MidpointRounding.AwayFromZero);
ct.HaberMarzo = Math.Round(sumatorio.Sum(x => x.HaberMarzo.NothingA0()), 2, MidpointRounding.AwayFromZero);
ct.HaberAbril = Math.Round(sumatorio.Sum(x => x.HaberAbril.NothingA0()), 2, MidpointRounding.AwayFromZero);
ct.HaberMayo = Math.Round(sumatorio.Sum(x => x.HaberMayo.NothingA0()), 2, MidpointRounding.AwayFromZero);
ct.HaberJunio = Math.Round(sumatorio.Sum(x => x.HaberJunio.NothingA0()), 2, MidpointRounding.AwayFromZero);
ct.HaberJulio = Math.Round(sumatorio.Sum(x => x.HaberJulio.NothingA0()), 2, MidpointRounding.AwayFromZero);
ct.HaberAgosto = Math.Round(sumatorio.Sum(x => x.HaberAgosto.NothingA0()), 2, MidpointRounding.AwayFromZero);
ct.HaberSeptiembre = Math.Round(sumatorio.Sum(x => x.HaberSeptiembre.NothingA0()), 2, MidpointRounding.AwayFromZero);
ct.HaberOctubre = Math.Round(sumatorio.Sum(x => x.HaberOctubre.NothingA0()), 2, MidpointRounding.AwayFromZero);
ct.HaberNoviembre = Math.Round(sumatorio.Sum(x => x.HaberNoviembre.NothingA0()), 2, MidpointRounding.AwayFromZero);
ct.HaberDiciembre = Math.Round(sumatorio.Sum(x => x.HaberDiciembre.NothingA0()), 2, MidpointRounding.AwayFromZero);
ct.TotalDebe = Math.Round(sumatorio.Sum(x => x.TotalDebe), 2, MidpointRounding.AwayFromZero);
ct.TotalHaber = Math.Round(sumatorio.Sum(x => x.TotalHaber), 2, MidpointRounding.AwayFromZero);
}
}
if (Nivel > 0)
{
var cta1 = bd.cuentas.First(x => x.NumeroCuenta == ct.NumeroCuenta.Substring(0, 1) && x.idEjercicio == this.idEjercicio);
ct.Grupo1 = cta1.NumeroCuenta + " " + cta1.Denominacion;
if (ct.Nivel > 1)
{
var cta2 = bd.cuentas.First(x => x.NumeroCuenta == ct.NumeroCuenta.Substring(0, 2) && x.idEjercicio == this.idEjercicio);
ct.Grupo2 = cta2.NumeroCuenta + " " + cta2.Denominacion;
if (ct.Nivel > 2)
{
var cta3 = bd.cuentas.First(x => x.NumeroCuenta == ct.NumeroCuenta.Substring(0, 3) && x.idEjercicio == this.idEjercicio);
ct.Grupo3 = cta3.NumeroCuenta + " " + cta3.Denominacion;
if (ct.Nivel > 3)
{
var cta4 = bd.cuentas.First(x => x.NumeroCuenta == ct.NumeroCuenta.Substring(0, 4) && x.idEjercicio == this.idEjercicio);
ct.Grupo4 = cta4.NumeroCuenta + " " + cta4.Denominacion;
}
}
}
}
return ct;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
#region Saldos
public double SaldoEnero => ValoresExtendidos.SaldoEnero;
public double SaldoFebrero => ValoresExtendidos.SaldoFebrero;
public double SaldoMarzo => ValoresExtendidos.SaldoMarzo;
public double SaldoAbril => ValoresExtendidos.SaldoAbril;
public double SaldoMayo => ValoresExtendidos.SaldoMayo;
public double SaldoJunio => ValoresExtendidos.SaldoJunio;
public double SaldoJulio => ValoresExtendidos.SaldoJulio;
public double SaldoAgosto => ValoresExtendidos.SaldoAgosto;
public double SaldoSeptiembre => ValoresExtendidos.SaldoSeptiembre;
public double SaldoOctubre => ValoresExtendidos.SaldoOctubre;
public double SaldoNoviembre => ValoresExtendidos.SaldoNoviembre;
public double SaldoDiciembre => ValoresExtendidos.SaldoDiciembre;
public double TotalSaldo => ValoresExtendidos.TotalSaldo;
#endregion
#region Presupuestos
public double TotalPresupuestado
{
get
{
return Math.Round(PresupuestoEnero + PresupuestoFebrero + PresupuestoMarzo + PresupuestoAbril + PresupuestoMayo + PresupuestoJunio + PresupuestoJulio + PresupuestoAgosto + PresupuestoSeptiembre + PresupuestoOctubre + PresupuestoNoviembre + PresupuestoDiciembre, 2, MidpointRounding.AwayFromZero);
}
}
public void ActualizaTotalPresupuestado() => OnPropertyChanged("TotalPresupuestado");
public double DesvioPresupuestoEnero => ValoresExtendidos.DesvioPresupuestoEnero;
public double DesvioPresupuestoFebrero => ValoresExtendidos.DesvioPresupuestoFebrero;
public double DesvioPresupuestoMarzo => ValoresExtendidos.DesvioPresupuestoMarzo;
public double DesvioPresupuestoAbril => ValoresExtendidos.DesvioPresupuestoAbril;
public double DesvioPresupuestoMayo => ValoresExtendidos.DesvioPresupuestoMayo;
public double DesvioPresupuestoJunio => ValoresExtendidos.DesvioPresupuestoJunio;
public double DesvioPresupuestoJulio => ValoresExtendidos.DesvioPresupuestoJulio;
public double DesvioPresupuestoAgosto => ValoresExtendidos.DesvioPresupuestoAgosto;
public double DesvioPresupuestoSeptiembre => ValoresExtendidos.DesvioPresupuestoSeptiembre;
public double DesvioPresupuestoOctubre => ValoresExtendidos.DesvioPresupuestoOctubre;
public double DesvioPresupuestoNoviembre => ValoresExtendidos.DesvioPresupuestoNoviembre;
public double DesvioPresupuestoDiciembre => ValoresExtendidos.DesvioPresupuestoDiciembre;
public double DesvioPresupuestoTotal => ValoresExtendidos.DesvioPresupuestoTotal;
public double PorcentajeDesvioPresupuestoEnero => ValoresExtendidos.PorcentajeDesvioPresupuestoEnero;
public double PorcentajeDesvioPresupuestoFebrero => ValoresExtendidos.PorcentajeDesvioPresupuestoFebrero;
public double PorcentajeDesvioPresupuestoMarzo => ValoresExtendidos.PorcentajeDesvioPresupuestoMarzo;
public double PorcentajeDesvioPresupuestoAbril => ValoresExtendidos.PorcentajeDesvioPresupuestoAbril;
public double PorcentajeDesvioPresupuestoMayo => ValoresExtendidos.PorcentajeDesvioPresupuestoMayo;
public double PorcentajeDesvioPresupuestoJunio => ValoresExtendidos.PorcentajeDesvioPresupuestoJunio;
public double PorcentajeDesvioPresupuestoJulio => ValoresExtendidos.PorcentajeDesvioPresupuestoJulio;
public double PorcentajeDesvioPresupuestoAgosto => ValoresExtendidos.PorcentajeDesvioPresupuestoAgosto;
public double PorcentajeDesvioPresupuestoSeptiembre => ValoresExtendidos.PorcentajeDesvioPresupuestoSeptiembre;
public double PorcentajeDesvioPresupuestoOctubre => ValoresExtendidos.PorcentajeDesvioPresupuestoOctubre;
public double PorcentajeDesvioPresupuestoNoviembre => ValoresExtendidos.PorcentajeDesvioPresupuestoNoviembre;
public double PorcentajeDesvioPresupuestoDiciembre => ValoresExtendidos.PorcentajeDesvioPresupuestoDiciembre;
public double PorcentajeDesvioPresupuestoTotal => ValoresExtendidos.PorcentajeDesvioPresupuestoTotal;
#endregion
public static List<GrupoCuenta> ListadoGruposCuentas()
{
return new List<GrupoCuenta>
{
new GrupoCuenta { Nivel = 1, Descripcion = "GRUPO INICIAL 1 DÍGITO" },
new GrupoCuenta { Nivel = 2, Descripcion = "GRUPO INTERMEDIO 2 DÍGITOS" },
new GrupoCuenta { Nivel = 3, Descripcion = "GRUPO INTERMEDIO 3 DÍGITOS" },
new GrupoCuenta { Nivel = 4, Descripcion = "GRUPO INTERMEDIO 4 DÍGITOS" },
new GrupoCuenta { Nivel = 8, Descripcion = "GRUPO FINAL 8 DÍGITOS" }
};
}
}
public class GrupoCuenta
{
public int Nivel { get; set; }
public string Descripcion { get; set; }
}
}