New pages: - app/(admin)/warranties/ — full warranty management with claims - app/(admin)/refunds/ — refund management - app/(admin)/notification-templates/ — email/SMS template editor - app/(admin)/user-pos-analytics/ — per-user POS analytics New components: - components/quotations/ — quotation components - components/ui/PartNumbersModal.tsx — part numbers selection New services: - creditService.ts, deliverySettingsService.ts - notificationTemplateService.ts, roleNotificationSettingsService.ts Updated: - Sidebar navigation, dashboard, orders, POS sessions, products - Bulk quotations, CRM leads/quotations, notifications gateway - settings, service invoices, quotes, technician, users pages - adminService, billingFinanceService, catalogService, client - inventoryPosService, notificationGatewayService, openwaService, userService - Removed pos-sync page (merged into POS sessions)
143 lines
6.8 KiB
TypeScript
143 lines
6.8 KiB
TypeScript
'use client';
|
|
|
|
import { useState, useEffect } from 'react';
|
|
import { ArrowLeftRight, Check, X, RefreshCw, Search } from 'lucide-react';
|
|
import { apiFetch } from '@/services/api/client';
|
|
import { toast } from 'sonner';
|
|
import { TablePagination, useClientPagination } from '@/components/ui/TablePagination';
|
|
import { formatCurrency } from '@/lib/utils';
|
|
import { format } from 'date-fns';
|
|
|
|
export default function RefundsPage() {
|
|
const [refunds, setRefunds] = useState<any[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [searchQuery, setSearchQuery] = useState('');
|
|
|
|
const loadRefunds = async () => {
|
|
setLoading(true);
|
|
try {
|
|
const data = await apiFetch<any[]>('/api/v1/refunds');
|
|
setRefunds(data);
|
|
} catch (err: any) {
|
|
toast.error(err.message || 'Failed to load refunds');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
loadRefunds();
|
|
}, []);
|
|
|
|
const handleAction = async (refundId: string, action: 'APPROVE' | 'REJECT') => {
|
|
try {
|
|
const payload = action === 'APPROVE'
|
|
? { admin_notes: "Approved from UI" }
|
|
: { rejection_reason: "Rejected from UI" };
|
|
|
|
await apiFetch(`/api/v1/refunds/${refundId}/${action.toLowerCase()}`, {
|
|
method: 'POST',
|
|
body: JSON.stringify(payload)
|
|
});
|
|
toast.success(`Refund ${action === 'APPROVE' ? 'Approved' : 'Rejected'}!`);
|
|
loadRefunds();
|
|
} catch (err: any) {
|
|
toast.error(err.message || `Failed to ${action.toLowerCase()} refund`);
|
|
}
|
|
};
|
|
|
|
const filteredRefunds = refunds.filter((r) =>
|
|
JSON.stringify(r).toLowerCase().includes(searchQuery.toLowerCase())
|
|
);
|
|
|
|
const pager = useClientPagination(filteredRefunds, 10);
|
|
|
|
return (
|
|
<div className="p-6 space-y-6">
|
|
<div className="flex items-center justify-between">
|
|
<div>
|
|
<h1 className="text-2xl font-bold tracking-tight text-foreground flex items-center gap-2">
|
|
<ArrowLeftRight className="w-6 h-6 text-primary" />
|
|
Refunds & Returns Management
|
|
</h1>
|
|
<p className="text-sm text-muted-foreground">Manage return requests, verify part numbers, and process refunds</p>
|
|
</div>
|
|
<button
|
|
onClick={loadRefunds}
|
|
className="w-9 h-9 border border-border crm-radius-control bg-card hover:bg-muted text-muted-foreground hover:text-foreground flex items-center justify-center transition-colors"
|
|
>
|
|
<RefreshCw className={`w-4 h-4 ${loading ? 'animate-spin text-primary' : ''}`} />
|
|
</button>
|
|
</div>
|
|
|
|
<div className="crm-radius-section border border-border bg-card shadow-[0_1px_3px_rgba(16,24,40,0.06)] overflow-hidden">
|
|
<div className="px-5 py-4 border-b border-border flex items-center justify-between gap-4">
|
|
<div className="relative w-[320px]">
|
|
<Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-muted-foreground" />
|
|
<input
|
|
type="text"
|
|
placeholder="Search refunds..."
|
|
value={searchQuery}
|
|
onChange={(e) => setSearchQuery(e.target.value)}
|
|
className="w-full pl-9 pr-4 h-9 crm-radius-control border border-border bg-card text-[13px] focus:outline-none focus:border-primary"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
{loading ? (
|
|
<div className="py-20 flex justify-center"><RefreshCw className="w-6 h-6 text-primary animate-spin" /></div>
|
|
) : (
|
|
<table className="crm-data-table">
|
|
<thead>
|
|
<tr className="bg-gray-50 border-y border-gray-200">
|
|
<th className="px-5 py-3 text-left text-[13px] font-semibold text-gray-700">Request ID</th>
|
|
<th className="px-5 py-3 text-left text-[13px] font-semibold text-gray-700">Entity</th>
|
|
<th className="px-5 py-3 text-left text-[13px] font-semibold text-gray-700">Part No. Verif.</th>
|
|
<th className="px-5 py-3 text-left text-[13px] font-semibold text-gray-700">Amount</th>
|
|
<th className="px-5 py-3 text-left text-[13px] font-semibold text-gray-700">Reason</th>
|
|
<th className="px-5 py-3 text-left text-[13px] font-semibold text-gray-700">Status</th>
|
|
<th className="px-5 py-3 text-center text-[13px] font-semibold text-gray-700">Action</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody className="bg-card">
|
|
{pager.items.length === 0 ? (
|
|
<tr><td colSpan={7} className="text-center py-12 text-muted-foreground text-[13px]">No refund requests found.</td></tr>
|
|
) : (
|
|
pager.items.map((r) => (
|
|
<tr key={r.refund_id} className="border-t border-border hover:bg-muted/10">
|
|
<td className="px-5 py-3 text-[13px] font-mono">{r.refund_id.slice(-8).toUpperCase()}</td>
|
|
<td className="px-5 py-3 text-[13px] font-semibold">
|
|
{r.entity_type === 'ECOM_ORDER' ? 'Order' : 'Service'}
|
|
<div className="text-[11px] text-muted-foreground font-mono">{r.entity_id}</div>
|
|
</td>
|
|
<td className="px-5 py-3 text-[13px] text-primary font-mono">{r.part_number_verification || '—'}</td>
|
|
<td className="px-5 py-3 text-[13px] text-foreground font-medium">{formatCurrency(r.amount)}</td>
|
|
<td className="px-5 py-3 text-[13px] text-muted-foreground max-w-[200px] truncate">{r.reason}</td>
|
|
<td className="px-5 py-3">
|
|
<span className={`px-2 py-1 text-[10px] font-bold uppercase rounded ${r.status === 'PENDING' ? 'bg-amber-100 text-amber-700' : r.status === 'APPROVED' ? 'bg-emerald-100 text-emerald-700' : 'bg-red-100 text-red-700'}`}>
|
|
{r.status}
|
|
</span>
|
|
</td>
|
|
<td className="px-5 py-3 text-center">
|
|
{r.status === 'PENDING' && (
|
|
<div className="flex justify-center gap-2">
|
|
<button onClick={() => handleAction(r.refund_id, 'APPROVE')} className="w-7 h-7 flex items-center justify-center text-emerald-600 bg-emerald-50 hover:bg-emerald-100 rounded border border-emerald-200">
|
|
<Check className="w-4 h-4" />
|
|
</button>
|
|
<button onClick={() => handleAction(r.refund_id, 'REJECT')} className="w-7 h-7 flex items-center justify-center text-red-600 bg-red-50 hover:bg-red-100 rounded border border-red-200">
|
|
<X className="w-4 h-4" />
|
|
</button>
|
|
</div>
|
|
)}
|
|
</td>
|
|
</tr>
|
|
))
|
|
)}
|
|
</tbody>
|
|
</table>
|
|
)}
|
|
<TablePagination {...pager} />
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|