From d0a24ce0b74336b795a260aee1bffefbe86cf55f Mon Sep 17 00:00:00 2001 From: fede Date: Mon, 27 Jan 2025 03:37:03 -0300 Subject: [PATCH] bueno parece que termine lo de ventas bruh --- Aspnet/Controllers/VentaController.cs | 149 +++++++++- Front/src/paginas/ContratoInquilino.svelte | 4 +- Front/src/paginas/ContratosPropietario.svelte | 4 +- Front/src/paginas/Notificaciones.svelte | 2 +- Front/src/paginas/Ventas.svelte | 258 +++++++++++++++++- Modelo/RepositorioVentas.cs | 21 ++ 6 files changed, 428 insertions(+), 10 deletions(-) diff --git a/Aspnet/Controllers/VentaController.cs b/Aspnet/Controllers/VentaController.cs index 9236327..b9857dd 100644 --- a/Aspnet/Controllers/VentaController.cs +++ b/Aspnet/Controllers/VentaController.cs @@ -1,7 +1,13 @@ +using System.Configuration; +using System.Formats.Asn1; +using System.Text.Json; using AlquilaFacil.Builder; +using AlquilaFacil.Config; using Entidades; using Entidades.Dto; using Microsoft.AspNetCore.Mvc; +using Minio; +using Minio.DataModel.Args; using Modelo; namespace AlquilaFacil.Controllers; @@ -36,18 +42,148 @@ public class VentaController:ControllerBase { 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 ) { + public async Task SubirRecibo([FromHeader(Name="Auth")]string Auth, [FromForm]IFormFile file, long idventa ) { + if (String.IsNullOrWhiteSpace(Auth)) return Unauthorized(); + var validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Propietario"); + if (validacion1 == false){ + validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Inquilino"); + if (validacion1 == false) { + return Unauthorized(); + } + } + + if (idventa <=0) return BadRequest(new { message = "Las id 0 o menor no son validas" }); + + Cliente? cli = RepositorioUsuarios.Singleton.ObtenerClientePorToken(Auth); + if (cli == null) return Unauthorized(); + + Venta? venta = RepositorioVentas.Singleton.ObtenerVentaPorId(idventa); + if (venta == null) return BadRequest(new { message = "no hay una venta por esa id"}); + + if (cli.Dni !=venta.IdComprador && cli.Dni != venta.IdVendedor) return Unauthorized(); + + if (file == null) return BadRequest(new { message = "Debe subir un archivo." }); + if (file.ContentType != "application/pdf") return BadRequest(new { message = "El archivo debe ser un documento PDF." }); + if (!Path.GetExtension(file.FileName).Equals(".pdf", StringComparison.OrdinalIgnoreCase)) return BadRequest(new { message = "El archivo debe tener la extensión .pdf." }); + + string nuevoNombreArchivo = $"id:{venta.Id}-comprador:{venta.IdComprador}-vendedor:{venta.IdVendedor}-idprop:{venta.Idpropiedad}.pdf"; + bool ret = await subirContrato(file, nuevoNombreArchivo); + if(ret == false) return BadRequest(new {message = "No se pudo subir el archivo"}); + + ret = RepositorioVentas.Singleton.SetUrlRecibo(venta.Id, nuevoNombreArchivo); + if (ret == false) return BadRequest(new { message = "no se pudo guardar el nombre del archivo subido"}); + + return Ok(new { message = "Se Subio el Recibo"}); } + private readonly IMinioClient mc; + public VentaController(IMinioClient minioClient) { + mc = minioClient; + if (mc == null){ + MinioConfigcus? mcon = JsonSerializer.Deserialize(System.IO.File.ReadAllText("./settings.json"))?? null; + if (mcon == null) throw new Exception(); + + mc = new MinioClient().WithCredentials(mcon.usr, mcon.scrt) + .WithEndpoint("192.168.1.11:9000") + .WithSSL(false) + .Build(); + } + } + private async Task subirContrato(IFormFile f, string flname) { + try { + var buck = new BucketExistsArgs().WithBucket("alquilafacil"); + bool encontrado = await mc.BucketExistsAsync(buck).ConfigureAwait(false); + + if(!encontrado){ + var mb = new MakeBucketArgs().WithBucket("alquilafacil"); + await mc.MakeBucketAsync(mb).ConfigureAwait(false); + } + using (var stream = new MemoryStream()){ + await f.CopyToAsync(stream); + stream.Position=0; + PutObjectArgs putbj = new PutObjectArgs() + .WithBucket("alquilafacil") + .WithObject(flname) + .WithStreamData(stream) + .WithContentType("application/pdf") + .WithObjectSize(stream.Length); + await mc.PutObjectAsync(putbj); + } + return true; + } catch (Exception e ) { + Console.Error.WriteLine(e.Message); + return false; + } + } + + [HttpGet("/api/ventas/verRecibo")] + public IActionResult verRecibo([FromHeader(Name="Auth")]string Auth, long idventa=0){ + var validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Propietario"); + if (validacion1 == false){ + validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Inquilino"); + if (validacion1 == false) { + return Unauthorized(); + } + } + if (idventa <= 0) return BadRequest(new { message = "No existen ventas validas para la id 0"}); + + Cliente? cli = RepositorioUsuarios.Singleton.ObtenerClientePorToken(Auth); + if (cli == null) return Unauthorized(); + + Venta? venta = RepositorioVentas.Singleton.ObtenerVentaPorId(idventa); + if (venta == null) return BadRequest(new { message = "no hay una venta con esa id"}); + if (cli.Dni != venta.IdComprador && cli.Dni != venta.IdVendedor) return Unauthorized(); + + try{ + var memstream = new MemoryStream(); + + var goa = new GetObjectArgs() + .WithBucket("alquilafacil") + .WithObject(venta.UrlRecibo) + .WithCallbackStream(stream => { + memstream.Position=0; + stream.CopyTo(memstream); + }); + + mc.GetObjectAsync(goa).Wait(); + memstream.Position = 0; + + if (memstream.Length == 0) return BadRequest(new { message = "El archivo está vacío" }); + + return File(memstream, "application/pdf", venta.UrlRecibo); + + } catch (Exception e){ + Console.Error.WriteLine(e); + return BadRequest(new { message = "Fallo al intentar obtener el archivo del almacenamiento o este no existe"}); + } + } + + [HttpPost("/api/ventas/propietarioverifica")] - public IActionResult PropietarioVerifica(long idventa) { + public IActionResult PropietarioVerifica([FromHeader(Name="Auth")]string Auth, long idventa=0) { + var validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Propietario"); + if (validacion1 == false){ + validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Inquilino"); + if (validacion1 == false) { + return Unauthorized(); + } + } + if (idventa <= 0) return BadRequest(new { message = "No existen ventas validas para la id 0"}); + Cliente? cli = RepositorioUsuarios.Singleton.ObtenerClientePorToken(Auth); + if (cli == null) return Unauthorized(); + + var ventas = RepositorioVentas.Singleton.ObtenerVentaPorId(idventa); + if (ventas == null) return BadRequest(new { message ="No hay una venta con ese id"}); + if (ventas.IdVendedor != cli.Dni) return Unauthorized(); + + bool ret = RepositorioVentas.Singleton.EfectuarVenta(idventa); + return ret ? Ok(new { message = "Se traspaso la propiedad"}): BadRequest(new { message = ""}); } - */ + [HttpGet("/api/venta")] public IActionResult ObtenerVenta([FromHeader(Name="Auth")]string Auth, long idventa=0) { var validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Propietario"); @@ -78,11 +214,12 @@ public class VentaController:ControllerBase { .SetEstado(ventas.IdestadoNavigation.Descripcion??"") .Build(); - return Ok(new { data = v, iscomprador = (ventas.IdComprador==cli.Dni)?true:false}); + return Ok(new { data = v, iscomprador = (ventas.IdComprador==cli.Dni)?true:false, + necesitaRecibo = ventas.UrlRecibo==null?true:false}); } [HttpGet("/api/ventas")] - public IActionResult ObtenerVenta([FromHeader(Name="Auth")]string Auth) { + public IActionResult ObtenerVentas([FromHeader(Name="Auth")]string Auth) { var validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Propietario"); if (validacion1 == false){ validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Inquilino"); diff --git a/Front/src/paginas/ContratoInquilino.svelte b/Front/src/paginas/ContratoInquilino.svelte index 372c24c..fbd5438 100644 --- a/Front/src/paginas/ContratoInquilino.svelte +++ b/Front/src/paginas/ContratoInquilino.svelte @@ -53,7 +53,9 @@ if (r.ok){ let data = await r.json(); TieneOpcionVenta = data.message; - ObtenerOpcionVentaDto(); + if (TieneOpcionVenta){ + ObtenerOpcionVentaDto(); + } return; } let data = await r.json(); diff --git a/Front/src/paginas/ContratosPropietario.svelte b/Front/src/paginas/ContratosPropietario.svelte index 349e17a..db9e19e 100644 --- a/Front/src/paginas/ContratosPropietario.svelte +++ b/Front/src/paginas/ContratosPropietario.svelte @@ -61,7 +61,9 @@ if (r.ok){ let data = await r.json(); TieneOpcionVenta = data.message; - ObtenerOpcionVentaDto(); + if (TieneOpcionVenta){ + ObtenerOpcionVentaDto(); + } return; } let data = await r.json(); diff --git a/Front/src/paginas/Notificaciones.svelte b/Front/src/paginas/Notificaciones.svelte index fb3d552..0b4915a 100644 --- a/Front/src/paginas/Notificaciones.svelte +++ b/Front/src/paginas/Notificaciones.svelte @@ -434,7 +434,7 @@ !!(Selmens.accion = "") }/> {/if} -
+


diff --git a/Front/src/paginas/Ventas.svelte b/Front/src/paginas/Ventas.svelte index e260581..3cd5d8c 100644 --- a/Front/src/paginas/Ventas.svelte +++ b/Front/src/paginas/Ventas.svelte @@ -1,7 +1,263 @@ - \ No newline at end of file + + +{#if modaldata} + !!(modaldata ="")} /> +{/if} + +
+
+
+
+ Datos Venta +
+
+ Monto: {venta.divisa} {venta.monto}
+ Ubicación: {venta.ubicacion}
+ Vendedor: {venta.nombreVendedor}
+ Comprador: {venta.nombreComprador}
+ Estado: {venta.estado}
+
+ +
+
+
+
+ {#if escomprador} +
+

+ +

+
+
+
+
+ Suba su Comprobante de pago +
+
+
+ + +
+ +
+
+ +
+
+
+
+
+ {:else} +
+

+ +

+
+
+
+
+ Vea el comprobante de pago +
+
+ {#if necesitaRecibo} +

El boton estará habilitado cuando el comprador suba el recibo

+ {/if} + +
+
+
+
+
+ {#if !necesitaRecibo} + +
+

+ +

+
+
+
+
+ +
+ + + {#if checkAceptaRecibo} +
+
+
+ +
+
+

Al darle al botón se va a iniciar el proceso de traspaso de la propiedad

+
+
+ + {/if} +
+
+
+
+
+ {/if} + {/if} +
+
+
\ No newline at end of file diff --git a/Modelo/RepositorioVentas.cs b/Modelo/RepositorioVentas.cs index 3ce7356..df962af 100644 --- a/Modelo/RepositorioVentas.cs +++ b/Modelo/RepositorioVentas.cs @@ -3,6 +3,19 @@ using Microsoft.EntityFrameworkCore; namespace Modelo; public class RepositorioVentas: RepositorioBase { + public bool EfectuarVenta(long idventa) { + var con = Context; + var vent = con.Ventas.Include(x=>x.IdpropiedadNavigation).FirstOrDefault(x => x.Id == idventa); + if (vent == null||vent.Idestado != 2) return false; + + vent.IdpropiedadNavigation.Dnipropietario = vent.IdComprador; + + vent.IdpropiedadNavigation.Idestado=3; + vent.Idestado=3; + + return Guardar(con); + } + public Contrato? ObtenerVentaPorContrato(long idcontrato) { var con = Context; var c = con.Contratos.Include(x=>x.Idcanons).Include(x=>x.IdventaNavigation).ThenInclude(x=>x.IddivisaNavigation) @@ -50,4 +63,12 @@ public class RepositorioVentas: RepositorioBase { return Guardar(con); } + + public bool SetUrlRecibo(long id, string nuevoNombreArchivo) { + var con = Context; + var venta = con.Ventas.FirstOrDefault(x=>x.Id == id); + if (venta==null) return false; + venta.UrlRecibo = nuevoNombreArchivo; + return Guardar(con); + } } \ No newline at end of file