diff --git a/Modelo/RepositorioEstadisticas.cs b/Modelo/RepositorioEstadisticas.cs index f6cd96d..0832c51 100644 --- a/Modelo/RepositorioEstadisticas.cs +++ b/Modelo/RepositorioEstadisticas.cs @@ -5,44 +5,59 @@ using Microsoft.EntityFrameworkCore; namespace Modelo; public class RepositorioEstadisticas: RepositorioBase { +public (ChartData, List) InformePagos(int year) { + var con = Context; - public (ChartData, List) InformePagos(int year) - { - var con = Context; + ChartData data = new(); + List meses = ["Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"]; + data.Labels = meses; - ChartData data = new(); - data.Labels = ["Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"]; + var canonData = con.Canons.Where(x => x.Fecha.Year == year) + .Include(x => x.IdreciboNavigation) + .Select(x => new { + Mes = x.Fecha.Month, + Pagado = x.Pagado, + CanonFecha = x.Fecha, + ReciboFecha = x.IdreciboNavigation != null ? x.IdreciboNavigation.Fecha : (DateTime?)null + }) + .ToList(); - var can = con.Canons.Where(x => x.Fecha.Year == year) - .Include(x=>x.IdreciboNavigation) - .GroupBy(x => x.Fecha.Month) - .Select(x=> new InformePagos{Mes = x.Key, - Realizados = x.Count(x=> x.Pagado == 1 && x.IdreciboNavigation.Fecha <= x.Fecha), - Atrasados = x.Count(x=> x.Pagado == 1 && x.IdreciboNavigation.Fecha > x.Fecha), - Sin_realizar = x.Count(x=> x.Pagado == 0)}); + var groupedData = canonData + .GroupBy(x => x.Mes) + .Select(g => new InformePagos { + Mes = g.Key, + Realizados = g.Count(x => x.Pagado == 1 && x.ReciboFecha.HasValue && x.ReciboFecha <= x.CanonFecha), + Atrasados = g.Count(x => x.Pagado == 1 && x.ReciboFecha.HasValue && x.ReciboFecha > x.CanonFecha), + Sin_realizar = g.Count(x => x.Pagado == 0) + }) + .ToList(); - List lst = can.ToList(); + List lst = Enumerable.Range(1, 12) + .Select(mes => groupedData.FirstOrDefault(x => x.Mes == mes) ?? new InformePagos { + Mes = mes, + Realizados = 0, + Atrasados = 0, + Sin_realizar = 0 + }) + .ToList(); - data.Datasets.Add(new Dataset - { - Label = "Realizados", - Data = lst.Select(x => x.Realizados.ToString()).ToList() - }); + data.Datasets.Add(new Dataset { + Label = "Realizados", + Data = lst.Select(x => x.Realizados.ToString()).ToList() + }); - data.Datasets.Add(new Dataset - { - Label = "Atrasados", - Data = lst.Select(x => x.Atrasados.ToString()).ToList() - }); + data.Datasets.Add(new Dataset { + Label = "Atrasados", + Data = lst.Select(x => x.Atrasados.ToString()).ToList() + }); - data.Datasets.Add(new Dataset - { - Label = "Sin_realizar", - Data = lst.Select(x => x.Sin_realizar.ToString()).ToList() - }); + data.Datasets.Add(new Dataset { + Label = "Sin_realizar", + Data = lst.Select(x => x.Sin_realizar.ToString()).ToList() + }); - return (data, lst); - } + return (data, lst); +} public ChartData? ObtenerDataIniciadosPorAño(int year){ var con = Context;