phase-5: contracts, warranties, insurance, demo equipment, and department visits
This commit is contained in:
@@ -3,6 +3,7 @@ import { Search, X, ShieldCheck, Ban } from "lucide-react";
|
||||
import { assetsApi, type AssetLookupItem } from "@/api/assets";
|
||||
import { formatDate } from "@/lib/format";
|
||||
import { CoverageBadge, AssetStatusBadge } from "./StatusBadge";
|
||||
import { InsuranceDrawer } from "./InsuranceDrawer";
|
||||
|
||||
interface AssetPickerProps {
|
||||
selectedAsset: AssetLookupItem | null;
|
||||
@@ -24,6 +25,7 @@ export function AssetPicker({
|
||||
const [results, setResults] = useState<AssetLookupItem[]>([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [open, setOpen] = useState(false);
|
||||
const [insuranceOpen, setInsuranceOpen] = useState(false);
|
||||
const containerRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -148,11 +150,28 @@ export function AssetPicker({
|
||||
</div>
|
||||
<div>
|
||||
<div className="text-gray-500">Insurance</div>
|
||||
<div className="font-medium text-gray-800">
|
||||
{selectedAsset.insurance_summary || selectedAsset.insurance_status || "None"}
|
||||
<div className="flex items-center gap-2 flex-wrap">
|
||||
<span className="font-medium text-gray-800">
|
||||
{selectedAsset.insurance_summary || selectedAsset.insurance_status || "None"}
|
||||
</span>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setInsuranceOpen(true)}
|
||||
className="text-xs font-medium text-[#1e3a8a] hover:underline"
|
||||
>
|
||||
View Insurance
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{insuranceOpen && (
|
||||
<InsuranceDrawer
|
||||
open={insuranceOpen}
|
||||
onClose={() => setInsuranceOpen(false)}
|
||||
assetId={selectedAsset.id}
|
||||
assetLabel={`${selectedAsset.asset_code} — ${selectedAsset.name}`}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,180 @@
|
||||
import { useEffect } from "react";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { Link } from "react-router-dom";
|
||||
import { X, ShieldCheck, AlertCircle, ExternalLink } from "lucide-react";
|
||||
import { insuranceApi } from "@/api/phase5";
|
||||
import { useAuthStore } from "@/hooks/useAuth";
|
||||
import { can } from "@/lib/permissions";
|
||||
import { formatDate, formatINR } from "@/lib/format";
|
||||
import { ExpiryChip } from "@/components/StatusBadge";
|
||||
import { Attachments } from "@/components/Attachments";
|
||||
import { Button } from "@/components/ui/button";
|
||||
|
||||
interface InsuranceDrawerProps {
|
||||
open: boolean;
|
||||
onClose: () => void;
|
||||
assetId: number | null;
|
||||
assetLabel?: string;
|
||||
}
|
||||
|
||||
export function InsuranceDrawer({
|
||||
open,
|
||||
onClose,
|
||||
assetId,
|
||||
assetLabel,
|
||||
}: InsuranceDrawerProps) {
|
||||
const role = useAuthStore((s) => s.user?.role);
|
||||
const canManage = can(role, "manage_calibration_contracts");
|
||||
|
||||
const policiesQuery = useQuery({
|
||||
queryKey: ["insurance", "asset-drawer", assetId],
|
||||
queryFn: () => insuranceApi.list({ asset_id: assetId!, page_size: 200 }),
|
||||
enabled: open && Boolean(assetId),
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) return;
|
||||
function onKeyDown(e: KeyboardEvent) {
|
||||
if (e.key === "Escape") onClose();
|
||||
}
|
||||
window.addEventListener("keydown", onKeyDown);
|
||||
return () => window.removeEventListener("keydown", onKeyDown);
|
||||
}, [open, onClose]);
|
||||
|
||||
if (!open || !assetId) return null;
|
||||
|
||||
const policies = policiesQuery.data?.items ?? [];
|
||||
|
||||
return (
|
||||
<div
|
||||
className="fixed inset-0 z-50 flex justify-end bg-black/40"
|
||||
role="dialog"
|
||||
aria-modal="true"
|
||||
aria-label="Asset Insurance Policies"
|
||||
onClick={onClose}
|
||||
>
|
||||
<div
|
||||
className="w-full max-w-lg bg-white h-full shadow-2xl border-l border-gray-200 flex flex-col"
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
>
|
||||
<div className="flex items-center justify-between px-5 py-4 border-b border-gray-200 bg-gray-50">
|
||||
<div className="min-w-0">
|
||||
<div className="flex items-center gap-2 text-sm font-semibold text-gray-900">
|
||||
<ShieldCheck className="h-4 w-4 text-[#1e3a8a] shrink-0" />
|
||||
<span>Asset Insurance</span>
|
||||
</div>
|
||||
{assetLabel && (
|
||||
<p className="text-xs text-gray-500 truncate mt-0.5">{assetLabel}</p>
|
||||
)}
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
onClick={onClose}
|
||||
aria-label="Close insurance drawer"
|
||||
className="p-1.5 rounded-md text-gray-500 hover:text-gray-800 hover:bg-gray-200"
|
||||
>
|
||||
<X className="h-4 w-4" />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="flex-1 overflow-y-auto p-5 space-y-4">
|
||||
{policiesQuery.isLoading ? (
|
||||
<div className="space-y-3">
|
||||
<div className="h-28 bg-gray-100 rounded-lg animate-pulse" />
|
||||
<div className="h-28 bg-gray-100 rounded-lg animate-pulse" />
|
||||
</div>
|
||||
) : policiesQuery.isError ? (
|
||||
<div className="flex items-start gap-2 rounded-md bg-red-50 border border-red-200 p-3 text-sm text-red-700">
|
||||
<AlertCircle className="h-4 w-4 shrink-0 mt-0.5" />
|
||||
<span>Failed to load insurance policies for this asset.</span>
|
||||
</div>
|
||||
) : policies.length === 0 ? (
|
||||
<div className="text-center py-10 bg-gray-50 border border-dashed border-gray-200 rounded-lg space-y-2">
|
||||
<ShieldCheck className="h-8 w-8 text-gray-300 mx-auto" />
|
||||
<p className="text-sm font-medium text-gray-700">
|
||||
No insurance policies recorded for this asset.
|
||||
</p>
|
||||
<p className="text-xs text-gray-500">
|
||||
Policies can be added from the Insurance register.
|
||||
</p>
|
||||
</div>
|
||||
) : (
|
||||
policies.map((p) => (
|
||||
<div
|
||||
key={p.id}
|
||||
className="rounded-lg border border-gray-200 bg-white p-4 space-y-3 shadow-sm"
|
||||
>
|
||||
<div className="flex items-start justify-between gap-2">
|
||||
<div>
|
||||
<div className="font-mono text-sm font-bold text-[#1e3a8a]">
|
||||
{p.policy_no}
|
||||
</div>
|
||||
<div className="text-xs text-gray-600 mt-0.5">
|
||||
{p.insurer_name || `Insurer #${p.insurer_id}`}
|
||||
</div>
|
||||
</div>
|
||||
<ExpiryChip
|
||||
daysToExpiry={p.days_to_expiry}
|
||||
status={p.status}
|
||||
severity={p.severity}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-3 gap-2 text-xs bg-gray-50 rounded-md p-2.5 border border-gray-100">
|
||||
<div>
|
||||
<div className="text-gray-500">Start</div>
|
||||
<div className="font-medium text-gray-900">
|
||||
{formatDate(p.start_date)}
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<div className="text-gray-500">End</div>
|
||||
<div className="font-medium text-gray-900">
|
||||
{formatDate(p.end_date)}
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<div className="text-gray-500">Coverage</div>
|
||||
<div className="font-medium text-gray-900">
|
||||
{formatINR(p.coverage_amount)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{p.remarks && (
|
||||
<p className="text-xs text-gray-600">
|
||||
<span className="font-medium">Remarks:</span> {p.remarks}
|
||||
</p>
|
||||
)}
|
||||
|
||||
<div className="pt-2 border-t border-gray-100">
|
||||
<div className="text-xs font-semibold text-gray-700 mb-1.5">
|
||||
Policy Certificates & Documents
|
||||
</div>
|
||||
<Attachments
|
||||
entityType="insurance"
|
||||
entityId={p.id}
|
||||
canUpload={canManage}
|
||||
canDelete={canManage}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
))
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="px-5 py-3 border-t border-gray-200 bg-gray-50 flex items-center justify-between gap-2">
|
||||
<Link to={`/insurance`} onClick={onClose}>
|
||||
<Button variant="outline" size="sm">
|
||||
<ExternalLink className="h-3.5 w-3.5 mr-1.5" />
|
||||
Open Insurance Register
|
||||
</Button>
|
||||
</Link>
|
||||
<Button variant="outline" size="sm" onClick={onClose}>
|
||||
Close
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -10,8 +10,8 @@ import { Button } from "@/components/ui/button";
|
||||
import { AssetPicker } from "@/components/AssetPicker";
|
||||
import { validateAttachmentFile } from "@/components/Attachments";
|
||||
import { ApiError } from "@/api/client";
|
||||
import type { AssetLookupItem, AssetCriticality } from "@/api/assets";
|
||||
import { complaintsApi } from "@/api/complaints";
|
||||
import { assetsApi, type AssetLookupItem, type AssetCriticality } from "@/api/assets";
|
||||
import { complaintsApi, type ComplaintItem } from "@/api/complaints";
|
||||
import { attachmentsApi } from "@/api/attachments";
|
||||
import { fetchUsersLookup } from "@/api/masters";
|
||||
import { useAuthStore } from "@/hooks/useAuth";
|
||||
@@ -29,13 +29,24 @@ type ComplaintFormValues = z.infer<typeof complaintSchema>;
|
||||
interface RaiseComplaintModalProps {
|
||||
open: boolean;
|
||||
onClose: () => void;
|
||||
initialAssetId?: number | null;
|
||||
initialDescription?: string;
|
||||
onCreated?: (complaint: ComplaintItem) => Promise<void> | void;
|
||||
navigateOnCreate?: boolean;
|
||||
}
|
||||
|
||||
const inputCls =
|
||||
"w-full px-3 py-2 text-sm border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500";
|
||||
const labelCls = "block text-sm font-medium text-gray-700 mb-1";
|
||||
|
||||
export function RaiseComplaintModal({ open, onClose }: RaiseComplaintModalProps) {
|
||||
export function RaiseComplaintModal({
|
||||
open,
|
||||
onClose,
|
||||
initialAssetId = null,
|
||||
initialDescription = "",
|
||||
onCreated,
|
||||
navigateOnCreate = true,
|
||||
}: RaiseComplaintModalProps) {
|
||||
const navigate = useNavigate();
|
||||
const qc = useQueryClient();
|
||||
const user = useAuthStore((s) => s.user);
|
||||
@@ -69,17 +80,39 @@ export function RaiseComplaintModal({ open, onClose }: RaiseComplaintModalProps)
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) return;
|
||||
setSelectedAsset(null);
|
||||
setFiles([]);
|
||||
setServerError(null);
|
||||
reset({
|
||||
description: "",
|
||||
description: initialDescription,
|
||||
breakdown_details: "",
|
||||
priority: "non_critical",
|
||||
notes: "",
|
||||
received_by_id: user?.id ? String(user.id) : "",
|
||||
});
|
||||
}, [open, user?.id, reset]);
|
||||
if (initialAssetId) {
|
||||
let cancelled = false;
|
||||
assetsApi
|
||||
.get(initialAssetId)
|
||||
.then((a) => {
|
||||
if (cancelled) return;
|
||||
setSelectedAsset({
|
||||
...a,
|
||||
contract_summary: a.contract_type
|
||||
? `${a.contract_type} (${a.contract_status})`
|
||||
: "None",
|
||||
insurance_summary: a.insurance_status || "None",
|
||||
});
|
||||
})
|
||||
.catch(() => {
|
||||
if (!cancelled) setSelectedAsset(null);
|
||||
});
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
} else {
|
||||
setSelectedAsset(null);
|
||||
}
|
||||
}, [open, user?.id, initialAssetId, initialDescription, reset]);
|
||||
|
||||
function handleFilePick(e: React.ChangeEvent<HTMLInputElement>) {
|
||||
const picked = Array.from(e.target.files ?? []);
|
||||
@@ -117,8 +150,13 @@ export function RaiseComplaintModal({ open, onClose }: RaiseComplaintModalProps)
|
||||
|
||||
await qc.invalidateQueries({ queryKey: ["complaints"] });
|
||||
await qc.invalidateQueries({ queryKey: ["assets"] });
|
||||
if (onCreated) {
|
||||
await onCreated(created);
|
||||
}
|
||||
onClose();
|
||||
navigate(`/complaints/${created.id}`);
|
||||
if (navigateOnCreate) {
|
||||
navigate(`/complaints/${created.id}`);
|
||||
}
|
||||
} catch (err) {
|
||||
setServerError(err instanceof ApiError ? err.detail : "Failed to register complaint.");
|
||||
}
|
||||
|
||||
@@ -306,3 +306,120 @@ export function CallStatusBadge({
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
||||
export function ExpiryChip({
|
||||
daysToExpiry,
|
||||
status,
|
||||
severity,
|
||||
className,
|
||||
}: {
|
||||
daysToExpiry: number;
|
||||
status: string;
|
||||
severity?: string;
|
||||
className?: string;
|
||||
}) {
|
||||
if (status === "superseded") {
|
||||
return (
|
||||
<span
|
||||
className={cn(
|
||||
"inline-flex items-center gap-1 px-2.5 py-0.5 rounded-full text-xs font-medium whitespace-nowrap bg-gray-100 text-gray-600 border border-gray-300",
|
||||
className
|
||||
)}
|
||||
>
|
||||
<MinusCircle className="h-3 w-3 shrink-0" />
|
||||
Superseded
|
||||
</span>
|
||||
);
|
||||
}
|
||||
if (status === "expired" || daysToExpiry < 0) {
|
||||
return (
|
||||
<span
|
||||
className={cn(
|
||||
"inline-flex items-center gap-1 px-2.5 py-0.5 rounded-full text-xs font-medium whitespace-nowrap bg-red-100 text-red-800 border border-red-200",
|
||||
className
|
||||
)}
|
||||
>
|
||||
<AlertCircle className="h-3 w-3 shrink-0" />
|
||||
Expired
|
||||
</span>
|
||||
);
|
||||
}
|
||||
if (status === "expiring") {
|
||||
const isRed = severity === "red";
|
||||
return (
|
||||
<span
|
||||
className={cn(
|
||||
"inline-flex items-center gap-1 px-2.5 py-0.5 rounded-full text-xs font-medium whitespace-nowrap border",
|
||||
isRed
|
||||
? "bg-red-100 text-red-800 border-red-200"
|
||||
: "bg-amber-100 text-amber-800 border-amber-200",
|
||||
className
|
||||
)}
|
||||
>
|
||||
<Clock className="h-3 w-3 shrink-0" />
|
||||
{daysToExpiry} {daysToExpiry === 1 ? "day" : "days"} left
|
||||
</span>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<span
|
||||
className={cn(
|
||||
"inline-flex items-center gap-1 px-2.5 py-0.5 rounded-full text-xs font-medium whitespace-nowrap bg-green-100 text-green-800 border border-green-200",
|
||||
className
|
||||
)}
|
||||
>
|
||||
<CheckCircle2 className="h-3 w-3 shrink-0" />
|
||||
Active
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
||||
export function DemoStatusBadge({
|
||||
status,
|
||||
daysRemaining,
|
||||
className,
|
||||
}: {
|
||||
status: string;
|
||||
daysRemaining?: number;
|
||||
className?: string;
|
||||
}) {
|
||||
if (status === "returned") {
|
||||
return (
|
||||
<span
|
||||
className={cn(
|
||||
"inline-flex items-center gap-1.5 px-2.5 py-0.5 rounded-full text-xs font-medium whitespace-nowrap bg-green-100 text-green-800 border border-green-200",
|
||||
className
|
||||
)}
|
||||
>
|
||||
<CheckCircle2 className="h-3 w-3 shrink-0" />
|
||||
Returned
|
||||
</span>
|
||||
);
|
||||
}
|
||||
if (status === "overdue_return") {
|
||||
const overdueDays =
|
||||
daysRemaining != null && daysRemaining < 0 ? Math.abs(daysRemaining) : null;
|
||||
return (
|
||||
<span
|
||||
className={cn(
|
||||
"inline-flex items-center gap-1.5 px-2.5 py-0.5 rounded-full text-xs font-medium whitespace-nowrap bg-red-100 text-red-800 border border-red-200",
|
||||
className
|
||||
)}
|
||||
>
|
||||
<AlertTriangle className="h-3 w-3 shrink-0" />
|
||||
{overdueDays != null ? `Overdue by ${overdueDays}d` : "Overdue Return"}
|
||||
</span>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<span
|
||||
className={cn(
|
||||
"inline-flex items-center gap-1.5 px-2.5 py-0.5 rounded-full text-xs font-medium whitespace-nowrap bg-blue-100 text-blue-800 border border-blue-200",
|
||||
className
|
||||
)}
|
||||
>
|
||||
<Clock className="h-3 w-3 shrink-0" />
|
||||
On Demo
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user