127 lines
4.9 KiB
C#
127 lines
4.9 KiB
C#
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([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 ) {
|
|
|
|
}
|
|
[HttpPost("/api/ventas/propietarioverifica")]
|
|
public IActionResult PropietarioVerifica(long idventa) {
|
|
|
|
}
|
|
|
|
[HttpGet("/api/venta")]
|
|
public IActionResult ObtenerVenta(long idventa) {
|
|
|
|
}
|
|
|
|
[HttpGet("/api/ventas")]
|
|
public IActionResult ObtenerVenta(long idventa) {
|
|
|
|
}
|
|
|
|
*/
|
|
[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});
|
|
}
|
|
} |