import React, { useState, useEffect } from 'react'; import { Plus, Minus, ShoppingCart, Package, TrendingUp, TrendingDown, Edit2, Trash2, Save, X, Users, CreditCard, DollarSign, BarChart3, Download, AlertCircle } from 'lucide-react'; function AccountingInventorySystem() { const [activeTab, setActiveTab] = useState('dashboard'); const [products, setProducts] = useState([ { id: 1, name: 'Rice', category: 'Food', buyPrice: 45, sellPrice: 50, quantity: 100, unit: 'kg', minStock: 20 }, { id: 2, name: 'Lentils', category: 'Food', buyPrice: 80, sellPrice: 90, quantity: 50, unit: 'kg', minStock: 15 }, { id: 3, name: 'Oil', category: 'Food', buyPrice: 120, sellPrice: 130, quantity: 25, unit: 'liter', minStock: 10 } ]); const [sales, setSales] = useState([]); const [customers, setCustomers] = useState([ { id: 1, name: 'John Smith', phone: '01712345678', address: 'New York', due: 0 }, { id: 2, name: 'Sarah Johnson', phone: '01798765432', address: 'Chicago', due: 500 } ]); const [expenses, setExpenses] = useState([ { id: 1, category: 'Rent', amount: 15000, date: '2025-06-01', description: 'June rent payment' }, { id: 2, category: 'Electricity', amount: 3500, date: '2025-06-05', description: 'May electricity bill' } ]); const [showModal, setShowModal] = useState(false); const [modalType, setModalType] = useState(''); const [editingItem, setEditingItem] = useState(null); const [formData, setFormData] = useState({}); const [selectedCustomer, setSelectedCustomer] = useState(null); const [cart, setCart] = useState([]); const [showInvoice, setShowInvoice] = useState(false); const [lastInvoice, setLastInvoice] = useState(null); useEffect(() => { const sampleSales = [ { id: 1, productId: 1, productName: 'Rice', quantity: 10, price: 50, total: 500, date: '2025-06-15', customerId: 1, customerName: 'John Smith', paymentType: 'Cash', paid: 500, due: 0 } ]; setSales(sampleSales); }, []); const openModal = (type, item) => { setModalType(type); setEditingItem(item); setShowModal(true); if (type === 'product') { setFormData(item || { name: '', category: '', buyPrice: 0, sellPrice: 0, quantity: 0, unit: '', minStock: 10 }); } else if (type === 'customer') { setFormData(item || { name: '', phone: '', address: '', due: 0 }); } else if (type === 'supplier') { setFormData(item || { name: '', phone: '', address: '', due: 0 }); } else if (type === 'expense') { setFormData(item || { category: '', amount: 0, date: new Date().toISOString().split('T')[0], description: '' }); } }; const closeModal = () => { setShowModal(false); setEditingItem(null); setFormData({}); }; const handleSubmit = () => { if (modalType === 'product') { if (editingItem) { setProducts(products.map(p => p.id === editingItem.id ? { ...formData, id: editingItem.id } : p)); } else { setProducts([...products, { ...formData, id: Date.now() }]); } } else if (modalType === 'customer') { if (editingItem) { setCustomers(customers.map(c => c.id === editingItem.id ? { ...formData, id: editingItem.id } : c)); } else { setCustomers([...customers, { ...formData, id: Date.now() }]); } } else if (modalType === 'supplier') { if (editingItem) { setSuppliers(suppliers.map(s => s.id === editingItem.id ? { ...formData, id: editingItem.id } : s)); } else { setSuppliers([...suppliers, { ...formData, id: Date.now() }]); } } else if (modalType === 'expense') { if (editingItem) { setExpenses(expenses.map(e => e.id === editingItem.id ? { ...formData, id: editingItem.id } : e)); } else { setExpenses([...expenses, { ...formData, id: Date.now() }]); } } closeModal(); }; const deleteItem = (type, id) => { if (type === 'product') { setProducts(products.filter(p => p.id !== id)); } else if (type === 'customer') { setCustomers(customers.filter(c => c.id !== id)); } else if (type === 'supplier') { setSuppliers(suppliers.filter(s => s.id !== id)); } else if (type === 'expense') { setExpenses(expenses.filter(e => e.id !== id)); } }; const addToCart = (product) => { const existingItem = cart.find(item => item.id === product.id); if (existingItem) { setCart(cart.map(item => item.id === product.id ? { ...item, quantity: item.quantity + 1 } : item )); } else { setCart([...cart, { ...product, quantity: 1 }]); } }; const removeFromCart = (productId) => { setCart(cart.filter(item => item.id !== productId)); }; const updateCartQuantity = (productId, quantity) => { if (quantity <= 0) { removeFromCart(productId); } else { setCart(cart.map(item => item.id === productId ? { ...item, quantity } : item )); } }; const processCheckout = () => { if (cart.length === 0 || !selectedCustomer) return; const invoiceData = { id: Date.now(), date: new Date().toISOString().split('T')[0], customer: selectedCustomer, items: cart, total: cart.reduce((sum, item) => sum + (item.sellPrice * item.quantity), 0) }; cart.forEach(item => { const saleData = { id: Date.now() + Math.random(), productId: item.id, productName: item.name, quantity: item.quantity, price: item.sellPrice, total: item.sellPrice * item.quantity, date: new Date().toISOString().split('T')[0], customerId: selectedCustomer.id, customerName: selectedCustomer.name, paymentType: 'Cash', paid: item.sellPrice * item.quantity, due: 0 }; setSales(prev => [...prev, saleData]); setProducts(prev => prev.map(p => p.id === item.id ? { ...p, quantity: p.quantity - item.quantity } : p )); }); setLastInvoice(invoiceData); setCart([]); setSelectedCustomer(null); setShowInvoice(true); }; const totalSales = sales.reduce((sum, sale) => sum + sale.total, 0); const totalExpenses = expenses.reduce((sum, expense) => sum + expense.amount, 0); const totalProfit = totalSales - totalExpenses; const totalInventoryValue = products.reduce((sum, product) => sum + (product.quantity * product.buyPrice), 0); const tabs = [ { key: 'dashboard', label: 'Dashboard', icon: TrendingUp }, { key: 'products', label: 'Products', icon: Package }, { key: 'sales', label: 'Sales', icon: DollarSign }, { key: 'purchases', label: 'Purchases', icon: ShoppingCart }, { key: 'customers', label: 'Customers', icon: Users }, { key: 'suppliers', label: 'Suppliers', icon: Users }, { key: 'expenses', label: 'Expenses', icon: CreditCard }, { key: 'reports', label: 'Reports', icon: BarChart3 } ]; return (
Total Sales
৳{totalSales.toLocaleString()}
Total Expenses
৳{totalExpenses.toLocaleString()}
Net Profit
= 0 ? 'text-green-600' : 'text-red-600'}`}> ৳{Math.abs(totalProfit).toLocaleString()}
Inventory Value
৳{totalInventoryValue.toLocaleString()}
All products have sufficient stock
)}No sales recorded yet
)}Wholesale Sales Management
Add and manage your wholesale sales transactions here.
Features: Bulk quantity sales, wholesale pricing, customer credit management
Stock Purchase Management
Record your inventory purchases from suppliers.
Features: Bulk purchasing, supplier management, payment tracking
Supplier Database
Manage your suppliers and track payments.
Features: Contact management, due payment tracking, purchase history
Product Name | Category | Stock | Buy Price | Sell Price | Status | Actions |
---|---|---|---|---|---|---|
{product.name} | {product.category} |
{product.quantity} {product.unit}
{product.quantity <= product.minStock && (
|
৳{product.buyPrice} | ৳{product.sellPrice} | {product.quantity <= 5 ? 'Out of Stock' : product.quantity <= product.minStock ? 'Low Stock' : 'Good'} |
{activeTab === 'customers' && 'Manage your wholesale customers, track credit limits and payment history.'} {activeTab === 'expenses' && 'Track business expenses, categorize costs and monitor spending patterns.'} {activeTab === 'reports' && 'Generate detailed business reports, profit analysis and financial summaries.'}
{activeTab === 'customers' && 'Features: Credit management, bulk order history, payment terms'} {activeTab === 'expenses' && 'Features: Category-wise tracking, monthly summaries, tax preparation'} {activeTab === 'reports' && 'Features: P&L statements, inventory reports, customer analysis'}
Address & Phone Number
Invoice #: #{lastInvoice.id}
Date: {lastInvoice.date}
Customer: {lastInvoice.customer.name}
Product | Qty | Price | Total |
---|---|---|---|
{item.name} | {item.quantity} | ৳{item.sellPrice} | ৳{item.sellPrice * item.quantity} |
Total: ৳{lastInvoice.total}
Thank you!