function StockHistory({ salesUrl, token, branchUser }) { const [supplies, setSupplies] = React.useState([]); const [branches, setBranches] = React.useState([]); const [selectedBranch, setSelectedBranch] = React.useState(''); const [error, setError] = React.useState(''); const [loading, setLoading] = React.useState(false); const [startDate, setStartDate] = React.useState(''); const [endDate, setEndDate] = React.useState(''); const [selected, setSelected] = React.useState(null); const getEffectiveToken = () => { const storedBranchToken = typeof window !== 'undefined' ? (localStorage.getItem('branch_token') || '') : ''; return token || storedBranchToken || ''; }; const loadBranches = async () => { if (branchUser) return; try { const eff = getEffectiveToken(); const res = await fetch((salesUrl || '') + '/api/branches', { headers: { Authorization: 'Bearer ' + eff } }); const d = await res.json(); if (res.ok && Array.isArray(d.branches)) setBranches(d.branches); } catch (e) { // ignore } }; const loadSupplies = async (branchId) => { setLoading(true); setError(''); try { const storedBranchToken = typeof window !== 'undefined' ? (localStorage.getItem('branch_token') || '') : ''; const effectiveToken = token || storedBranchToken || ''; const url = new URL((salesUrl || '') + '/api/branch-supplies'); const bid = branchUser ? (branchUser.branch_id || branchUser._id || branchUser.id) : (branchId || ''); if (bid) url.searchParams.set('branch_id', bid); let res = await fetch(url.toString(), { headers: { Authorization: 'Bearer ' + effectiveToken } }); if (res.status === 401 && storedBranchToken && storedBranchToken !== effectiveToken) { res = await fetch(url.toString(), { headers: { Authorization: 'Bearer ' + storedBranchToken } }); } const data = await res.json(); if (!res.ok) throw new Error(data.message || 'Failed to load supplies'); setSupplies(Array.isArray(data.supplies) ? data.supplies : []); } catch (e) { setError(e.message); } finally { setLoading(false); } }; React.useEffect(() => { loadBranches(); if (branchUser) { const bid = branchUser.branch_id || branchUser._id || branchUser.id || ''; setSelectedBranch(bid); loadSupplies(bid); } else { loadSupplies(''); } }, [token, branchUser]); // Only show supplies that were created by branches (createdByType === 'branch') and match date range const visibleSupplies = React.useMemo(() => { if (!Array.isArray(supplies)) return []; let list = supplies.filter(s => String(s.createdByType || '').toLowerCase() === 'branch'); if (startDate) { try { const start = new Date(startDate); start.setHours(0,0,0,0); list = list.filter(s => { const when = s.createdAt ? new Date(s.createdAt) : null; if (!when) return false; return when.getTime() >= start.getTime(); }); } catch (e) { /* ignore invalid */ } } if (endDate) { try { const end = new Date(endDate); end.setHours(23,59,59,999); list = list.filter(s => { const when = s.createdAt ? new Date(s.createdAt) : null; if (!when) return false; return when.getTime() <= end.getTime(); }); } catch (e) { /* ignore invalid */ } } return list; }, [supplies, startDate, endDate]); return (
Track and manage your inventory supply history across all branches
{loading ? 'Loading supplies...' : `Showing ${visibleSupplies.length} supply record${visibleSupplies.length !== 1 ? 's' : ''}`}
| Supplier | Bank | Supplier Amount | GST Amount | Created By | Branch | Created At |
|---|---|---|---|---|---|---|
|
{s.supplierName || s.supplier || '-'}
|
{s.bankName || s.bank || '-'} | {s.supplierAmount != null ? new Intl.NumberFormat('en-IN', { style: 'currency', currency: 'INR', maximumFractionDigits: 2 }).format(s.supplierAmount) : '-' } | {s.gstAmount != null ? new Intl.NumberFormat('en-IN', { style: 'currency', currency: 'INR', maximumFractionDigits: 2 }).format(s.gstAmount) : '-' } | {s.createdByType === 'branch' ? 'Branch' : (s.createdByType === 'admin' ? 'Admin' : (s.createdBy || 'Unknown'))} |
{s.branch_name || s.branchName || (s.branch_id ? String(s.branch_id) : '-')}
|
{s.createdAt ? (
{new Date(s.createdAt).toLocaleDateString()}
{new Date(s.createdAt).toLocaleTimeString()}
|
Complete information about this supply record
| Product No | Product Name | Brand | Model | Qty | Cost Price | Validity |
|---|---|---|---|---|---|---|
| {it.productNo || it.productId || '-'} |
{it.productName || '-'}
|
{it.brand || '-'} | {it.model || '-'} | {it.qty != null ? it.qty : (it.quantity != null ? it.quantity : '-')} | {it.costPrice != null ? new Intl.NumberFormat('en-IN', { style: 'currency', currency: 'INR', maximumFractionDigits: 2 }).format(it.costPrice) : '-' } | {it.validity ? new Date(it.validity).toLocaleDateString() : '-'} |