Files
co-lite/solvway-admin-portal/app/components/events/EventCloseOutDialog.tsx
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session,
api-gateway, mobile-api, services-auth, services-system,
services-counter, services-member, services-log, services-email.
solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal
from tastefully 8bd93d15. Copied as tracked files only.

Literal secrets replaced with environment placeholders:
- services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY,
  MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default)
- services-system: JUMIO_TOKEN, JUMIO_SECRET (optional)
- services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed
- core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default
  removed, ipgeolocation key read from IPGEOLOCATION_API_KEY
- services-system EkycAppConfig: commented Jumio credentials removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 15:55:45 +08:00

146 lines
5.5 KiB
TypeScript

"use client";
import { useEffect, useState } from "react";
import Link from "next/link";
import { useRouter } from "next/navigation";
import { Button } from "@/app/components/ui/button";
import { Label } from "@/app/components/ui/label";
import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from "@/app/components/ui/dialog";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/app/components/ui/select";
import { ErrorState } from "@/app/components/ui/states";
import { closeEvent, getEventCloseOut, startEventCloseOut } from "@/app/lib/services/adminStockCounts";
import { listAdminWarehouses } from "@/app/lib/services/adminWarehouses";
import { useT } from "@/app/lib/i18n/MessagesProvider";
import type { AdminEvent, AdminWarehouse, EventCloseOutState } from "@/app/lib/types";
// Event close-out (add-stock-count-event-closeout): after the sales window, with no open pickups,
// count the event warehouse; once that count is approved, return every leftover to a warehouse and
// close the event. A Closed event's warehouse accepts no further movement. The server enforces each
// step; this dialog shows the current one.
const message = (e: unknown) => (e instanceof Error ? e.message : String(e));
export function EventCloseOutDialog({
event,
onClose,
onClosed,
}: {
event: AdminEvent;
onClose: () => void;
onClosed: () => void;
}) {
const t = useT();
const router = useRouter();
const [state, setState] = useState<EventCloseOutState | null>(null);
const [warehouses, setWarehouses] = useState<AdminWarehouse[]>([]);
const [returnId, setReturnId] = useState("");
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
Promise.all([getEventCloseOut(event.id), listAdminWarehouses()])
.then(([s, ws]) => {
setState(s);
setWarehouses(ws);
if (s.suggestedReturnWarehouseId !== null) setReturnId(String(s.suggestedReturnWarehouseId));
})
.catch(e => setError(message(e)));
}, [event.id]);
const returnOptions = warehouses.filter(w => w.status === "Y" && w.type !== "EVENT");
const codeOf = (id: number | null) => warehouses.find(w => w.id === id)?.code ?? String(id ?? "");
const start = async () => {
setBusy(true);
setError(null);
try {
const count = await startEventCloseOut(event.id);
router.push(`/inventory/stock-counts/${count.id}`);
} catch (e) {
setError(message(e));
setBusy(false);
}
};
const close = async () => {
setBusy(true);
setError(null);
try {
await closeEvent(event.id, Number(returnId));
onClosed();
} catch (e) {
setError(message(e));
setBusy(false);
}
};
const blocked = state !== null && (!state.windowsOver || state.openReservations.length > 0);
return (
<Dialog open onOpenChange={open => { if (!open) onClose(); }}>
<DialogContent>
<DialogHeader>
<DialogTitle>{t("EvtCloseOutTitle", { name: event.name })}</DialogTitle>
</DialogHeader>
{state?.phase === "NOT_STARTED" && (
<div className="space-y-3 text-sm">
<p>{t("EvtCloseOutIntro")}</p>
{!state.windowsOver && <p>{t("EvtCloseOutSalesNotOver")}</p>}
{state.openReservations.length > 0 && (
<div>
<p>{t("EvtCloseOutOpenOrders")}</p>
<ul className="mt-1 list-disc pl-5">
{state.openReservations.map(b => <li key={b.ref}>{b.orderNo ?? t("EvtCloseOutUnpaidOrder", { ref: b.ref })}</li>)}
</ul>
</div>
)}
</div>
)}
{(state?.phase === "COUNTING" || state?.phase === "AWAITING_APPROVAL") && (
<p className="text-sm">
{state.phase === "COUNTING" ? t("EvtCloseOutCounting") : t("EvtCloseOutAwaitingApproval")}{" "}
<Link className="underline underline-offset-4" href={`/inventory/stock-counts/${state.countId}`}>
{t("EvtCloseOutOpenCount", { id: String(state.countId) })}
</Link>
</p>
)}
{state?.phase === "READY_TO_CLOSE" && (
<div className="space-y-2 text-sm">
<p>{t("EvtCloseOutReady")}</p>
<Label htmlFor="close-out-return-warehouse">{t("EvtReturnWarehouse")}</Label>
<Select value={returnId} onValueChange={setReturnId}>
<SelectTrigger id="close-out-return-warehouse" aria-label={t("EvtReturnWarehouse")}>
<SelectValue placeholder={t("EvtChooseWarehouse")} />
</SelectTrigger>
<SelectContent>
{returnOptions.map(w => (
<SelectItem key={w.id} value={String(w.id)}>{w.code} — {w.name}</SelectItem>
))}
</SelectContent>
</Select>
</div>
)}
{state?.phase === "CLOSED" && (
<p className="text-sm">{t("EvtCloseOutClosed", { warehouse: codeOf(state.returnWarehouseId) })}</p>
)}
{error && <ErrorState message={t(error)} />}
<DialogFooter>
<Button variant="outline" onClick={onClose}>{t("EvtDone")}</Button>
{state?.phase === "NOT_STARTED" && (
<Button disabled={busy || blocked} onClick={start}>{t("EvtStartCloseOutCount")}</Button>
)}
{state?.phase === "READY_TO_CLOSE" && (
<Button disabled={busy || !returnId} onClick={close}>{t("EvtReturnStockAndClose")}</Button>
)}
</DialogFooter>
</DialogContent>
</Dialog>
);
}