/** * SCREEN-BARDANA-001: Bardana (Packaging Bags) Inventory Ledger & Commercials. * * Fulfills: * - STORY-BARDANA-001 (FR-BARDANA-001, FR-BARDANA-002, FR-BARDANA-003): * Double-entry packaging bag stock ledger, non-negative invariants, and packaging commercials. * * 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 { Package, Layers, ArrowDownLeft, ArrowUpRight, Calculator, Plus, RefreshCw, AlertTriangle, CheckCircle2, ShieldCheck, Building2, } from 'lucide-react'; export interface BardanaBalance { godown_id: string; bag_type: string; current_closing_count: number; last_reference_id?: string; last_updated?: string; } export interface StockEntryRecord { id: string; godown_id: string; bag_type: string; transaction_type: string; reference_id: string; opening_count: number; inward_count: number; outward_count: number; damaged_count: number; closing_count: number; created_at: string; } export const BardanaLedgerView: React.FC = () => { const [balances, setBalances] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [activeGodown, setActiveGodown] = useState('ALL'); // Modal states const [isEntryModalOpen, setIsEntryModalOpen] = useState(false); const [isCalcModalOpen, setIsCalcModalOpen] = useState(false); // Form states const [entryGodown, setEntryGodown] = useState('WH-01'); const [entryBagType, setEntryBagType] = useState('NEW_JUTE_50KG'); const [entryTxType, setEntryTxType] = useState('PURCHASE_RECEIPT'); const [entryRefId, setEntryRefId] = useState(''); const [entryOpening, setEntryOpening] = useState(0); const [entryInward, setEntryInward] = useState(0); const [entryOutward, setEntryOutward] = useState(0); const [entryDamaged, setEntryDamaged] = useState(0); // Calculator states const [calcTerm, setCalcTerm] = useState('BARDANA_ALAG'); const [calcCount, setCalcCount] = useState(500); const [calcRate, setCalcRate] = useState(65.0); const [calcResult, setCalcResult] = useState(null); const fetchBalances = useCallback(async () => { setLoading(true); setError(null); try { const url = activeGodown !== 'ALL' ? `/api/v1/bardana/balances?godown_id=${activeGodown}` : '/api/v1/bardana/balances'; const res = await apiClient.get<{ balances: BardanaBalance[] }>(url); setBalances(res.balances || []); } catch (err: any) { setError(err?.message || 'Failed to fetch bardana balances'); } finally { setLoading(false); } }, [activeGodown]); useEffect(() => { fetchBalances(); }, [fetchBalances]); const handleCreateEntry = async (e: React.FormEvent) => { e.preventDefault(); try { await apiClient.post('/api/v1/bardana/stock-entries', { godown_id: entryGodown, bag_type: entryBagType, transaction_type: entryTxType, reference_id: entryRefId, opening_count: entryOpening, inward_count: entryInward, outward_count: entryOutward, damaged_count: entryDamaged, }); setIsEntryModalOpen(false); fetchBalances(); } catch (err: any) { alert(err?.message || 'Failed to record stock entry'); } }; const handleCalculateCommercials = async () => { try { const res = await apiClient.post('/api/v1/bardana/calculate-commercials', { packaging_term: calcTerm, bag_count: calcCount, bag_type: entryBagType, bag_unit_rate: calcTerm === 'BARDANA_ALAG' ? calcRate : 0, filling_rate_per_bag: 2.5, stitching_rate_per_bag: 1.5, marking_rate_per_bag: 0.5, }); setCalcResult(res.commercials || res); } catch (err: any) { alert(err?.message || 'Calculation error'); } }; const totalBags = balances.reduce((sum, b) => sum + b.current_closing_count, 0); const juteBags = balances .filter((b) => b.bag_type.includes('JUTE')) .reduce((sum, b) => sum + b.current_closing_count, 0); const syntheticBags = balances .filter((b) => b.bag_type.includes('HDPE') || b.bag_type.includes('PP')) .reduce((sum, b) => sum + b.current_closing_count, 0); return (
{/* View Header */}

Bardana Inventory Ledger

Tactile sub-ledger for packaging bags (New/Old Jute, HDPE, PP) and commercial billing reconciliation.

{/* Summary KPI Cards */}
Total Closing Bags
{totalBags.toLocaleString()}

Across all storage godowns

Jute Packaging
{juteBags.toLocaleString()}

50kg & 100kg Gunny standard

Synthetic (HDPE / PP)
{syntheticBags.toLocaleString()}

Moisture barrier bags

Active Godowns
{new Set(balances.map((b) => b.godown_id)).size || 1}

Storage centres configured

{/* Main Stock Table */}

Physical Bag Balances by Godown

Filter Godown:
{error && (
{error}
)}
{balances.length === 0 ? ( ) : ( balances.map((b, idx) => ( )) )}
Godown Bag Type Closing Count Last Reference Last Movement Invariant Status
No bardana balance records found for selected godown. Click "Record Movement" to initialize stock.
{b.godown_id} {b.bag_type} {b.current_closing_count.toLocaleString()} {b.last_reference_id || 'INITIAL'} {b.last_updated ? new Date(b.last_updated).toLocaleString() : 'N/A'} Closing ≥ 0
{/* Commercial Terms Calculator Modal */} {isCalcModalOpen && (

Packaging Commercial Terms Calculator

setCalcCount(Number(e.target.value))} className="w-full bg-sidebar-surface text-white border border-card-border rounded p-2" />
{calcTerm === 'BARDANA_ALAG' && (
setCalcRate(Number(e.target.value))} className="w-full bg-sidebar-surface text-white border border-card-border rounded p-2" />
)} {calcResult && (
Bag Billing Amount: ₹{calcResult.bag_billing_amount?.toFixed(2)}
Handling Labor (Fill/Stitch/Mark): ₹{calcResult.packaging_labor_amount?.toFixed(2)}
Total Landed Packaging: ₹{calcResult.total_packaging_cost?.toFixed(2)}
)}
)} {/* Record Stock Movement Modal */} {isEntryModalOpen && (

Record Packaging Bag Movement

setEntryGodown(e.target.value)} className="w-full bg-sidebar-surface text-white border border-card-border rounded p-2" required />
setEntryRefId(e.target.value)} className="w-full bg-sidebar-surface text-white border border-card-border rounded p-2" required />
setEntryOpening(Number(e.target.value))} className="w-full bg-sidebar-surface text-white border border-card-border rounded p-2" />
setEntryInward(Number(e.target.value))} className="w-full bg-sidebar-surface text-white border border-card-border rounded p-2" />
setEntryOutward(Number(e.target.value))} className="w-full bg-sidebar-surface text-white border border-card-border rounded p-2" />
setEntryDamaged(Number(e.target.value))} className="w-full bg-sidebar-surface text-white border border-card-border rounded p-2" />
)}
); }; export default BardanaLedgerView;