Cobros recurrentes: panel de cliente, comprobantes verificados y notificaciones
- Panel de cliente con acceso propio (role=cliente): resumen, cotizaciones con descarga PDF y guia de pagos recurrentes con subida de comprobante (imagen->WebP o PDF). - Módulo admin /cobros: programas de pago (semanal/quincenal/mensual), generación idempotente manual y automatica cada hora (scheduler en instrumentation, zona America/Mexico_City), revisión de comprobantes con aprobar/rechazar (el rechazo borra el archivo del servidor y permite re-subir). - Cuentas de cliente creadas/rotadas/revocadas desde la página de clientes. - Notificaciones: servicio SMTP configurable por env (se omite con aviso si no hay credenciales) y Web Push con VAPID + service worker. - Separación de roles en middleware (cliente solo ve /cliente, staff no entra al panel de cliente; PDF y comprobantes son rutas compartidas con validación de pertenencia). - Producción: volumen uploads_data en ambos compose, env vars documentadas en .env.example, migración 20261020000000.
This commit is contained in:
@@ -0,0 +1,154 @@
|
||||
import Link from "next/link";
|
||||
import { redirect } from "next/navigation";
|
||||
import { ArrowRight, FileText } from "lucide-react";
|
||||
import { requireRole } from "@/lib/auth";
|
||||
import { prisma } from "@/lib/db";
|
||||
import {
|
||||
IVA_RATE,
|
||||
calcularTotalesCotizacion,
|
||||
calcularTotalesOpcion,
|
||||
formatCurrency,
|
||||
formatDate,
|
||||
} from "@/lib/calculators";
|
||||
import { EstadoBadge } from "@/components/EstadoBadge";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
// Lista de cotizaciones del cliente: numero, proyecto, fecha, estado y totales.
|
||||
export default async function ClienteCotizacionesPage() {
|
||||
const session = await requireRole("cliente");
|
||||
if (!session) redirect("/login?panel=cliente");
|
||||
if (!session.clienteId) redirect("/login?panel=cliente");
|
||||
|
||||
const cotizaciones = await prisma.cotizacion.findMany({
|
||||
where: { clienteId: session.clienteId },
|
||||
orderBy: { fecha: "desc" },
|
||||
select: {
|
||||
id: true,
|
||||
numero: true,
|
||||
proyecto: true,
|
||||
fecha: true,
|
||||
estado: true,
|
||||
esDoble: true,
|
||||
incluirIva: true,
|
||||
servicios: { select: { tipoPago: true, precio: true, seleccionado: true, opcion: true } },
|
||||
},
|
||||
});
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div className="mb-8">
|
||||
<h1 className="text-2xl font-bold">Mis cotizaciones</h1>
|
||||
<p className="text-muted text-sm mt-1">
|
||||
{cotizaciones.length === 0
|
||||
? "Aún no tienes cotizaciones"
|
||||
: `${cotizaciones.length} cotizacion${cotizaciones.length === 1 ? "" : "es"} a tu nombre`}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{cotizaciones.length === 0 ? (
|
||||
<div className="bg-card-bg rounded-xl border border-border p-12 text-center">
|
||||
<FileText className="w-16 h-16 text-muted mx-auto mb-4" />
|
||||
<h2 className="text-lg font-semibold mb-2">No hay cotizaciones todavía</h2>
|
||||
<p className="text-muted">
|
||||
Cuando tengas una cotización disponible aparecerá en esta sección.
|
||||
</p>
|
||||
</div>
|
||||
) : (
|
||||
<div className="bg-card-bg rounded-xl border border-border overflow-hidden">
|
||||
<table className="w-full text-sm">
|
||||
<thead>
|
||||
<tr className="border-b border-border bg-gray-50">
|
||||
<th className="text-left px-5 py-3 font-medium text-muted">No. Cotizacion</th>
|
||||
<th className="text-left px-5 py-3 font-medium text-muted">Proyecto</th>
|
||||
<th className="text-left px-5 py-3 font-medium text-muted">Fecha</th>
|
||||
<th className="text-left px-5 py-3 font-medium text-muted">Estado</th>
|
||||
<th className="text-right px-5 py-3 font-medium text-muted">Total Unico</th>
|
||||
<th className="text-right px-5 py-3 font-medium text-muted">Total Mensual</th>
|
||||
<th className="text-right px-5 py-3 font-medium text-muted"> </th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{cotizaciones.map((c) => {
|
||||
const totales = calcularTotalesCotizacion(c.servicios, {
|
||||
incluirIva: c.incluirIva,
|
||||
});
|
||||
const conIva = (m: number) =>
|
||||
c.incluirIva ? Math.round(m * (1 + IVA_RATE) * 100) / 100 : m;
|
||||
return (
|
||||
<tr key={c.id} className="border-b border-border hover:bg-gray-50 transition-colors">
|
||||
<td className="px-5 py-3">
|
||||
<Link
|
||||
href={`/cliente/cotizaciones/${c.id}`}
|
||||
className="font-medium text-primary hover:underline"
|
||||
>
|
||||
{c.numero}
|
||||
</Link>
|
||||
{c.esDoble && (
|
||||
<span className="ml-2 text-[11px] font-semibold uppercase tracking-wide px-2 py-0.5 rounded-full bg-primary-light text-primary">
|
||||
Doble propuesta
|
||||
</span>
|
||||
)}
|
||||
</td>
|
||||
<td className="px-5 py-3 text-muted">{c.proyecto}</td>
|
||||
<td className="px-5 py-3 text-muted">{formatDate(c.fecha)}</td>
|
||||
<td className="px-5 py-3">
|
||||
<EstadoBadge estado={c.estado} />
|
||||
</td>
|
||||
<td className="px-5 py-3 text-right font-medium">
|
||||
{c.esDoble ? (
|
||||
<div className="space-y-0.5">
|
||||
{(["1", "2"] as const).map((op) => {
|
||||
const t = calcularTotalesOpcion(c.servicios, op);
|
||||
return (
|
||||
<p key={op} className="text-xs">
|
||||
<span className="text-muted">Op {op}:</span>{" "}
|
||||
{t.totalUnico > 0 ? formatCurrency(conIva(t.totalUnico)) : "-"}
|
||||
</p>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
) : totales.totalUnico > 0 ? (
|
||||
formatCurrency(totales.totalUnico)
|
||||
) : (
|
||||
"-"
|
||||
)}
|
||||
</td>
|
||||
<td className="px-5 py-3 text-right font-medium">
|
||||
{c.esDoble ? (
|
||||
<div className="space-y-0.5">
|
||||
{(["1", "2"] as const).map((op) => {
|
||||
const t = calcularTotalesOpcion(c.servicios, op);
|
||||
return (
|
||||
<p key={op} className="text-xs">
|
||||
<span className="text-muted">Op {op}:</span>{" "}
|
||||
{t.totalMensual > 0 ? formatCurrency(conIva(t.totalMensual)) : "-"}
|
||||
</p>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
) : totales.totalMensual > 0 ? (
|
||||
formatCurrency(totales.totalMensual)
|
||||
) : (
|
||||
"-"
|
||||
)}
|
||||
</td>
|
||||
<td className="px-5 py-3 text-right">
|
||||
<Link
|
||||
href={`/cliente/cotizaciones/${c.id}`}
|
||||
className="text-xs text-primary hover:underline inline-flex items-center gap-1"
|
||||
>
|
||||
Ver
|
||||
<ArrowRight className="w-3 h-3" />
|
||||
</Link>
|
||||
</td>
|
||||
</tr>
|
||||
);
|
||||
})}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user