116 lines
5.0 KiB
C#
116 lines
5.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using bdAsegasa.dbcontext;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace bdAsegasa.db
|
|
{
|
|
public partial class ejercicioscontables
|
|
{
|
|
public static int ObtieneidEjercicioAbierto(DateTime fecha)
|
|
{
|
|
using (var bd = tscgestionasegasa.NuevoContexto())
|
|
{
|
|
var ej = bd.ejercicioscontables.FirstOrDefault(x => x.FechaInicio <= DateOnly.FromDateTime(fecha) && x.FechaFin >= DateOnly.FromDateTime(fecha) && x.FechaCierre == null);
|
|
if (ej == null)
|
|
{
|
|
ej = bd.ejercicioscontables.FirstOrDefault(x => x.FechaInicio <= DateOnly.FromDateTime(fecha) && x.FechaFin >= DateOnly.FromDateTime(fecha) && x.FechaCierre != null);
|
|
if (ej == null)
|
|
{
|
|
if (Math.Abs((DateTime.Today - fecha).TotalDays) > 365)
|
|
{
|
|
throw new Exception("La fecha del ejercicio " + fecha.ToShortDateString() + " parece incorrecta");
|
|
}
|
|
else
|
|
{
|
|
return AbreEjercicio(bd, fecha).idEjercicio;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
throw new Exception("El ejercicio está cerrado");
|
|
}
|
|
}
|
|
return ej.idEjercicio;
|
|
}
|
|
}
|
|
|
|
public string DescripcionExtendida
|
|
{
|
|
get
|
|
{
|
|
if (this.FechaInicio.Month == 1 && this.FechaInicio.Day == 1 && this.FechaFin.Month == 12 && this.FechaFin.Day == 31)
|
|
{
|
|
return $"{this.idEmpresaNavigation?.RazonSocial} {this.FechaInicio.Year}";
|
|
}
|
|
return $"{this.idEmpresaNavigation?.RazonSocial} {this.FechaInicio} {this.FechaFin}";
|
|
}
|
|
}
|
|
|
|
public void CopiaCuentas(tscgestionasegasa bd)
|
|
{
|
|
var ea = bd.ejercicioscontables
|
|
.Where(x => x.FechaInicio < this.FechaInicio)
|
|
.OrderByDescending(x => x.FechaInicio)
|
|
.FirstOrDefault();
|
|
|
|
if (ea != null)
|
|
{
|
|
var ctas = bd.cuentas.Where(x => x.idEjercicio == ea.idEjercicio).ToList();
|
|
foreach (var cta in ctas.OrderBy(x => x.NumeroCuenta.Length))
|
|
{
|
|
if (!bd.cuentas.Any(x => x.idEjercicio == this.idEjercicio && x.NumeroCuenta == cta.NumeroCuenta))
|
|
{
|
|
var nc = new cuentas
|
|
{
|
|
Denominacion = cta.Denominacion,
|
|
EsCuentaFinal = cta.EsCuentaFinal,
|
|
Mote = cta.Mote,
|
|
Observaciones = cta.Observaciones,
|
|
NumeroCuenta = cta.NumeroCuenta,
|
|
idEjercicio = this.idEjercicio,
|
|
idEmpresaAmortizacion = cta.idEmpresaAmortizacion,
|
|
PresupuestoEnero = cta.PresupuestoEnero,
|
|
PresupuestoFebrero = cta.PresupuestoFebrero,
|
|
PresupuestoMarzo = cta.PresupuestoMarzo,
|
|
PresupuestoAbril = cta.PresupuestoAbril,
|
|
PresupuestoMayo = cta.PresupuestoMayo,
|
|
PresupuestoJunio = cta.PresupuestoJunio,
|
|
PresupuestoJulio = cta.PresupuestoJulio,
|
|
PresupuestoAgosto = cta.PresupuestoAgosto,
|
|
PresupuestoSeptiembre = cta.PresupuestoSeptiembre,
|
|
PresupuestoOctubre = cta.PresupuestoOctubre,
|
|
PresupuestoNoviembre = cta.PresupuestoNoviembre,
|
|
PresupuestoDiciembre = cta.PresupuestoDiciembre
|
|
};
|
|
bd.cuentas.Add(nc);
|
|
}
|
|
}
|
|
bd.SaveChanges();
|
|
}
|
|
}
|
|
|
|
public static ejercicioscontables AbreEjercicio(tscgestionasegasa bd, DateTime fecha)
|
|
{
|
|
var ej = new ejercicioscontables
|
|
{
|
|
FechaApertura = DateOnly.FromDateTime(DateTime.Now),
|
|
FechaInicio = new DateOnly(fecha.Year, 1, 1),
|
|
FechaFin = new DateOnly(fecha.Year, 12, 31),
|
|
Descripcion = fecha.Year.ToString()
|
|
};
|
|
bd.ejercicioscontables.Add(ej);
|
|
bd.SaveChanges();
|
|
ej.CopiaCuentas(bd);
|
|
return ej;
|
|
}
|
|
|
|
public static bool CompruebaEjereciciosAbiertos(tscgestionasegasa bd, List<DateTime> fechas)
|
|
{
|
|
var ejeabiertos = bd.ejercicioscontables.Where(x => x.FechaCierre == null).ToList();
|
|
return fechas.All(f => ejeabiertos.Any(e => e.FechaInicio <= DateOnly.FromDateTime(f) && e.FechaFin >= DateOnly.FromDateTime(f)));
|
|
}
|
|
}
|
|
}
|