From 9d52a212fa3a74a10978e4ceff51487a66dbd43a Mon Sep 17 00:00:00 2001 From: Dennis Eckerskorn Date: Wed, 14 May 2025 22:08:22 +0200 Subject: [PATCH] Added invoicelist and some dto changes --- .../finance_management_dtos/InvoiceDTO.java | 13 ++ .../dtos/user_managment_dtos/UserDTO.java | 13 ++ .../src/components/layout/MainLayout.jsx | 4 +- .../src/components/lists/InvoiceList.jsx | 112 ++++++++++++++++++ 4 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 memberflow-frontend/src/components/lists/InvoiceList.jsx diff --git a/memberflow-api/src/main/java/com/denniseckerskorn/dtos/finance_management_dtos/InvoiceDTO.java b/memberflow-api/src/main/java/com/denniseckerskorn/dtos/finance_management_dtos/InvoiceDTO.java index 0edf1bb..b3410ef 100644 --- a/memberflow-api/src/main/java/com/denniseckerskorn/dtos/finance_management_dtos/InvoiceDTO.java +++ b/memberflow-api/src/main/java/com/denniseckerskorn/dtos/finance_management_dtos/InvoiceDTO.java @@ -1,5 +1,6 @@ package com.denniseckerskorn.dtos.finance_management_dtos; +import com.denniseckerskorn.dtos.user_managment_dtos.UserDTO; import com.denniseckerskorn.entities.finance.Invoice; import com.denniseckerskorn.enums.StatusValues; import jakarta.validation.constraints.NotNull; @@ -16,6 +17,9 @@ public class InvoiceDTO { @NotNull private Integer userId; + private UserDTO user; + + @NotNull private LocalDateTime date; @@ -35,6 +39,7 @@ public class InvoiceDTO { public InvoiceDTO(Invoice invoice) { this.id = invoice.getId(); this.userId = invoice.getUser() != null ? invoice.getUser().getId() : null; + this.user = invoice.getUser() != null ? new UserDTO(invoice.getUser()) : null; this.date = invoice.getDate(); this.total = invoice.getTotal(); this.status = invoice.getStatus(); @@ -77,6 +82,14 @@ public class InvoiceDTO { this.userId = userId; } + public UserDTO getUser() { + return user; + } + + public void setUser(UserDTO user) { + this.user = user; + } + public LocalDateTime getDate() { return date; } diff --git a/memberflow-api/src/main/java/com/denniseckerskorn/dtos/user_managment_dtos/UserDTO.java b/memberflow-api/src/main/java/com/denniseckerskorn/dtos/user_managment_dtos/UserDTO.java index f58c83c..0e7473d 100644 --- a/memberflow-api/src/main/java/com/denniseckerskorn/dtos/user_managment_dtos/UserDTO.java +++ b/memberflow-api/src/main/java/com/denniseckerskorn/dtos/user_managment_dtos/UserDTO.java @@ -42,6 +42,19 @@ public class UserDTO { this.status = status; } + public UserDTO(User user) { + this.id = user.getId(); + this.name = user.getName(); + this.surname = user.getSurname(); + this.email = user.getEmail(); + this.phoneNumber = user.getPhoneNumber(); + this.address = user.getAddress(); + this.registerDate = user.getRegisterDate(); + this.roleName = user.getRole() != null ? user.getRole().getName() : null; + this.status = user.getStatus(); + } + + // Getters y Setters public Integer getId() { return id; diff --git a/memberflow-frontend/src/components/layout/MainLayout.jsx b/memberflow-frontend/src/components/layout/MainLayout.jsx index 04aaf23..feda04b 100644 --- a/memberflow-frontend/src/components/layout/MainLayout.jsx +++ b/memberflow-frontend/src/components/layout/MainLayout.jsx @@ -23,6 +23,7 @@ import ViewTimetable from '../forms/ViewTimetable'; import MembershipForm from '../forms/MembershipCreateForm'; import MembershipList from '../lists/MembershipList'; import InvoiceForm from '../forms/InvoiceForm'; +import InvoiceList from '../lists/InvoiceList'; import '../styles/MainLayout.css'; @@ -66,9 +67,8 @@ const MainLayout = () => { } /> } /> - - } /> + } /> {/* Profile Page*/} } /> diff --git a/memberflow-frontend/src/components/lists/InvoiceList.jsx b/memberflow-frontend/src/components/lists/InvoiceList.jsx new file mode 100644 index 0000000..d8a1bf1 --- /dev/null +++ b/memberflow-frontend/src/components/lists/InvoiceList.jsx @@ -0,0 +1,112 @@ +import React, { useEffect, useState } from "react"; +import api from "../../api/axiosConfig"; +import ErrorMessage from "../common/ErrorMessage"; +import "../styles/ContentArea.css"; + +const InvoiceList = () => { + const [invoices, setInvoices] = useState([]); + const [loading, setLoading] = useState(true); + const [errorMsg, setErrorMsg] = useState(""); + const [successMsg, setSuccessMsg] = useState(""); + + useEffect(() => { + fetchInvoices(); + }, []); + + const fetchInvoices = async () => { + try { + const res = await api.get("/invoices/getAll"); + setInvoices(res.data); + setErrorMsg(""); + } catch (err) { + console.error(err); + setErrorMsg("❌ Error al cargar las facturas."); + } finally { + setLoading(false); + } + }; + + const handleDelete = async (id) => { + if (!window.confirm("¿Estás seguro de que deseas eliminar esta factura?")) return; + + try { + await api.delete(`/invoices/deleteById/${id}`); + setInvoices(invoices.filter((inv) => inv.id !== id)); + setSuccessMsg("✅ Factura eliminada correctamente."); + setErrorMsg(""); + } catch (err) { + console.error(err); + setErrorMsg("❌ Error al eliminar la factura."); + setSuccessMsg(""); + } + }; + + const downloadPdf = async (id) => { + try { + const res = await api.get(`/invoices/generatePDFById/${id}`, { + responseType: "blob", + }); + + const blob = new Blob([res.data], { type: "application/pdf" }); + const url = window.URL.createObjectURL(blob); + const a = document.createElement("a"); + a.href = url; + a.download = `factura_${id}.pdf`; + a.click(); + window.URL.revokeObjectURL(url); + } catch (err) { + console.error(err); + setErrorMsg("❌ Error al descargar el PDF."); + } + }; + + if (loading) { + return ( +
+

Cargando facturas...

+
+ ); + } + + return ( +
+

Listado de Facturas

+ + + +
+ + + + + + + + + + + + + + {invoices.map((inv) => ( + + + + + + + + + + ))} + +
IDClienteEmailFechaEstadoTotal (€)Acciones
{inv.id}{inv.user?.name} {inv.user?.surname}{inv.user?.email}{new Date(inv.date).toLocaleString()}{inv.status}{inv.total?.toFixed(2)} + + +
+
+
+ ); +}; + +export default InvoiceList;