c_agri_trade/project_code/frontend/src/features/compliance/MandiPermitsView.tsx

445 lines
19 KiB
TypeScript

/**
* SCREEN-MANDI-001: Mandi Transit Permits (Anugya Patra) & Compliance Console.
*
* Fulfills:
* - STORY-MANDI-001 (FR-MANDI-001, FR-MANDI-002, FR-MANDI-003):
* Statutory transit permits, validity expiry tracking, and single-point tax paid volume balance.
*
* Styled per DESIGN.md: AgriTrade Mercantile (Tactile Corporate Minimalism).
*/
import React, { useState, useEffect, useCallback } from 'react';
import { Card } from '../../components/ui/Card';
import { Button } from '../../components/ui/Button';
import { StatusPill } from '../../components/ui/StatusPill';
import { apiClient } from '../../services/api';
import {
FileText,
Truck,
ShieldCheck,
Plus,
RefreshCw,
AlertTriangle,
CheckCircle2,
Building2,
} from 'lucide-react';
export interface MandiTransitPermit {
id: string;
permit_number: string;
mandi_samiti_code: string;
dispatch_id: string;
vehicle_reg_no: string;
commodity_id: string;
quantity_mt: number;
bag_count: number;
destination_city: string;
issued_at: string;
valid_until: string;
tax_paid_status: string;
status: string;
}
export interface TaxLedgerEntry {
id: string;
commodity_id: string;
opening_tax_paid_mt: number;
inward_tax_paid_mt: number;
dispatched_tax_paid_mt: number;
closing_tax_paid_mt: number;
created_at: string;
}
export const MandiPermitsView: React.FC = () => {
const [permits, setPermits] = useState<MandiTransitPermit[]>([]);
const [taxLedgers, setTaxLedgers] = useState<TaxLedgerEntry[]>([]);
const [loading, setLoading] = useState<boolean>(true);
const [error, setError] = useState<string | null>(null);
// Modal & form states
const [isModalOpen, setIsModalOpen] = useState<boolean>(false);
const [permitNo, setPermitNo] = useState<string>('');
const [samitiCode, setSamitiCode] = useState<string>('MANDI-NEEMUCH-01');
const [dispatchId, setDispatchId] = useState<string>('');
const [vehicleNo, setVehicleNo] = useState<string>('');
const [commodityId, setCommodityId] = useState<string>('SOYBEAN');
const [quantityMt, setQuantityMt] = useState<number>(25.0);
const [bagCount, setBagCount] = useState<number>(500);
const [destinationCity, setDestinationCity] = useState<string>('Kota');
const [validityHours, setValidityHours] = useState<number>(36);
const fetchData = useCallback(async () => {
setLoading(true);
setError(null);
try {
const [permitsRes, taxRes] = await Promise.all([
apiClient.get<{ permits: MandiTransitPermit[] }>('/api/v1/compliance/mandi/permits'),
apiClient.get<{ ledger_entries: TaxLedgerEntry[] }>('/api/v1/compliance/mandi/tax-ledger'),
]);
setPermits(permitsRes.permits || []);
setTaxLedgers(taxRes.ledger_entries || []);
} catch (err: any) {
setError(err?.message || 'Failed to load transit permits data');
} finally {
setLoading(false);
}
}, []);
useEffect(() => {
fetchData();
}, [fetchData]);
const handleCreatePermit = async (e: React.FormEvent) => {
e.preventDefault();
try {
const now = new Date();
const validUntil = new Date(now.getTime() + validityHours * 3600 * 1000);
await apiClient.post('/api/v1/compliance/mandi/permits', {
permit_number: permitNo || `ANUGYA-${Date.now().toString().slice(-6)}`,
mandi_samiti_code: samitiCode,
dispatch_id: dispatchId,
vehicle_reg_no: vehicleNo,
commodity_id: commodityId,
quantity_mt: quantityMt,
bag_count: bagCount,
destination_city: destinationCity,
issued_at: now.toISOString(),
valid_until: validUntil.toISOString(),
});
setIsModalOpen(false);
fetchData();
} catch (err: any) {
alert(err?.message || 'Failed to issue transit permit');
}
};
const activePermits = permits.filter((p) => p.status === 'ISSUED' || p.status === 'IN_TRANSIT');
const totalTaxPaidBalance = taxLedgers.reduce((acc, curr) => acc + curr.closing_tax_paid_mt, 0);
return (
<div className="space-y-6">
{/* Header */}
<div className="flex flex-col md:flex-row md:items-center justify-between gap-4 pb-4 border-b border-card-border">
<div>
<div className="flex items-center space-x-2">
<h2 className="text-xl font-bold text-text-primary tracking-tight">Mandi Transit Permits Console</h2>
<StatusPill status="normal" label="APMC Statutory" />
</div>
<p className="text-xs text-text-muted mt-1">
Official Anugya Patra generation, validity window monitoring, and single-point Mandi tax-paid volume ledger.
</p>
</div>
<div className="flex items-center space-x-3">
<Button variant="primary" onClick={() => setIsModalOpen(true)}>
<Plus className="w-4 h-4 mr-1.5" />
Issue Anugya Patra
</Button>
<Button variant="secondary" onClick={fetchData} disabled={loading}>
<RefreshCw className={`w-4 h-4 ${loading ? 'animate-spin' : ''}`} />
</Button>
</div>
</div>
{/* KPI Cards */}
<div className="grid grid-cols-1 md:grid-cols-4 gap-4">
<Card className="p-4 border-l-4 border-l-emerald-600">
<div className="flex items-center justify-between">
<span className="text-xs font-mono uppercase text-text-muted">Active In-Transit Permits</span>
<Truck className="w-4 h-4 text-emerald-600" />
</div>
<div className="text-2xl font-bold font-mono text-text-primary mt-2">
{activePermits.length}
</div>
<p className="text-[11px] text-text-muted mt-1">En-route with valid window</p>
</Card>
<Card className="p-4 border-l-4 border-l-accent-ochre">
<div className="flex items-center justify-between">
<span className="text-xs font-mono uppercase text-text-muted">Total Permits Issued</span>
<FileText className="w-4 h-4 text-accent-ochre" />
</div>
<div className="text-2xl font-bold font-mono text-text-primary mt-2">
{permits.length}
</div>
<p className="text-[11px] text-text-muted mt-1">All time statutory Anugya Patra</p>
</Card>
<Card className="p-4 border-l-4 border-l-blue-600">
<div className="flex items-center justify-between">
<span className="text-xs font-mono uppercase text-text-muted">Tax-Paid Volume (MT)</span>
<ShieldCheck className="w-4 h-4 text-blue-600" />
</div>
<div className="text-2xl font-bold font-mono text-text-primary mt-2">
{totalTaxPaidBalance.toFixed(2)} MT
</div>
<p className="text-[11px] text-text-muted mt-1">Shulk Chhukta balance</p>
</Card>
<Card className="p-4 border-l-4 border-l-amber-600">
<div className="flex items-center justify-between">
<span className="text-xs font-mono uppercase text-text-muted">Primary Samiti</span>
<Building2 className="w-4 h-4 text-amber-600" />
</div>
<div className="text-sm font-bold font-mono text-text-primary mt-2">
APMC Neemuch / Kota
</div>
<p className="text-[11px] text-text-muted mt-1">Single-point jurisdiction</p>
</Card>
</div>
{/* Permits Table */}
<Card className="overflow-hidden">
<div className="p-4 border-b border-card-border flex items-center justify-between bg-card-surface/50">
<div className="flex items-center space-x-2">
<FileText className="w-4 h-4 text-accent-ochre" />
<h3 className="text-sm font-semibold text-text-primary">Statutory Anugya Patra Register</h3>
</div>
</div>
{error && (
<div className="p-4 bg-red-950/20 border-b border-red-800/40 text-red-400 text-xs flex items-center space-x-2">
<AlertTriangle className="w-4 h-4 flex-shrink-0" />
<span>{error}</span>
</div>
)}
<div className="overflow-x-auto">
<table className="w-full text-left text-xs">
<thead className="bg-card-surface border-b border-card-border text-text-muted uppercase font-mono text-[10px]">
<tr>
<th className="p-3">Permit #</th>
<th className="p-3">Samiti Code</th>
<th className="p-3">Vehicle #</th>
<th className="p-3">Commodity</th>
<th className="p-3 text-right">Tonnage (MT)</th>
<th className="p-3 text-right">Bags</th>
<th className="p-3">Destination</th>
<th className="p-3">Valid Until</th>
<th className="p-3 text-center">Status</th>
</tr>
</thead>
<tbody className="divide-y divide-card-border font-mono">
{permits.length === 0 ? (
<tr>
<td colSpan={9} className="p-8 text-center text-text-muted">
No statutory transit permits issued. Click "Issue Anugya Patra" to issue the first transit pass.
</td>
</tr>
) : (
permits.map((p) => (
<tr key={p.id} className="hover:bg-card-surface/50 transition">
<td className="p-3 font-semibold text-text-primary">{p.permit_number}</td>
<td className="p-3 text-text-muted">{p.mandi_samiti_code}</td>
<td className="p-3 font-semibold text-text-primary">{p.vehicle_reg_no}</td>
<td className="p-3">{p.commodity_id}</td>
<td className="p-3 text-right font-bold text-text-primary">{p.quantity_mt} MT</td>
<td className="p-3 text-right">{p.bag_count}</td>
<td className="p-3 text-text-muted">{p.destination_city}</td>
<td className="p-3 text-text-muted">
{new Date(p.valid_until).toLocaleString()}
</td>
<td className="p-3 text-center">
<span className="px-2 py-0.5 rounded text-[10px] bg-emerald-950/40 text-emerald-400 border border-emerald-800/40">
{p.status}
</span>
</td>
</tr>
))
)}
</tbody>
</table>
</div>
</Card>
{/* Tax Paid Volume Ledger */}
<Card className="overflow-hidden">
<div className="p-4 border-b border-card-border flex items-center justify-between bg-card-surface/50">
<div className="flex items-center space-x-2">
<ShieldCheck className="w-4 h-4 text-emerald-500" />
<h3 className="text-sm font-semibold text-text-primary">Single-Point Mandi Tax Paid Ledger (Shulk Chhukta)</h3>
</div>
</div>
<div className="overflow-x-auto">
<table className="w-full text-left text-xs">
<thead className="bg-card-surface border-b border-card-border text-text-muted uppercase font-mono text-[10px]">
<tr>
<th className="p-3">Commodity</th>
<th className="p-3 text-right">Opening Balance (MT)</th>
<th className="p-3 text-right">Tax-Paid Inward (MT)</th>
<th className="p-3 text-right">Permit Dispatched (MT)</th>
<th className="p-3 text-right">Closing Balance (MT)</th>
<th className="p-3 text-center">Invariant Status</th>
</tr>
</thead>
<tbody className="divide-y divide-card-border font-mono">
{taxLedgers.length === 0 ? (
<tr>
<td colSpan={6} className="p-6 text-center text-text-muted">
No active tax-paid ledger entries recorded yet.
</td>
</tr>
) : (
taxLedgers.map((l) => (
<tr key={l.id} className="hover:bg-card-surface/50 transition">
<td className="p-3 font-semibold text-text-primary">{l.commodity_id}</td>
<td className="p-3 text-right text-text-muted">{l.opening_tax_paid_mt}</td>
<td className="p-3 text-right text-emerald-400">+{l.inward_tax_paid_mt}</td>
<td className="p-3 text-right text-red-400">-{l.dispatched_tax_paid_mt}</td>
<td className="p-3 text-right font-bold text-text-primary text-sm">{l.closing_tax_paid_mt} MT</td>
<td className="p-3 text-center">
<span className="inline-flex items-center text-[10px] text-emerald-400 gap-1 bg-emerald-950/30 px-2 py-0.5 rounded border border-emerald-800/40">
<CheckCircle2 className="w-3 h-3" />
Non-Negative
</span>
</td>
</tr>
))
)}
</tbody>
</table>
</div>
</Card>
{/* Issue Permit Modal */}
{isModalOpen && (
<div className="fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4">
<Card className="max-w-lg w-full p-6 space-y-4">
<div className="flex items-center justify-between border-b border-card-border pb-3">
<h3 className="text-sm font-bold text-text-primary flex items-center gap-2">
<FileText className="w-4 h-4 text-accent-ochre" />
Issue Official Anugya Patra (Transit Pass)
</h3>
<button
onClick={() => setIsModalOpen(false)}
className="text-text-muted hover:text-white"
>
&times;
</button>
</div>
<form onSubmit={handleCreatePermit} className="space-y-3 text-xs">
<div className="grid grid-cols-2 gap-3">
<div>
<label className="block text-text-muted font-mono mb-1">Permit Number</label>
<input
type="text"
placeholder="e.g. ANUGYA-2026-001"
value={permitNo}
onChange={(e) => setPermitNo(e.target.value)}
className="w-full bg-sidebar-surface text-white border border-card-border rounded p-2"
required
/>
</div>
<div>
<label className="block text-text-muted font-mono mb-1">Mandi Samiti Code</label>
<input
type="text"
value={samitiCode}
onChange={(e) => setSamitiCode(e.target.value)}
className="w-full bg-sidebar-surface text-white border border-card-border rounded p-2"
required
/>
</div>
</div>
<div className="grid grid-cols-2 gap-3">
<div>
<label className="block text-text-muted font-mono mb-1">Dispatch Reference ID</label>
<input
type="text"
placeholder="e.g. disp-001"
value={dispatchId}
onChange={(e) => setDispatchId(e.target.value)}
className="w-full bg-sidebar-surface text-white border border-card-border rounded p-2"
required
/>
</div>
<div>
<label className="block text-text-muted font-mono mb-1">Vehicle Registration #</label>
<input
type="text"
placeholder="e.g. MP-44-CA-5678"
value={vehicleNo}
onChange={(e) => setVehicleNo(e.target.value)}
className="w-full bg-sidebar-surface text-white border border-card-border rounded p-2"
required
/>
</div>
</div>
<div className="grid grid-cols-3 gap-2">
<div>
<label className="block text-text-muted font-mono mb-1">Commodity</label>
<input
type="text"
value={commodityId}
onChange={(e) => setCommodityId(e.target.value)}
className="w-full bg-sidebar-surface text-white border border-card-border rounded p-2"
required
/>
</div>
<div>
<label className="block text-text-muted font-mono mb-1">Quantity (MT)</label>
<input
type="number"
step="0.001"
value={quantityMt}
onChange={(e) => setQuantityMt(Number(e.target.value))}
className="w-full bg-sidebar-surface text-white border border-card-border rounded p-2"
required
/>
</div>
<div>
<label className="block text-text-muted font-mono mb-1">Bag Count</label>
<input
type="number"
value={bagCount}
onChange={(e) => setBagCount(Number(e.target.value))}
className="w-full bg-sidebar-surface text-white border border-card-border rounded p-2"
required
/>
</div>
</div>
<div className="grid grid-cols-2 gap-3">
<div>
<label className="block text-text-muted font-mono mb-1">Destination City</label>
<input
type="text"
value={destinationCity}
onChange={(e) => setDestinationCity(e.target.value)}
className="w-full bg-sidebar-surface text-white border border-card-border rounded p-2"
required
/>
</div>
<div>
<label className="block text-text-muted font-mono mb-1">Validity (Hours)</label>
<select
value={validityHours}
onChange={(e) => setValidityHours(Number(e.target.value))}
className="w-full bg-sidebar-surface text-white border border-card-border rounded p-2"
>
<option value={24}>24 Hours (Intra-District)</option>
<option value={36}>36 Hours (Inter-District)</option>
<option value={48}>48 Hours (Inter-State)</option>
</select>
</div>
</div>
<div className="pt-2">
<Button variant="primary" type="submit" className="w-full">
Generate Anugya Patra
</Button>
</div>
</form>
</Card>
</div>
)}
</div>
);
};
export default MandiPermitsView;