240 lines
8.8 KiB
C#
240 lines
8.8 KiB
C#
using Entidades;
|
|
using Entidades.Dto;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Modelo;
|
|
public class RepositorioContratos: RepositorioBase<RepositorioContratos> {
|
|
public IQueryable<Contrato>? ObtenerContratosPorEmailInquilino(string email) {
|
|
var con = Context;
|
|
try{
|
|
var listcont = con.Contratos.Where(x=>x.DniinquilinoNavigation.Email == email);
|
|
return listcont;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public bool CargaPrecontrato( Contrato? c = null, Notificacione? n = null) {
|
|
if (c == null || c.Habilitado == 1) return false;
|
|
if (n == null) return false;
|
|
var con = Context;
|
|
|
|
var prop = con.Propiedades.FirstOrDefault(x=>x.Id==c.Idpropiedad);
|
|
if (prop == null) return false;
|
|
prop.Idestado = 2;
|
|
|
|
c.Iddivisa = prop.Iddivisa;
|
|
c.Id = (con.Contratos.Any() ? con.Contratos.Max(x => x.Id) : 0) + 1;
|
|
c.Monto = prop.Monto;
|
|
|
|
con.Contratos.Add(c);
|
|
con.Notificaciones.Add(n);
|
|
|
|
return Guardar(con);
|
|
}
|
|
public bool CargaGarantes(List<Garante> gar, string emailInquilino, int idpropiedad) {
|
|
var con = Context;
|
|
Contrato? contr = con.Contratos.Include(x=>x.DniinquilinoNavigation).Include(x=>x.Idgarantes)
|
|
.FirstOrDefault(x=>x.Idpropiedad == idpropiedad &&
|
|
x.DniinquilinoNavigation.Email == emailInquilino &&
|
|
x.Habilitado == 0 &&
|
|
x.Cancelado == 0);
|
|
|
|
if (contr == null) return false;
|
|
int inicial = (con.Garantes.Any()? con.Garantes.Max(x=>x.Id): 0) + 1;
|
|
foreach (var i in gar) {
|
|
i.Id = inicial;
|
|
inicial++;
|
|
con.Garantes.Add(i);
|
|
contr.Idgarantes.Add(i);
|
|
}
|
|
|
|
return Guardar(con);
|
|
}
|
|
|
|
public int CantidadGarantesEncontrato(string emailInquilino, int idpropiedad) {
|
|
var con = Context;
|
|
Contrato? contr = con.Contratos.Include(x=>x.DniinquilinoNavigation)
|
|
.FirstOrDefault(x=>x.Idpropiedad == idpropiedad &&
|
|
x.DniinquilinoNavigation.Email == emailInquilino &&
|
|
x.Habilitado == 0 &&
|
|
x.Cancelado == 0);
|
|
if (contr == null) return 0;
|
|
return contr.Cantgarantemin;
|
|
}
|
|
|
|
public Contrato? ObtenerContrato(string emailInquilino, int idpropiedad) {
|
|
var con = Context;
|
|
Contrato? contr = con.Contratos.Include(x=>x.DniinquilinoNavigation)
|
|
.FirstOrDefault(x=>x.Idpropiedad == idpropiedad &&
|
|
x.DniinquilinoNavigation.Email == emailInquilino &&
|
|
x.Habilitado == 0 &&
|
|
x.Cancelado == 0);
|
|
if (contr == null) return null;
|
|
return contr;
|
|
|
|
}
|
|
|
|
public bool CancelarPrecontrato(string emailInquilino, int idpropiedad) {
|
|
var con = Context;
|
|
Contrato? contr = con.Contratos.Include(x=>x.DniinquilinoNavigation)
|
|
.FirstOrDefault(x=>x.Idpropiedad == idpropiedad &&
|
|
x.DniinquilinoNavigation.Email == emailInquilino &&
|
|
x.Habilitado == 0 &&
|
|
x.Cancelado == 0);
|
|
|
|
if (contr == null || contr.IdpropiedadNavigation == null) return false;
|
|
contr.Cancelado = 1;
|
|
contr.IdpropiedadNavigation.Idestado = 1;
|
|
return Guardar(con);
|
|
}
|
|
|
|
public Contrato? ObtenerPreContratoPorId(long idcontrato) {
|
|
var con = Context;
|
|
Contrato? contr = con.Contratos
|
|
.Include(x=>x.DnipropietarioNavigation)
|
|
.Include(x=>x.Idgarantes)
|
|
.Include(x=>x.DniinquilinoNavigation)
|
|
.Where(x=>x.Cancelado == 0 && x.Habilitado == 0)
|
|
.FirstOrDefault(x=>x.Id == idcontrato);
|
|
if (contr == null) return null;
|
|
return contr;
|
|
}
|
|
|
|
public bool AddUrl(long id, string nuevoNombreArchivo) {
|
|
var con = Context;
|
|
Contrato? contrato = con.Contratos
|
|
.FirstOrDefault(x=>x.Id == id);
|
|
if (contrato == null) return false;
|
|
contrato.UrlContrato = nuevoNombreArchivo;
|
|
return Guardar(con);
|
|
}
|
|
|
|
public bool AceptarContrato(long idcontrato) {
|
|
var con = Context;
|
|
Contrato? cont = con.Contratos
|
|
.Include(x=>x.Idcanons)
|
|
.Include(x=>x.IdpropiedadNavigation)
|
|
.FirstOrDefault(x=>x.Id ==idcontrato &&
|
|
x.Habilitado ==0 &&
|
|
x.Cancelado == 0);
|
|
|
|
if (cont == null || cont.IdpropiedadNavigation==null) return false;
|
|
|
|
cont.Habilitado = 1;
|
|
cont.Fechainicio = DateTime.Now;
|
|
var fecha = cont.Fechainicio;
|
|
if (cont.MesesDurationContrato < cont.MesesHastaAumento) {
|
|
for (int i = 0; i < cont.MesesDurationContrato; i++) {
|
|
Canon can = new Canon{
|
|
Fecha = fecha.AddMonths(i),
|
|
Monto = cont.IdpropiedadNavigation.Monto,
|
|
Pagado = 0,
|
|
};
|
|
can.Id = (con.Canons.Any()? con.Canons.Count() :0)+1+i;
|
|
|
|
|
|
con.Canons.Add(can);
|
|
cont.Idcanons.Add(can);
|
|
}
|
|
} else {
|
|
for (int i = 0; i < cont.MesesHastaAumento; i++) {
|
|
Canon can = new Canon{
|
|
Fecha = fecha.AddMonths(i),
|
|
Monto = cont.IdpropiedadNavigation.Monto,
|
|
Pagado = 0,
|
|
};
|
|
can.Id = (con.Canons.Any()? con.Canons.Count() :0)+1+i;
|
|
|
|
|
|
con.Canons.Add(can);
|
|
cont.Idcanons.Add(can);
|
|
}
|
|
}
|
|
return Guardar(con);
|
|
}
|
|
|
|
public bool CancelarPrecontrato(long idcontrato) {
|
|
var con = Context;
|
|
Contrato? cont = con.Contratos.Include(x=>x.IdpropiedadNavigation).FirstOrDefault(x=>x.Id ==idcontrato && x.Habilitado ==0);
|
|
if (cont == null|| cont.IdpropiedadNavigation==null) return false;
|
|
cont.Cancelado = 1;
|
|
cont.IdpropiedadNavigation.Idestado = 1;
|
|
return Guardar(con);
|
|
}
|
|
|
|
public IQueryable<Contrato> ObtenerContratosDePropietario(long dni) {
|
|
var con = Context;
|
|
var l = con.Contratos
|
|
.Include(x=>x.DniinquilinoNavigation)
|
|
.Include(x=>x.DnipropietarioNavigation)
|
|
.Include(x=>x.IdpropiedadNavigation)
|
|
.ThenInclude(x=>x.IdtipropiedadNavigation)
|
|
.Where(x=>x.Dnipropietario == dni);
|
|
return l;
|
|
}
|
|
|
|
public IQueryable<Contrato> ObtenerContratosDeInquilino(long dni) {
|
|
var con = Context;
|
|
var l = con.Contratos
|
|
.Include(x=>x.DniinquilinoNavigation)
|
|
.Include(x=>x.DnipropietarioNavigation)
|
|
.Include(x=>x.IdpropiedadNavigation)
|
|
.ThenInclude(x=>x.IdtipropiedadNavigation)
|
|
.Where(x=>x.Dniinquilino == dni);
|
|
return l;
|
|
}
|
|
|
|
public Contrato? ObtenerContratoPorId(long idcontrato) {
|
|
var con = Context;
|
|
var l = con.Contratos
|
|
.Include(x=>x.Idgarantes)
|
|
.Include(x=>x.DniinquilinoNavigation)
|
|
.Include(x=>x.DnipropietarioNavigation)
|
|
.Include(x=>x.IdpropiedadNavigation)
|
|
.ThenInclude(x=>x.IdtipropiedadNavigation)
|
|
.FirstOrDefault(x=>x.Id == idcontrato);
|
|
return l;
|
|
}
|
|
|
|
public bool HayContratosEnAño(int year) {
|
|
var con = Context;
|
|
return con.Contratos.Where(x=>x.Fechainicio.Year == year).Any();
|
|
}
|
|
|
|
public bool SetOpcionVenta(Venta v, long idcontrato) {
|
|
var con = Context;
|
|
var cont = con.Contratos.Include(x=>x.IdventaNavigation).FirstOrDefault(x=>x.Id == idcontrato);
|
|
if (cont != null) return false;
|
|
|
|
v.Id = (con.Ventas.Any()?con.Ventas.Count():0)+1;
|
|
|
|
cont.IdventaNavigation = v;
|
|
con.Ventas.Add(v);
|
|
return Guardar(con);
|
|
}
|
|
|
|
public bool CargaPrecontratoOpcionVenta(Contrato c, Notificacione n, Venta v) {
|
|
if (c == null || c.Habilitado == 1) return false;
|
|
if (n == null) return false;
|
|
var con = Context;
|
|
|
|
var prop = con.Propiedades.FirstOrDefault(x=>x.Id==c.Idpropiedad);
|
|
if (prop == null) return false;
|
|
prop.Idestado = 2;
|
|
|
|
c.Iddivisa = prop.Iddivisa;
|
|
c.Id = (con.Contratos.Any() ? con.Contratos.Max(x => x.Id) : 0) + 1;
|
|
c.Monto = prop.Monto;
|
|
|
|
v.Id = (con.Ventas.Any()?con.Ventas.Count():0)+1;
|
|
c.Idventa = v.Id;
|
|
|
|
con.Ventas.Add(v);
|
|
con.Contratos.Add(c);
|
|
con.Notificaciones.Add(n);
|
|
|
|
return Guardar(con);
|
|
}
|
|
}
|