157 lines
5.4 KiB
C#
157 lines
5.4 KiB
C#
using Entidades;
|
|
using Entidades.Informes;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Modelo;
|
|
public class RepositorioEstadisticas: RepositorioBase<RepositorioEstadisticas> {
|
|
|
|
public (ChartData, List<InformePagos>) InformePagos(int year) {
|
|
var con = Context;
|
|
|
|
ChartData data = new();
|
|
List<string> meses = ["Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"];
|
|
data.Labels = meses;
|
|
|
|
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 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<InformePagos> 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 = "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()
|
|
});
|
|
|
|
return (data, lst);
|
|
}
|
|
|
|
public ChartData? ObtenerDataIniciadosPorAño(int year){
|
|
var con = Context;
|
|
var contratosPorMes = con.Contratos
|
|
.Where(c => c.Habilitado == 1 && c.Fechainicio.Year == year)
|
|
.GroupBy(c => c.Fechainicio.Month)
|
|
.Select(g => new { Mes = g.Key, Cantidad = g.Count() })
|
|
.OrderBy(x => x.Mes).ToList();
|
|
|
|
if (!contratosPorMes.Any()) return null;
|
|
|
|
var data = new ChartData();
|
|
data.Labels = ["Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"];
|
|
|
|
List<string> ddd = new List<string>(["0","0","0","0","0","0","0","0","0","0","0","0"]);
|
|
foreach (var contrato in contratosPorMes) {
|
|
ddd[contrato.Mes-1] = contrato.Cantidad.ToString();
|
|
}
|
|
|
|
Dataset dd = new();
|
|
dd.Data = ddd;
|
|
dd.Label = $"Alquileres por Mes del año {year}";
|
|
data.Datasets.Add(dd);
|
|
return data;
|
|
}
|
|
|
|
public ChartData? ObtenerDataDuracionContratos() {
|
|
var con = Context;
|
|
var meses = con.Contratos.Where(x=>x.Habilitado ==1)
|
|
.GroupBy(x=>x.MesesDurationContrato).Select(g=> new {Duracion = g.Key, Cantidad = g.Count()});
|
|
if(!meses.Any()) return null;
|
|
|
|
var data = new ChartData();
|
|
Dataset dd = new();
|
|
|
|
foreach (var mes in meses){
|
|
data.Labels.Add(mes.Duracion.ToString());
|
|
dd.Data.Add(mes.Cantidad.ToString());
|
|
}
|
|
dd.Label="Duracion Contratos";
|
|
data.Datasets.Add(dd);
|
|
return data;
|
|
}
|
|
|
|
public ChartData? ObtenerDatosPagosContrato(long idcontrato) {
|
|
var con = Context;
|
|
var cont = con.Contratos.Include(c=>c.Idcanons).ThenInclude(x=>x.IdreciboNavigation)
|
|
.Where(x=>x.Habilitado==1)
|
|
.FirstOrDefault(x=>x.Id == idcontrato);
|
|
if(cont==null)return null;
|
|
|
|
var data = new ChartData();
|
|
data.Labels= ["Pago", "Sin Pagar", "Pago Atrasado"];
|
|
|
|
Dataset d1 = new();
|
|
d1.Label="Estadistica Pagos";
|
|
|
|
var l = new List<string>(["0","0","0"]);
|
|
|
|
l[0] = cont.Idcanons.Where(x=>x.Pagado == 1Lu && x.IdreciboNavigation != null && x.IdreciboNavigation.Fecha <= x.Fecha)
|
|
.Count().ToString();
|
|
|
|
l[1] = cont.Idcanons.Where(x=>x.Pagado==0).Count().ToString();
|
|
|
|
l[2] = cont.Idcanons.Where(x=>x.Pagado == 1Lu && x.IdreciboNavigation != null && x.IdreciboNavigation.Fecha > x.Fecha)
|
|
.Count().ToString();
|
|
d1.Data.AddRange(l);
|
|
data.Datasets.Add(d1);
|
|
|
|
return data;
|
|
}
|
|
|
|
public IQueryable<Contrato> TablaObtenerContratosIniciadosPorAño(int year) {
|
|
var con = Context;
|
|
var contratosPorMes = con.Contratos.Include(x=>x.IddivisaNavigation).Include(x=>x.IdpropiedadNavigation)
|
|
.Where(c => c.Habilitado == 1 && c.Fechainicio.Year == year);
|
|
return contratosPorMes;
|
|
}
|
|
|
|
public List<InformesMeses> TablaObtenerDataDuracionContratos() {
|
|
var con = Context;
|
|
var meses = con.Contratos.Where(x=>x.Habilitado ==1)
|
|
.GroupBy(x=>x.MesesDurationContrato).Select(g=> new {Duracion = g.Key, Cantidad = g.Count()});
|
|
|
|
List<InformesMeses> l = new();
|
|
foreach (var mes in meses){
|
|
l.Add(new InformesMeses{
|
|
Meses = mes.Duracion,
|
|
Repes = mes.Cantidad
|
|
});
|
|
}
|
|
return l;
|
|
|
|
}
|
|
|
|
}
|