From 2b481e2ae2fb07e82938f03d93b5fd482c54a30c Mon Sep 17 00:00:00 2001 From: fede Date: Fri, 24 Jan 2025 21:31:37 -0300 Subject: [PATCH] falta interfaz venta --- .../DtoBuilder/OpcionVentaDtoBuilder.cs | 25 ++++ Aspnet/Builder/PrecontratoBuilder.cs | 5 + Aspnet/Controllers/ContratoController.cs | 15 ++- Aspnet/Controllers/VentaController.cs | 101 +++++++++++++- Entidades/Dto/OpcionVentaDto.cs | 8 ++ Entidades/Dto/PrecontratoDto.cs | 2 + Front/src/Componentes/ModalPrecontrato.svelte | 21 ++- Front/src/paginas/ContratoInquilino.svelte | 124 +++++++++++++++++- Front/src/paginas/ContratosPropietario.svelte | 93 ++++++++++++- Front/src/paginas/Notificaciones.svelte | 7 +- Front/src/types.d.ts | 8 ++ Modelo/RepositorioContratos.cs | 37 +++++- Modelo/RepositorioVentas.cs | 26 ++++ 13 files changed, 457 insertions(+), 15 deletions(-) create mode 100644 Aspnet/Builder/DtoBuilder/OpcionVentaDtoBuilder.cs create mode 100644 Entidades/Dto/OpcionVentaDto.cs create mode 100644 Modelo/RepositorioVentas.cs diff --git a/Aspnet/Builder/DtoBuilder/OpcionVentaDtoBuilder.cs b/Aspnet/Builder/DtoBuilder/OpcionVentaDtoBuilder.cs new file mode 100644 index 0000000..8df2256 --- /dev/null +++ b/Aspnet/Builder/DtoBuilder/OpcionVentaDtoBuilder.cs @@ -0,0 +1,25 @@ +using Entidades.Dto; + +namespace AlquilaFacil.Builder; +public class OpcionVentaDtoBuilder: Builder{ + public OpcionVentaDtoBuilder SetId(long id) { + data.Id = id; + return this; + } + public OpcionVentaDtoBuilder SetMonto(decimal monto) { + data.Monto = monto; + return this; + } + public OpcionVentaDtoBuilder SetDivisa(string divisa) { + data.Divisa = divisa; + return this; + } + public OpcionVentaDtoBuilder SetEnOrden(bool v) { + data.EnOrden = v; + return this; + } + public OpcionVentaDtoBuilder SetFueEjercido(int idestado) { + data.FueEjercido = idestado==1?false:true; + return this; + } +} \ No newline at end of file diff --git a/Aspnet/Builder/PrecontratoBuilder.cs b/Aspnet/Builder/PrecontratoBuilder.cs index 5e58b57..5d20313 100644 --- a/Aspnet/Builder/PrecontratoBuilder.cs +++ b/Aspnet/Builder/PrecontratoBuilder.cs @@ -47,4 +47,9 @@ public class PrecontratoBuilder : Builder { data.MesesDurationContrato = mesesDuracionContrato; return this; } + + public PrecontratoBuilder SetOpcionVenta(bool tieneOpcionVenta){ + data.Tieneopcionventa = tieneOpcionVenta == false?0Lu:1Lu; + return this; + } } \ No newline at end of file diff --git a/Aspnet/Controllers/ContratoController.cs b/Aspnet/Controllers/ContratoController.cs index fa6c146..6c6e822 100644 --- a/Aspnet/Controllers/ContratoController.cs +++ b/Aspnet/Controllers/ContratoController.cs @@ -336,6 +336,7 @@ public class ContratoController: ControllerBase { .SetPropiedad(p.Id) .SetFecha(DateTime.Parse(dto.fechaprimernotificacion)) .SetMesesDuracion(dto.MesesDuracionContrato) + .SetOpcionVenta(dto.TieneOpcionVenta) .Build(); @@ -349,7 +350,18 @@ public class ContratoController: ControllerBase { .SetMensaje($"El propietario {propi.Nombre} {propi.Apellido} te requiere que carges informacion de {dto.CantidadGarantes} Garantes") .Build(); - var ret = RepositorioContratos.Singleton.CargaPrecontrato(precontrato, notificacion); + bool ret; + if (dto.TieneOpcionVenta==false){ + ret = RepositorioContratos.Singleton.CargaPrecontrato(precontrato, notificacion); + } else { + Venta v = new Venta{ + Idestado = 1, + Iddivisa = dto.iddivisa, + Monto = dto.MontoOpcion, + }; + ret = RepositorioContratos.Singleton.CargaPrecontratoOpcionVenta(precontrato, notificacion, v); + } + if (ret) { ret = RepositorioNotificaciones.Singleton.MarcarComoLeido(cli.Dni, DateTime.Parse(dto.fechaprimernotificacion)); } @@ -816,6 +828,7 @@ public class ContratoController: ControllerBase { if (dto.MesesHastaAumento <= 0) ret += "No puede tener 0 o menos meses hasta el aumento\n"; if (dto.MesesDuracionContrato <= 0) ret += "No puede tener 0 o menos meses de duracion\n"; if (dto.MesesDuracionContrato < dto.MesesHastaAumento) ret += "el tiempo hasta aumento no puede ser mayor de \n"; + if (dto.TieneOpcionVenta == true && dto.MontoOpcion <=0) ret +="No puede tener un monto de venta negativo o 0"; return ret; } diff --git a/Aspnet/Controllers/VentaController.cs b/Aspnet/Controllers/VentaController.cs index 443d1d6..2fa2822 100644 --- a/Aspnet/Controllers/VentaController.cs +++ b/Aspnet/Controllers/VentaController.cs @@ -1,12 +1,42 @@ +using AlquilaFacil.Builder; +using Entidades; +using Entidades.Dto; using Microsoft.AspNetCore.Mvc; +using Modelo; namespace AlquilaFacil.Controllers; [ApiController] public class VentaController:ControllerBase { - [HttpPost("/api/ventas/ejercerOpcionVenta")] - public IActionResult EjercerOpcionVenta(long idcontrato) { + [HttpPost("/api/ventas/ejercerOpcionVenta")] + public IActionResult EjercerOpcionVenta([FromHeader(Name="Auth")]string Auth, [FromQuery]long idcontrato=0) { + var validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Inquilino"); + if (validacion1 == false) { + return Unauthorized(); + } + if (idcontrato <= 0) return BadRequest(new { message = "No pueden hacer cotratos con id 0 o menor"}); + + Cliente? cli = RepositorioUsuarios.Singleton.ObtenerClientePorToken(Auth); + if (cli == null) return Unauthorized(); + + Contrato? cont = RepositorioVentas.Singleton.ObtenerVentaPorContrato(idcontrato); + if (cont == null || cont.IdventaNavigation == null) return BadRequest(new { message = "no hay un contrato por esa id"}); + if (cont.Tieneopcionventa == 0) return BadRequest(new { message = "No tiene opcion de venta"}); + if (puedeEjercer(cont) == false) return BadRequest(new { message = "No cumple con los requisitos para ejercer la opcion de compra"}); + + Venta venta = cont.IdventaNavigation; + venta.IdVendedor = cont.Dnipropietario; + venta.IdComprador = cont.Dniinquilino; + venta.Idpropiedad = cont.Idpropiedad; + venta.Fechainicio = DateTime.Now; + + bool ret = RepositorioVentas.Singleton.PatchVenta(venta); + + return ret? + Ok(new { message = "Se ejercio la opcion de venta"}): + BadRequest(new { message = "No se pude ejercer la opcion de venta"}); } + /* [HttpPost("/api/ventas/subirReciboPago")] public IActionResult SubirRecibo([FromForm]IFormFile file, long idventa ) { @@ -27,8 +57,71 @@ public class VentaController:ControllerBase { } - [HttpGet("/api/contrato/tieneopcionventa")] - public IActionResult TieneOpcionVenta(long idcontrato) { + */ + [HttpGet("/api/opcionventa")] + public IActionResult ObtenerDto([FromHeader(Name="Auth")]string Auth, long idcontrato=0) { + var validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Propietario"); + if (validacion1 == false){ + validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Inquilino"); + if (validacion1 == false) { + return Unauthorized(); + } + } + if (idcontrato == 0) return BadRequest(new { message = "No existen contatos validos para la id 0"}); + Cliente? cli = RepositorioUsuarios.Singleton.ObtenerClientePorToken(Auth); + if (cli == null) return Unauthorized(); + + Contrato? cont = RepositorioVentas.Singleton.ObtenerVentaPorContrato(idcontrato); + if (cont == null) return BadRequest(new { message = "No hay un contrato por esa id"}); + + var dto = new OpcionVentaDtoBuilder() + .SetId(cont.Idventa??0) + .SetMonto(cont.IdventaNavigation.Monto) + .SetDivisa(cont.IdventaNavigation.IddivisaNavigation.Signo) + .SetEnOrden(puedeEjercer(cont)) + .SetFueEjercido(cont.IdventaNavigation.Idestado??0) + .Build(); + + return Ok(dto); + } + + private bool puedeEjercer(Contrato c) { + bool ret = c.Idcanons.All(x => x.Pagado == 1); + + if (ret) { + var canonConFechaMasTardia = c.Idcanons.OrderByDescending(x => x.Fecha).FirstOrDefault(); + + if (canonConFechaMasTardia != null && canonConFechaMasTardia.Fecha.Year >= DateTime.Now.Year + && canonConFechaMasTardia.Fecha.Month >= DateTime.Now.Month) { + ret = true; + }else{ + ret = false; + } + } + + return ret; + } + + [HttpGet("/api/contrato/tieneopcionventa")] + public IActionResult TieneOpcionVenta([FromHeader(Name="Auth")]string Auth, long idcontrato=0) { + var validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Propietario"); + if (validacion1 == false){ + validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Inquilino"); + if (validacion1 == false) { + return Unauthorized(); + } + } + + if (idcontrato == 0) return BadRequest(new { message = "No existen contatos validos para la id 0"}); + + Cliente? cli = RepositorioUsuarios.Singleton.ObtenerClientePorToken(Auth); + if (cli == null) return Unauthorized(); + + Contrato? cont = RepositorioContratos.Singleton.ObtenerContratoPorId(idcontrato); + if (cont == null) return BadRequest(new { message = "No hay un contrato por esa id"}); + if (cont.Dniinquilino !=cli.Dni && cont.Dnipropietario != cli.Dni) return Unauthorized(); + + return Ok( new { message = cont.Tieneopcionventa}); } } \ No newline at end of file diff --git a/Entidades/Dto/OpcionVentaDto.cs b/Entidades/Dto/OpcionVentaDto.cs new file mode 100644 index 0000000..3f11e1d --- /dev/null +++ b/Entidades/Dto/OpcionVentaDto.cs @@ -0,0 +1,8 @@ +namespace Entidades.Dto; +public class OpcionVentaDto{ + public long Id { get; set;} + public decimal Monto { get; set;} + public string Divisa { get; set;} =""; + public bool EnOrden { get; set;} + public bool FueEjercido { get; set; } +} \ No newline at end of file diff --git a/Entidades/Dto/PrecontratoDto.cs b/Entidades/Dto/PrecontratoDto.cs index 650e075..d1a76fa 100644 --- a/Entidades/Dto/PrecontratoDto.cs +++ b/Entidades/Dto/PrecontratoDto.cs @@ -8,4 +8,6 @@ public class PrecontratoDto { public bool TieneOpcionVenta { get; set; } public string fechaprimernotificacion { get; set; } = ""; public int MesesDuracionContrato { get; set; } + public Decimal MontoOpcion {get; set; } + public int iddivisa { get; set; } } \ No newline at end of file diff --git a/Front/src/Componentes/ModalPrecontrato.svelte b/Front/src/Componentes/ModalPrecontrato.svelte index 6f04d38..a44bdf9 100644 --- a/Front/src/Componentes/ModalPrecontrato.svelte +++ b/Front/src/Componentes/ModalPrecontrato.svelte @@ -1,16 +1,19 @@ @@ -25,11 +28,23 @@