using System; using System.ComponentModel; using System.Runtime.CompilerServices; using Microsoft.VisualBasic.CompilerServices; using tsUtilidades.Extensiones; namespace bdGrupoSanchoToro.db { public partial class cuentas { public static int LongitudCuentaFinal { get { return (8); } } public const string CUENTA_PERDIDAS_Y_GANANCIAS = "12900000"; public List ApuntesTemporales { get; set; } public string Descripcion { get { return this.NumeroCuenta + " " + this.Denominacion; } } public void RellenaApuntesTemporales(bool IncluirAsientoCierre) { if (this.Nivel == LongitudCuentaFinal) { int TipoApertura = (int)asientos.TipoAsiento.APERTURA; int TipoRegularizacion = (int)asientos.TipoAsiento.REGULARIZACION; var apap = this.apuntes.Where(x => x.idAsientoNavigation.Tipo == TipoApertura).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 != TipoApertura).ToList(); else ApuntesTemporales = this.apuntes.Where(x => x.idAsientoNavigation.Tipo != TipoApertura & x.idAsientoNavigation.Tipo != TipoRegularizacion).ToList(); } else { int TipoApertura = (int)asientos.TipoAsiento.APERTURA; int TipoRegularizacion = (int)asientos.TipoAsiento.REGULARIZACION; bdGrupoSanchoToro.tscGrupoSanchoToro.NuevoContexto(); var apap = bd.apuntes.Where(x => x.idCuentaNavigation.NumeroCuenta.StartsWith(this.NumeroCuenta) & x.idAsientoNavigation.Tipo == TipoApertura ).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 != TipoApertura).ToList(); else ApuntesTemporales = bd.apuntes.Where(x => x.idAsientoNavigation.idEjercicio == this.idEjercicio & x.idCuentaNavigation.NumeroCuenta.StartsWith(this.NumeroCuenta) & x.idAsientoNavigation.Tipo != TipoRegularizacion & x.idAsientoNavigation.Tipo != TipoApertura).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(tscGrupoSanchoToro 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(); { var withBlock = cta; withBlock.NumeroCuenta = NumeroCuenta; withBlock.idEjercicio = idEjercicio; withBlock.Denominacion = Descripcion.Acortar(150); withBlock.EsCuentaFinal = true; withBlock.Observaciones = Observaciones; } bd.cuentas.Add(cta); bd.SaveChanges(); } return cta; } public static cuentas CreaCuenta(tscGrupoSanchoToro 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); } int TipoApertura = (int)asientos.TipoAsiento.APERTURA; int TipoRegularizacion = (int)asientos.TipoAsiento.REGULARIZACION; public double TotalSaldoAntesCierre { get { var ac = this.idEjercicioNavigation.asientos.FirstOrDefault(x => x.Tipo == TipoRegularizacion); 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) { var nc = this.NumeroCuenta.Substring(0, 1); return nc + " - " + this.idEjercicioNavigation.cuentas.First(x => x.NumeroCuenta == nc).Denominacion; } else return ""; } } public string Grupo2 { get { if (this.NumeroCuenta.Length > 2) { var nc = this.NumeroCuenta.Substring(0, 2); return nc + " - " + this.idEjercicioNavigation.cuentas.First(x => x.NumeroCuenta == nc).Denominacion; } else return ""; } } public string Grupo3 { get { if (this.NumeroCuenta.Length > 3) { var nc = this.NumeroCuenta.Substring(0, 3); return nc + " - " + this.idEjercicioNavigation.cuentas.First(x => x.NumeroCuenta == nc).Denominacion; } else return ""; } } public string Grupo4 { get { if (this.NumeroCuenta.Length > 4) { var nc = this.NumeroCuenta.Substring(0, 4); return nc + " - " + this.idEjercicioNavigation.cuentas.First(x => x.NumeroCuenta == nc).Denominacion; } else return ""; } } private tscGrupoSanchoToro _bd; public tscGrupoSanchoToro bd { get { if (_bd == null) _bd = bdGrupoSanchoToro.tscGrupoSanchoToro.NuevoContexto(); return _bd; } } public cuentas CuentaSuperior1 { get { if (this.NumeroCuenta.Length == 1) return null/* TODO Change to default(_) if this is not a reference type */; else { var CuentaSuperior = this.NumeroCuenta.Substring(0, 1); return bd.cuentas.First(x => x.NumeroCuenta == CuentaSuperior & x.idEjercicio == this.idEjercicio); } } } public cuentas CuentaSuperior2 { get { if (this.NumeroCuenta.Length < 3) return null/* TODO Change to default(_) if this is not a reference type */; else { var CuentaSuperior = this.NumeroCuenta.Substring(0, 2); return bd.cuentas.First(x => x.NumeroCuenta == CuentaSuperior & x.idEjercicio == this.idEjercicio); } } } public cuentas CuentaSuperior3 { get { if (this.NumeroCuenta.Length < 4) return null/* TODO Change to default(_) if this is not a reference type */; else { var CuentaSuperior = this.NumeroCuenta.Substring(0, 3); return bd.cuentas.First(x => x.NumeroCuenta == CuentaSuperior & x.idEjercicio == this.idEjercicio); } } } public cuentas CuentaSuperior4 { get { if (this.NumeroCuenta.Length < 5) return null/* TODO Change to default(_) if this is not a reference type */; else { var CuentaSuperior = this.NumeroCuenta.Substring(0, 4); return bd.cuentas.First(x => x.NumeroCuenta == CuentaSuperior & x.idEjercicio == this.idEjercicio); } } } public int Nivel { get { return 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 { tscGrupoSanchoToro bd = tscGrupoSanchoToro.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(); { var withBlock = ct; withBlock.idCuenta = 0; withBlock.idEjercicio = 0; withBlock.Mote = ""; withBlock.Denominacion = ""; withBlock.idEmpresaAmortizacion = 0; withBlock.NumeroCuenta = ""; withBlock.Observaciones = ""; withBlock.PresupuestoEnero = 0; withBlock.PresupuestoFebrero = 0; withBlock.PresupuestoMarzo = 0; withBlock.PresupuestoAbril = 0; withBlock.PresupuestoMayo = 0; withBlock.PresupuestoJunio = 0; withBlock.PresupuestoJulio = 0; withBlock.PresupuestoAgosto = 0; withBlock.PresupuestoSeptiembre = 0; withBlock.PresupuestoOctubre = 0; withBlock.PresupuestoNoviembre = 0; withBlock.PresupuestoDiciembre = 0; withBlock.DebeEnero = 0; withBlock.DebeFebrero = 0; withBlock.DebeMarzo = 0; withBlock.DebeAbril = 0; withBlock.DebeMayo = 0; withBlock.DebeJunio = 0; withBlock.DebeJulio = 0; withBlock.DebeAgosto = 0; withBlock.DebeSeptiembre = 0; withBlock.DebeOctubre = 0; withBlock.DebeNoviembre = 0; withBlock.DebeDiciembre = 0; withBlock.HaberEnero = 0; withBlock.HaberFebrero = 0; withBlock.HaberMarzo = 0; withBlock.HaberAbril = 0; withBlock.HaberMayo = 0; withBlock.HaberJunio = 0; withBlock.HaberJulio = 0; withBlock.HaberAgosto = 0; withBlock.HaberSeptiembre = 0; withBlock.HaberOctubre = 0; withBlock.HaberNoviembre = 0; withBlock.HaberDiciembre = 0; withBlock.TotalDebe = 0; withBlock.TotalHaber = 0; } } else { var sumatorio = bd.vf_cuentas.Where(x => x.NumeroCuenta.StartsWith(this.NumeroCuenta) & x.idEjercicio == this.idEjercicio).ToList(); ct = new vf_cuentas(); { var withBlock = ct; withBlock.idCuenta = this.idCuenta; withBlock.idEjercicio = this.idEjercicio; withBlock.Mote = this.Mote; withBlock.Denominacion = this.Denominacion; withBlock.idEmpresaAmortizacion = this.idEmpresaAmortizacion; withBlock.NumeroCuenta = this.NumeroCuenta; withBlock.Observaciones = this.Observaciones; withBlock.PresupuestoEnero = Math.Round(sumatorio.Sum(x => x.PresupuestoEnero), 2, MidpointRounding.AwayFromZero); withBlock.PresupuestoFebrero = Math.Round(sumatorio.Sum(x => x.PresupuestoEnero), 2, MidpointRounding.AwayFromZero); withBlock.PresupuestoMarzo = Math.Round(sumatorio.Sum(x => x.PresupuestoMarzo), 2, MidpointRounding.AwayFromZero); withBlock.PresupuestoAbril = Math.Round(sumatorio.Sum(x => x.PresupuestoAbril), 2, MidpointRounding.AwayFromZero); withBlock.PresupuestoMayo = Math.Round(sumatorio.Sum(x => x.PresupuestoMayo), 2, MidpointRounding.AwayFromZero); withBlock.PresupuestoJunio = Math.Round(sumatorio.Sum(x => x.PresupuestoJunio), 2, MidpointRounding.AwayFromZero); withBlock.PresupuestoJulio = Math.Round(sumatorio.Sum(x => x.PresupuestoJulio), 2, MidpointRounding.AwayFromZero); withBlock.PresupuestoAgosto = Math.Round(sumatorio.Sum(x => x.PresupuestoAgosto), 2, MidpointRounding.AwayFromZero); withBlock.PresupuestoSeptiembre = Math.Round(sumatorio.Sum(x => x.PresupuestoSeptiembre), 2, MidpointRounding.AwayFromZero); withBlock.PresupuestoOctubre = Math.Round(sumatorio.Sum(x => x.PresupuestoOctubre), 2, MidpointRounding.AwayFromZero); withBlock.PresupuestoNoviembre = Math.Round(sumatorio.Sum(x => x.PresupuestoNoviembre), 2, MidpointRounding.AwayFromZero); withBlock.PresupuestoDiciembre = Math.Round(sumatorio.Sum(x => x.PresupuestoDiciembre), 2, MidpointRounding.AwayFromZero); withBlock.DebeEnero = Math.Round(sumatorio.Sum(x => x.DebeEnero.NothingA0()), 2, MidpointRounding.AwayFromZero); withBlock.DebeFebrero = Math.Round(sumatorio.Sum(x => x.DebeEnero.NothingA0()), 2, MidpointRounding.AwayFromZero); withBlock.DebeMarzo = Math.Round(sumatorio.Sum(x => x.DebeMarzo.NothingA0()), 2, MidpointRounding.AwayFromZero); withBlock.DebeAbril = Math.Round(sumatorio.Sum(x => x.DebeAbril.NothingA0()), 2, MidpointRounding.AwayFromZero); withBlock.DebeMayo = Math.Round(sumatorio.Sum(x => x.DebeMayo.NothingA0()), 2, MidpointRounding.AwayFromZero); withBlock.DebeJunio = Math.Round(sumatorio.Sum(x => x.DebeJunio.NothingA0()), 2, MidpointRounding.AwayFromZero); withBlock.DebeJulio = Math.Round(sumatorio.Sum(x => x.DebeJulio.NothingA0()), 2, MidpointRounding.AwayFromZero); withBlock.DebeAgosto = Math.Round(sumatorio.Sum(x => x.DebeAgosto.NothingA0()), 2, MidpointRounding.AwayFromZero); withBlock.DebeSeptiembre = Math.Round(sumatorio.Sum(x => x.DebeSeptiembre.NothingA0()), 2, MidpointRounding.AwayFromZero); withBlock.DebeOctubre = Math.Round(sumatorio.Sum(x => x.DebeOctubre.NothingA0()), 2, MidpointRounding.AwayFromZero); withBlock.DebeNoviembre = Math.Round(sumatorio.Sum(x => x.DebeNoviembre.NothingA0()), 2, MidpointRounding.AwayFromZero); withBlock.DebeDiciembre = Math.Round(sumatorio.Sum(x => x.DebeDiciembre.NothingA0()), 2, MidpointRounding.AwayFromZero); withBlock.HaberEnero = Math.Round(sumatorio.Sum(x => x.HaberEnero.NothingA0()), 2, MidpointRounding.AwayFromZero); withBlock.HaberFebrero = Math.Round(sumatorio.Sum(x => x.HaberEnero.NothingA0()), 2, MidpointRounding.AwayFromZero); withBlock.HaberMarzo = Math.Round(sumatorio.Sum(x => x.HaberMarzo.NothingA0()), 2, MidpointRounding.AwayFromZero); withBlock.HaberAbril = Math.Round(sumatorio.Sum(x => x.HaberAbril.NothingA0()), 2, MidpointRounding.AwayFromZero); withBlock.HaberMayo = Math.Round(sumatorio.Sum(x => x.HaberMayo.NothingA0()), 2, MidpointRounding.AwayFromZero); withBlock.HaberJunio = Math.Round(sumatorio.Sum(x => x.HaberJunio.NothingA0()), 2, MidpointRounding.AwayFromZero); withBlock.HaberJulio = Math.Round(sumatorio.Sum(x => x.HaberJulio.NothingA0()), 2, MidpointRounding.AwayFromZero); withBlock.HaberAgosto = Math.Round(sumatorio.Sum(x => x.HaberAgosto.NothingA0()), 2, MidpointRounding.AwayFromZero); withBlock.HaberSeptiembre = Math.Round(sumatorio.Sum(x => x.HaberSeptiembre.NothingA0()), 2, MidpointRounding.AwayFromZero); withBlock.HaberOctubre = Math.Round(sumatorio.Sum(x => x.HaberOctubre.NothingA0()), 2, MidpointRounding.AwayFromZero); withBlock.HaberNoviembre = Math.Round(sumatorio.Sum(x => x.HaberNoviembre.NothingA0()), 2, MidpointRounding.AwayFromZero); withBlock.HaberDiciembre = Math.Round(sumatorio.Sum(x => x.HaberDiciembre.NothingA0()), 2, MidpointRounding.AwayFromZero); withBlock.TotalDebe = Math.Round(sumatorio.Sum(x => x.TotalDebe), 2, MidpointRounding.AwayFromZero); withBlock.TotalHaber = Math.Round(sumatorio.Sum(x => x.TotalHaber), 2, MidpointRounding.AwayFromZero); } } try { 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; } } } } } catch (Exception ex) { throw new Exception("Error obteniendo cuentas intermedia de la cuenta " + this.NumeroCuenta + " " + ex.Message, ex); } return ct; } catch (Exception ex) { throw new Exception(ex.Message, ex); } } #region "Saldos" public double SaldoEnero { get { return ValoresExtendidos.SaldoEnero; } } public double SaldoFebrero { get { return ValoresExtendidos.SaldoFebrero; } } public double SaldoMarzo { get { return ValoresExtendidos.SaldoMarzo; } } public double SaldoAbril { get { return ValoresExtendidos.SaldoAbril; } } public double SaldoMayo { get { return ValoresExtendidos.SaldoMayo; } } public double SaldoJunio { get { return ValoresExtendidos.SaldoJunio; } } public double SaldoJulio { get { return ValoresExtendidos.SaldoJulio; } } public double SaldoAgosto { get { return ValoresExtendidos.SaldoAgosto; } } public double SaldoSeptiembre { get { return ValoresExtendidos.SaldoSeptiembre; } } public double SaldoOctubre { get { return ValoresExtendidos.SaldoOctubre; } } public double SaldoNoviembre { get { return ValoresExtendidos.SaldoNoviembre; } } public double SaldoDiciembre { get { return ValoresExtendidos.SaldoDiciembre; } } public double TotalSaldo { get { return 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 { get { return ValoresExtendidos.DesvioPresupuestoEnero; } } public double DesvioPresupuestoFebrero { get { return ValoresExtendidos.DesvioPresupuestoFebrero; } } public double DesvioPresupuestoMarzo { get { return ValoresExtendidos.DesvioPresupuestoMarzo; } } public double DesvioPresupuestoAbril { get { return ValoresExtendidos.DesvioPresupuestoAbril; } } public double DesvioPresupuestoMayo { get { return ValoresExtendidos.DesvioPresupuestoMayo; } } public double DesvioPresupuestoJunio { get { return ValoresExtendidos.DesvioPresupuestoJunio; } } public double DesvioPresupuestoJulio { get { return ValoresExtendidos.DesvioPresupuestoJulio; } } public double DesvioPresupuestoAgosto { get { return ValoresExtendidos.DesvioPresupuestoAgosto; } } public double DesvioPresupuestoSeptiembre { get { return ValoresExtendidos.DesvioPresupuestoSeptiembre; } } public double DesvioPresupuestoOctubre { get { return ValoresExtendidos.DesvioPresupuestoOctubre; } } public double DesvioPresupuestoNoviembre { get { return ValoresExtendidos.DesvioPresupuestoNoviembre; } } public double DesvioPresupuestoDiciembre { get { return ValoresExtendidos.DesvioPresupuestoDiciembre; } } public double DesvioPresupuestoTotal { get { return ValoresExtendidos.DesvioPresupuestoTotal; } } public double PorcentajeDesvioPresupuestoEnero { get { return ValoresExtendidos.PorcentajeDesvioPresupuestoEnero; } } public double PorcentajeDesvioPresupuestoFebrero { get { return ValoresExtendidos.PorcentajeDesvioPresupuestoFebrero; } } public double PorcentajeDesvioPresupuestoMarzo { get { return ValoresExtendidos.PorcentajeDesvioPresupuestoMarzo; } } public double PorcentajeDesvioPresupuestoAbril { get { return ValoresExtendidos.PorcentajeDesvioPresupuestoAbril; } } public double PorcentajeDesvioPresupuestoMayo { get { return ValoresExtendidos.PorcentajeDesvioPresupuestoMayo; } } public double PorcentajeDesvioPresupuestoJunio { get { return ValoresExtendidos.PorcentajeDesvioPresupuestoJunio; } } public double PorcentajeDesvioPresupuestoJulio { get { return ValoresExtendidos.PorcentajeDesvioPresupuestoJulio; } } public double PorcentajeDesvioPresupuestoAgosto { get { return ValoresExtendidos.PorcentajeDesvioPresupuestoAgosto; } } public double PorcentajeDesvioPresupuestoSeptiembre { get { return ValoresExtendidos.PorcentajeDesvioPresupuestoSeptiembre; } } public double PorcentajeDesvioPresupuestoOctubre { get { return ValoresExtendidos.PorcentajeDesvioPresupuestoOctubre; } } public double PorcentajeDesvioPresupuestoNoviembre { get { return ValoresExtendidos.PorcentajeDesvioPresupuestoNoviembre; } } public double PorcentajeDesvioPresupuestoDiciembre { get { return ValoresExtendidos.PorcentajeDesvioPresupuestoDiciembre; } } public double PorcentajeDesvioPresupuestoTotal { get { return ValoresExtendidos.PorcentajeDesvioPresupuestoTotal; } } #endregion public static List ListadoGruposCuentas() { List lGrupos = new List(); lGrupos.Add(new GrupoCuenta() { Nivel = 1, Descripcion = "GRUPO INICIAL 1 DÍGITO" }); lGrupos.Add(new GrupoCuenta() { Nivel = 2, Descripcion = "GRUPO INTERMEDIO 2 DÍGITOS" }); lGrupos.Add(new GrupoCuenta() { Nivel = 3, Descripcion = "GRUPO INTERMEDIO 3 DÍGITOS" }); lGrupos.Add(new GrupoCuenta() { Nivel = 4, Descripcion = "GRUPO INTERMEDIO 4 DÍGITOS" }); lGrupos.Add(new GrupoCuenta() { Nivel = 8, Descripcion = "GRUPO FINAL 8 DÍGITOS" }); return lGrupos; } public class GrupoCuenta { public int Nivel { get; set; } public string Descripcion { get; set; } } } }