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
171 lines
7.4 KiB
TypeScript
171 lines
7.4 KiB
TypeScript
"use client";
|
|
|
|
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
import type { SortingState } from "@tanstack/react-table";
|
|
import Link from "next/link";
|
|
import { useRouter } from "next/navigation";
|
|
import { Badge } from "@/app/components/ui/badge";
|
|
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 { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/app/components/ui/table";
|
|
import { SortableHead, sortRows } from "@/app/components/ui/sortable-head";
|
|
import { EmptyState, ErrorState, TableSkeleton } from "@/app/components/ui/states";
|
|
import { useAdminAuth } from "@/app/lib/AdminAuthContext";
|
|
import { INVENTORY_MODULES } from "@/app/lib/inventoryModules";
|
|
import { formatDateTime } from "@/app/lib/format";
|
|
import { listStockCounts, openStockCount } from "@/app/lib/services/adminStockCounts";
|
|
import { listAdminWarehouses } from "@/app/lib/services/adminWarehouses";
|
|
import { STOCK_COUNT_STATUS_LABEL, STOCK_COUNT_STATUS_TONE } from "@/app/lib/stockCountStatus";
|
|
import type { AdminWarehouse, StockCount } from "@/app/lib/types";
|
|
import { useT } from "@/app/lib/i18n/MessagesProvider";
|
|
|
|
// Stock counts tab (add-stock-count-event-closeout): lists counts and opens a new one. Opening a
|
|
// count freezes its warehouse until the count is approved or cancelled. The ONLINE warehouse is
|
|
// counted in QianYi, so it is never offered.
|
|
|
|
const message = (e: unknown) => (e instanceof Error ? e.message : String(e));
|
|
|
|
export function StockCountsTab() {
|
|
const t = useT();
|
|
const { hasModule } = useAdminAuth();
|
|
const router = useRouter();
|
|
const [counts, setCounts] = useState<StockCount[] | null>(null);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [newOpen, setNewOpen] = useState(false);
|
|
const [sorting, setSorting] = useState<SortingState>([]);
|
|
const sortedCounts = useMemo(
|
|
() =>
|
|
sortRows(counts ?? [], sorting, {
|
|
id: c => c.id,
|
|
warehouse: c => c.warehouseCode ?? c.warehouseId,
|
|
status: c => c.status,
|
|
opened: c => Number.isNaN(Date.parse(c.openedDate ?? "")) ? null : Date.parse(c.openedDate!),
|
|
submittedBy: c => c.submittedBy,
|
|
closedBy: c => c.closedBy,
|
|
}),
|
|
[counts, sorting],
|
|
);
|
|
|
|
// State is only set in the async callbacks, never synchronously inside the effect.
|
|
const load = useCallback(() => {
|
|
listStockCounts({})
|
|
.then(rows => {
|
|
setError(null);
|
|
setCounts(rows);
|
|
})
|
|
.catch(e => setError(message(e)));
|
|
}, []);
|
|
useEffect(load, [load]);
|
|
|
|
return (
|
|
<div className="space-y-3">
|
|
{hasModule(INVENTORY_MODULES.count) && (
|
|
<div className="flex justify-end">
|
|
<Button onClick={() => setNewOpen(true)}>{t("StockCountNew")}</Button>
|
|
</div>
|
|
)}
|
|
{error && <ErrorState message={error} onRetry={load} />}
|
|
{!error && counts === null && <TableSkeleton columns={6} />}
|
|
{!error && counts?.length === 0 && <EmptyState title={t("StockCountsEmpty")} />}
|
|
{!error && counts && counts.length > 0 && (
|
|
<div className="overflow-hidden rounded-table border bg-card">
|
|
<Table aria-label={t("BO_STOCK_COUNT")}>
|
|
<TableHeader>
|
|
<TableRow>
|
|
<SortableHead label={t("StockCountColCount")} sortKey="id" sorting={sorting} onSortingChange={setSorting} />
|
|
<SortableHead label={t("Warehouse")} sortKey="warehouse" sorting={sorting} onSortingChange={setSorting} />
|
|
<SortableHead label={t("Status")} sortKey="status" sorting={sorting} onSortingChange={setSorting} />
|
|
<SortableHead label={t("StockCountColOpened")} sortKey="opened" sorting={sorting} onSortingChange={setSorting} />
|
|
<SortableHead label={t("StockCountColSubmittedBy")} sortKey="submittedBy" sorting={sorting} onSortingChange={setSorting} />
|
|
<SortableHead label={t("StockCountColClosedBy")} sortKey="closedBy" sorting={sorting} onSortingChange={setSorting} />
|
|
</TableRow>
|
|
</TableHeader>
|
|
<TableBody>
|
|
{sortedCounts.map(c => (
|
|
<TableRow key={c.id}>
|
|
<TableCell className="tabular">
|
|
#{c.id}
|
|
</TableCell>
|
|
<TableCell>
|
|
<Link className="font-medium text-primary underline-offset-4 hover:underline focus-visible:ring-2 focus-visible:ring-ring focus-visible:outline-none" href={`/inventory/stock-counts/${c.id}`}>{c.warehouseCode ?? c.warehouseId}</Link>
|
|
</TableCell>
|
|
<TableCell>
|
|
<Badge variant={STOCK_COUNT_STATUS_TONE[c.status]}>{t(STOCK_COUNT_STATUS_LABEL[c.status])}</Badge>
|
|
</TableCell>
|
|
<TableCell className="tabular">{formatDateTime(c.openedDate)}</TableCell>
|
|
<TableCell>{c.submittedBy ?? "—"}</TableCell>
|
|
<TableCell>{c.closedBy ?? "—"}</TableCell>
|
|
</TableRow>
|
|
))}
|
|
</TableBody>
|
|
</Table>
|
|
</div>
|
|
)}
|
|
{newOpen && (
|
|
<NewCountDialog
|
|
onClose={() => setNewOpen(false)}
|
|
onOpened={c => router.push(`/inventory/stock-counts/${c.id}`)}
|
|
/>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function NewCountDialog({ onClose, onOpened }: { onClose: () => void; onOpened: (c: StockCount) => void }) {
|
|
const t = useT();
|
|
const [warehouses, setWarehouses] = useState<AdminWarehouse[]>([]);
|
|
const [warehouseId, setWarehouseId] = useState("");
|
|
const [busy, setBusy] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
listAdminWarehouses()
|
|
.then(rows => setWarehouses(rows.filter(w => w.status === "Y" && w.type !== "ONLINE")))
|
|
.catch(e => setError(message(e)));
|
|
}, []);
|
|
|
|
const start = async () => {
|
|
setBusy(true);
|
|
setError(null);
|
|
try {
|
|
onOpened(await openStockCount(warehouseId));
|
|
} catch (e) {
|
|
setError(message(e));
|
|
setBusy(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<Dialog open onOpenChange={open => { if (!open) onClose(); }}>
|
|
<DialogContent>
|
|
<DialogHeader>
|
|
<DialogTitle>{t("StockCountNewTitle")}</DialogTitle>
|
|
</DialogHeader>
|
|
<p className="text-sm text-muted-foreground">
|
|
{t("StockCountFreezeNotice")}
|
|
</p>
|
|
<div className="space-y-2">
|
|
<Label htmlFor="stock-count-warehouse">{t("Warehouse")}</Label>
|
|
<Select value={warehouseId} onValueChange={setWarehouseId}>
|
|
<SelectTrigger id="stock-count-warehouse" aria-label={t("Warehouse")}>
|
|
<SelectValue placeholder={t("StockCountChooseWarehouse")} />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
{warehouses.map(w => (
|
|
<SelectItem key={w.id} value={String(w.id)}>{w.code} — {w.name}</SelectItem>
|
|
))}
|
|
</SelectContent>
|
|
</Select>
|
|
</div>
|
|
{error && <ErrorState message={error} />}
|
|
<DialogFooter>
|
|
<Button variant="outline" onClick={onClose}>{t("Cancel")}</Button>
|
|
<Button onClick={start} disabled={!warehouseId || busy}>{t("StockCountStart")}</Button>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
}
|