91 lines
2.8 KiB
C#
91 lines
2.8 KiB
C#
using Entidades;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Org.BouncyCastle.Math.EC.Rfc7748;
|
|
|
|
namespace Modelo;
|
|
|
|
public class RepositorioCanons: RepositorioBase<RepositorioCanons> {
|
|
public ICollection<Canon>? ObtenerCanonsPorContrato(int id) {
|
|
var con = Context;
|
|
var l = con.Contratos.Include(x=>x.Idcanons).FirstOrDefault(x => x.Id == id);
|
|
if (l == null) return null;
|
|
return l.Idcanons;
|
|
}
|
|
public Canon? ObtenerCanonContrato(DateTime f, long idcont) {
|
|
var con = Context;
|
|
Canon? cc = null;
|
|
|
|
var c = con.Contratos.Include(x=>x.Idcanons).ThenInclude(x=>x.IdreciboNavigation).FirstOrDefault(x => x.Id == idcont);
|
|
if (c == null) return null;
|
|
|
|
foreach (var j in c.Idcanons) {
|
|
if (j.Fecha == f) {
|
|
cc = j;
|
|
break;
|
|
}
|
|
}
|
|
return cc;
|
|
}
|
|
|
|
public bool SetRecibo(Canon c, Recibo re) {
|
|
var con = Context;
|
|
var cc = con.Canons
|
|
.Include(x=>x.Idcontratos)
|
|
.ThenInclude(x=>x.Idcanons)
|
|
.FirstOrDefault(x=>x.Id == c.Id);
|
|
if (cc == null) return false;
|
|
|
|
if (cc.Idcontratos.Count()!=1) return false;
|
|
Contrato ccc = cc.Idcontratos.First();
|
|
if (ccc.Cancelado == 1 || ccc.Habilitado == 0) return false;
|
|
con.Entry(ccc).Reference(x=>x.IdpropiedadNavigation).Load();
|
|
|
|
re.Id = (con.Recibos.Any()?con.Recibos.Max(x=>x.Id):0)+1;
|
|
con.Recibos.Add(re);
|
|
cc.Idrecibo = re.Id;
|
|
cc.Pagado = 1;
|
|
|
|
|
|
if (ccc.Idcanons.Where(x=>x.Pagado==1).Count() == ccc.MesesDurationContrato){
|
|
ccc.Cancelado = 1;
|
|
ccc.IdpropiedadNavigation.Idestado = 3;
|
|
}
|
|
|
|
return Guardar(con);
|
|
}
|
|
|
|
public bool CrearCanons(decimal aumento, long idcontrato) {
|
|
var con = Context;
|
|
|
|
aumento/=100;
|
|
aumento+=1;
|
|
|
|
var cont = con.Contratos.Include(x=>x.Idcanons).FirstOrDefault(x=>x.Id == idcontrato);
|
|
if (cont == null) return false;
|
|
|
|
int exist = cont.Idcanons.Count();
|
|
Canon? d = cont.Idcanons.OrderByDescending(x=>x.Fecha).FirstOrDefault();
|
|
if (d == null) return false;
|
|
|
|
if (exist == cont.MesesDurationContrato) return false;
|
|
if (exist+cont.MesesHastaAumento >= cont.MesesDurationContrato){
|
|
exist = cont.MesesDurationContrato-exist;
|
|
} else{
|
|
exist = cont.MesesHastaAumento;
|
|
}
|
|
|
|
cont.Monto = cont.Monto * aumento;
|
|
|
|
for (int i = 0; i < exist; i++){
|
|
Canon c = new Canon{
|
|
Fecha = d.Fecha.AddMonths(i==0?1:i+1),
|
|
Id = con.Canons.Count()+i+1,
|
|
Monto = cont.Monto,
|
|
Pagado = 0,
|
|
};
|
|
con.Canons.Add(c);
|
|
cont.Idcanons.Add(c);
|
|
}
|
|
return Guardar(con);
|
|
}
|
|
} |